gpt4 book ai didi

python - 为什么我的 Sprite 不经常出现,而不是静止不动时通过图像动画

转载 作者:行者123 更新时间:2023-12-04 10:37:09 25 4
gpt4 key购买 nike

所以当我创建一个游戏时,我的 Sprite 是一只 bat 。到目前为止,当用户按住左键或右键时,动画效果很好;但是,我尝试添加用户站立不动的位置,“飞行”动画仍在播放,面向玩家最后面对的方向,因此看起来 bat 并不是只是静止不动。

我得到的结果是 Sprite 不会出现在屏幕上,除非我按住其中一个移动键,当我只想让它持续动画时,即使用户没有移动。

我可以通过在静止时显示单个图像来使 Sprite 面向某个方向,但不能正确设置动画。

我听说过库 pyganim,但由于我所处的环境我无法使用它,所以我正在为选择而苦苦挣扎。

下面是 draw 函数的相关代码:

     def draw(self,window): 
#we must keep track of direction, are they moving and how many steps for frames
if self.walkcount +1 >= 15: #15 as we have 5 sprites, which will be displayed 3 times per second
self.walkcount = 0

if not(self.standing): #if hes not standing still or moving, we'll walk through our animation
if self.left:
window.blit(walkleft[self.walkcount//3],(self.x,self.y))#excludes remainders
self.walkcount+=1
elif self.right:
window.blit(walkright[self.walkcount//3],(self.x,self.y))
self.walkcount+=1
elif self.isjump:
window.blit(jumpp,(self.x,self.y))
self.walkcount+=1
else: #if he is, we'll blit him looking right or left
if self.right:
if self.walkcount +1 >= 15:#
self.walkcount = 0#
while True:#
#anim1.blit(window,(self.x,self.y)) #previous attempt
#window.blit(walkright[0],(self.x,self.y)) #displays first image of that direction when stood still
window.blit(walkright[self.walkcount//3],(self.x,self.y))
self.walkcount+=1
else:
if self.walkcount +1 >= 15:
self.walkcount = 0#
while True:#
#anim2.blit(window,(self.x,self.y))
#window.blit(walkleft[0],(self.x,self.y))
window.blit(walkright[self.walkcount//3],(self.x,self.y))
self.walkcount+=1

这是为 right 或 left 为真的地方完成基本动画的地方,动画将在它为真时播放。

我一直在编辑的实际部分试图让它工作:
else: #if he is, we'll blit him looking right or left
if self.right:
if self.walkcount +1 >= 15:#
self.walkcount = 0#
while True:#
#anim1.blit(window,(self.x,self.y)) #previous attempt
#window.blit(walkright[0],(self.x,self.y)) #displays first image of that direction
window.blit(walkright[self.walkcount//3],(self.x,self.y))
self.walkcount+=1
else:
if self.walkcount +1 >= 15:
self.walkcount = 0#
while True:#
#anim2.blit(window,(self.x,self.y))
#window.blit(walkleft[0],(self.x,self.y))
window.blit(walkright[self.walkcount//3],(self.x,self.y))
self.walkcount+=1

运动功能:
if keys[pygame.K_LEFT] and man.x > man.vel-man.vel: #if pressed we move our character by the velocity in whatever direction via grid which works from the TOP LEFT of screen
#vel-vel to equal 0, makes border better
man.x -= man.vel
man.left = True #were global variables
man.right = False
man.standing = False
elif keys[pygame.K_RIGHT] and man.x < 500 - man.width:
man.x += man.vel #screenwidth
man.right = True
man.left = False
man.standing = False
else:
man.standing = True #now we'll know if he's looking left or right
man.walkcount = 0

任何其他建议/反馈表示赞赏。

最佳答案

因为当站着不动时,你设置:

    man.standing = True #now we'll know if he's looking left or right
man.walkcount = 0

所以 walkcount0standingTrue .

draw功能, not(self.standing)False ,所以我们跳转到 else堵塞。

还有一个 if/else ,并在每个分支上检查
if self.walkcount +1 >= 15:

永远不会是 True因为 walkcount0所以 window.blit永远不会到达任一分支中的行;因此图像不会被传送到窗口。

无需测试,我认为整个方法可以简化为
 def draw(self,window): 

#we must keep track of direction, are they moving and how many steps for frames
if self.walkcount +1 >= 15: #15 as we have 5 sprites, which will be displayed 3 times per second
self.walkcount = 0

if self.isjump:
window.blit(jumpp,(self.x,self.y))
else:
if self.left:
window.blit(walkleft[self.walkcount//3],(self.x,self.y))#excludes remainders
else:
window.blit(walkright[self.walkcount//3],(self.x,self.y))

self.walkcount+=1

和线
    man.walkcount = 0

应该被删除。

关于python - 为什么我的 Sprite 不经常出现,而不是静止不动时通过图像动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60115822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com