2014-04-16 2 views
0

헬스가 0이되면 '적'에 대한 이미지가 변경되는 프로그램을 작성하려고합니다. 각 요소마다 다른 클래스가 있지만, 이 수업에서.pygame : 아이템/적의 이미지 대체

class Enemy(pygame.sprite.Sprite): 
    def __init__(self, gs=None): 
      pygame.sprite.Sprite.__init__(self) 
      ...initialization stuff... 
      self.image = pygame.image.load("enemy.png") #enemy image 
      self.rect = self.image.get_rectangle() 

      self.hp = 40 #initial health 

    def damage(self): 
      if self.rect.colliderect(self.user.rect): #collision/how the health goes down 
            self.hp = self.hp - 5 

지금 여기에 내가 나는이 적을 가지고있는 기존 이미지를 대체하는 새 이미지를로드 할에 대해 궁금 부분입니다. 추가/추가 할 수 있습니까 (손상 기능 있음)

  if(self.hp == 0): 
        self.image = pygame.image.load("dead.png") 

대체 할 수 있습니까? 또는 다른 이미지를 그 위에 올려 놓으시겠습니까? 내가 무엇을 놓치고 있는지 알려주세요, 고마워요!

답변

1

개체를 만들 때 모든 이미지를 init 메서드로로드 한 다음 나중에 사용 목록 할당/변경을 수행 할 수 있습니다.

class Enemy(pygame.sprite.Sprite): 
    def __init__(self, gs=None): 
     pygame.sprite.Sprite.__init__(self) 
     self.images = [pygame.image.load("enemy.png"), pygame.image.load("dead.png")] 
     self.current_image = self.images[0] 
     self.rect = self.current_image.get_rectangle() 

그런 다음 나중에 당신이 할 수 있습니다 :

if(self.hp == 0): 
    self.current_image = self.images[1] 

이 의지 사실, 당신의 관심사에 따라, 단지 그 위에 그리는 대신 현재 이미지를 대체 여기에 예입니다. 희망이 도움이됩니다.

+0

잘 했어.하지만 실제로는 이미지를 대체하고 그 위에 그릴뿐 아니라 그 위에 다른 사람들에게 좋은 제안을 함을 분명히하지 않았다. – KodyVanRy

+0

@ DuhProgrammer13, 감사합니다. 지금 내 대답을 업데이트 할 것입니다. – sshashank124