我在Pygame上制作2D平台游戏。虽然我在面向左边的时候成功地激活了我的敌人精灵,但是当我翻转图像使他面朝右时,我也很难做同样的事情。我怀疑它是因为我在self.currentImage = 4
下面使用了self.x += self.speed
,所以它不断循环并跳过我blitted的其他图像。基本上,图像看起来只是跳过而从selfcurrentimage
1到self.currentimage
3,动画工作正常。
class Enemy:
def __init__(self,x,y):
self.x = x
self.y = y
self.width = 55
self.height = 51
self.rect = pygame.Rect(self.x,self.y,self.width,self.height)
self.speed=1
self.s0 = pygame.image.load("s0.png")
self.s1 = pygame.image.load("s1.png")
self.s2 = pygame.image.load("s2.png")
self.s3 = pygame.image.load("s3.png")
self.attack = pygame.image.load("attack.png")
self.rotateds0 = pygame.transform.flip(self.s0 ,True, False)
self.rotateds1 = pygame.transform.flip(self.s1 ,True, False)
self.rotateds2 = pygame.transform.flip(self.s2 ,True, False)
self.rotateds3 = pygame.transform.flip(self.s3 ,True, False)
self.rotate = False
collision2 = False
self.TimeTarget = 10
self.TimeNum = 0
self.currentImage = 0
def move(self,player):
if player.x > 100:
if self.x > player.x:
self.x -= self.speed
elif self.x < player.x:
self.x += self.speed
self.currentImage = 4
def update(self,CollisionDetect,player):
self.TimeNum+=1
if self.TimeNum == self.TimeTarget:
if self.currentImage == 0:
self.currentImage = 1
elif self.currentImage == 1:
self.currentImage = 2
elif self.currentImage == 2:
self.currentImage = 3
elif self.currentImage == 3:
self.currentImage = 0
elif self.currentImage == 4:
self.currentImage = 5
elif self.currentImage == 5:
self.currentImage = 6
elif self.currentImage == 6:
self.currentImage = 7
elif self.currentImage == 7:
self.currentImage = 4
self.TimeNum = 0
if self.currentImage == 0:
screen.blit(self.s0, (self.x,self.y))
elif self.currentImage == 1:
screen.blit(self.s1, (self.x,self.y))
elif self.currentImage == 2:
screen.blit(self.s2, (self.x,self.y))
elif self.currentImage == 3:
screen.blit(self.s3, (self.x,self.y))
elif self.currentImage == 4:
screen.blit(self.rotateds0, (self.x,self.y))
elif self.currentImage == 5:
screen.blit(self.rotateds1, (self.x,self.y))
elif self.currentImage == 6:
screen.blit(self.rotateds2, (self.x,self.y))
elif self.currentImage == 7:
screen.blit(self.rotateds3, (self.x,self.y))
collision2 = CollisionDetect(self.x,self.y,self.width,self.height,player.x,player.y,player.width,player.height)
答案 0 :(得分:1)
您在每次移动中都设置self.currentImage = 4
,因此无法更改为5,6或7.您必须在当前移动self.x < player.x
时才能这样做,但在之前的移动中有self.x > player.x
(敌人改变方向)。您可以使用self.currentImage
来识别这种情况 - 如果在之前的移动中您有self.x > player.x
,那么self.currentImage
仍然是< 4
def move(self,player):
if player.x > 100:
if self.x > player.x:
self.x -= self.speed
# enemy changed direction
if self.currentImage > 3:
self.currentImage = 0
elif self.x < player.x:
self.x += self.speed
# enemy changed direction
if self.currentImage < 4:
self.currentImage = 4
答案 1 :(得分:0)
您不应强制move()
中的帧编号。而不是使用一个单独的变量,它将从正常或翻转的图像中进行选择,并在move()
中为其设置一个值。也许你打算self.rotate
来达到这个目的。
这也将简化用于currentImage的状态机到简单的添加和环绕。