메뉴 건너뛰기

OBG

Programming

MoA
조회 수 545 추천 수 0 댓글 1
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부

7_main.png

 

농부가 농사짓는데 필요한 오브젝트도 추가했으니 이제 서로 상호작용할 함수를 추가해보자.

 

1. Farmer 클래스에 interact() 함수 추가

 

 

    def interact(self):
        checkspot = self.rect.move((-64 * (self.facing == 'left') + 64 * (self.facing == 'right')), (-64 * (self.facing == 'up') + 64 * (self.facing == 'down')))  # Determines the block the farmer is facing
        found_object = checkspot.collidelist(objectlist)
        
        if found_object != -1:
            entity = objectlist[found_object]
            if entity.__class__.__name__ == 'Plot':
                plot = entity
                if plot.state == 'dry' and self.water > 0:
                    plot.wet()
                    self.water -= 1
            elif entity.__class__.__name__ == 'Well':
                well = entity
                self.water = self.water_max
            elif entity.__class__.__name__ == 'Shop':
                shop = entity
                shop.open()
            elif entity.__class__.__name__ == 'WateringCan':
                wateringcan = entity
                self.water_max = 4
                wateringcan.kill()
                hud_sprites = allhudsprites.sprites()
                for sprite in hud_sprites:
                    if sprite.name == "Watering Can":
                        sprite.image = load_img('HUD', 'watering_can_large.png')
                        objectlist.remove(wateringcan) 

 

처음에는 충돌 체크를 한다. 농부의 이동방향에 따라 충돌 체크 위치를 정하는 부분을 잘 이해하도록 하자. collidelist() 함수를 통해 충돌하는 오브젝트가 있다면 해당 오브젝트의 인덱스가 found_object에 저장된다. 그리고 그 오브젝트의 클래스 네임에 따라 원하는 동작을 한다. 땅이라면 땅을 젖게 하고 우물이라면 물뿌리개를 가득 채우고 하는 식이다. 만약 물뿌리개를 얻었다면 hud 부분에 물뿌리개 이미지를 바꾸어야 한다. 이 작업을 하려면 hud의 sprite 변수를 얻어와야 하는데 이건 allhudsprites.sprites() 부분을 통해 할 수 있다. 이 함수를 통해 얻은 sprite 리스트에서 sprite의 이름이 Watering Can이면 이것의 이미지를 바꾼다. 또한 물뿌리개를 얻었으므로 화면상에서 물뿌리개를 제거해야 하는데 이는 remove() 함수를 통해 할 수 있다. (물뿌리개 클래스가 Sprite 클래스를 상속받았으므로 가능하다.)

 

 

2. 입력 체크 부분 수정

 

 

                elif event.key == K_SPACE:
                    farmer.interact()


스페이스키를 눌렀을 때 interact()함수를 호출하는 부분이다. 간단하다.

 

 

 

 

 

 

 

?

  1. Programming 게시판 관련

    Date2014.11.01 CategoryTool/etc ByMoA Views1946
    read more
  2. UI Guidelines

    Date2014.03.11 CategoryTool/etc ByMoA Views534
    Read More
  3. [S/W 공학] 월-인원(man-month), LOC

    Date2013.09.23 CategoryTool/etc ByMoA Views543
    Read More
  4. [농장게임 만들기] 7. 농부 행동 추가

    Date2014.05.01 CategoryPython ByMoA Views545
    Read More
  5. [농장게임 만들기] 5. 플레이어를 추가하자

    Date2014.04.30 CategoryPython ByMoA Views546
    Read More
  6. 리스트 컨트롤 클릭 이벤트

    Date2013.06.12 CategoryAPI/MFC ByMoA Views548
    Read More
  7. Legacy MFC 어플리케이션을 MFC feature pack으로 포팅

    Date2013.07.30 CategoryAPI/MFC ByMoA Views557
    Read More
  8. 2048게임 높은 점수 얻기 위한 알고리즘

    Date2014.03.29 CategoryAlgorithm ByMoA Views570
    Read More
  9. 특정 자료형의 데이터를 binary(hex값, 2진수값)으로 변환

    Date2012.11.15 CategorySite ByNaya Views573
    Read More
  10. 정적 배열과 STL vector 속도 비교

    Date2013.07.28 CategorySTL/Boost ByMoA Views573
    Read More
  11. MFC TIP

    Date2013.07.28 CategoryAPI/MFC ByMoA Views574
    Read More
  12. R language 사이트

    Date2012.02.08 CategoryTool/etc By너울 Views576
    Read More
  13. 정신나간 정렬 알고리즘

    Date2015.10.13 CategoryC/C++ ByMoA Views592
    Read More
  14. PackageBroadcastReceiver 구현

    Date2013.05.22 CategoryJAVA/Android ByMoA Views600
    Read More
  15. Thread Programming

    Date2012.08.02 CategoryAPI/MFC ByNaya Views602
    Read More
  16. fopen 함수가 Multi Thread 에서 안전한가?

    Date2013.07.28 CategoryC/C++ ByMoA Views604
    Read More
  17. ChartFX 7.0 MFC에서 사용하기

    Date2013.07.28 CategoryLibrary ByMoA Views609
    Read More
  18. __FILE__ __LINE__ __FUNCTION__ 등 매크로

    Date2014.01.02 CategoryC/C++ ByMoA Views614
    Read More
  19. [농장게임 만들기] 4. 펜스를 그리자

    Date2014.04.30 CategoryPython ByMoA Views620
    Read More
  20. SciPy and NumPy

    Date2013.12.23 CategoryPython ByMoA Views621
    Read More
  21. 스레드(CreateThread), EVENT 동기화

    Date2013.07.28 CategoryAPI/MFC ByMoA Views628
    Read More
Board Pagination Prev 1 ... 5 6 7 8 9 10 11 12 13 14 ... 15 Next
/ 15
위로