python - 为什么我的角色会同时闪烁两个不同的图像
问题描述
我正在制作游戏,并且角色要根据他所面临的位置来显示枪支动画,这是确定的代码
class Player(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.velo = 8
self.left = False
self.right = False
self.walkcount = 0
self.Idlecount = 0
self.guncount = 0
self.gunisfired = False
self.isIdle = False
self.standing = True
def draw(self, screen):
#draws all the animations to the screen
if self.walkcount + 1 >= 27:
self.walkcount = 0
elif self.Idlecount + 1 >= 27:
self.Idlecount = 0
if not(self.standing):
if self.left:
screen.blit(walkLeft[self.walkcount//3], (self.x,self.y))
self.walkcount += 1
if self.gunisfired == True:
if self.guncount//5 >= len(ShootR):
self.guncount = 0
self.gunisfired == False
screen.blit(ShootR[self.guncount//5], (self.x,self.y))
self.guncount += 1
if self.right:
screen.blit(walkRight[self.walkcount//3], (self.x,self.y))
self.walkcount += 1
if self.gunisfired == True:
if self.guncount//5 >= len(ShootR):
self.guncount = 0
self.gunisfired == False
screen.blit(ShootR[self.guncount//5], (self.x,self.y))
self.guncount += 1
else:
if self.left:
if self.isIdle == True:
if self.Idlecount//3 >= len(IdleL):
self.Idlecount = 0
self.isIdle == False
screen.blit(IdleL[self.Idlecount//3], (self.x,self.y))
self.Idlecount += 1
if self.gunisfired == True:
if self.guncount//5 >= len(ShootL):
self.guncount = 0
self.gunisfired == False
screen.blit(ShootL[self.guncount//5], (self.x,self.y))
self.guncount += 1
else:
if self.Idlecount//3 >= len(IdleR):
self.Idlecount = 0
self.isIdle == False
screen.blit(IdleR[self.Idlecount//3], (self.x,self.y))
self.Idlecount += 1
if self.gunisfired == True:
if self.guncount//5 >= len(ShootR):
self.guncount = 0
self.gunisfired == False
screen.blit(ShootR[self.guncount//5], (self.x,self.y))
self.guncount += 1
pygame.display.update()
这是键盘绑定的代码
man = Player(300, 508, 64, 64)
run = True
while run:
clock.tick(27)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and man.x > man.velo:
man.x -= man.velo
man.right = False
man.left = True
man.Idlecount = 0
man.guncount = 0
man.gunisfired = False
man.standing = False
elif keys[pygame.K_RIGHT] and man.x < 600 - man.width - man.velo:
man.x += man.velo
man.right = True
man.left = False
man.Idlecount = 0
man.guncount = 0
man.gunisfired = False
man.standing = False
elif keys[pygame.K_SPACE]:
man.gunisfired = True
man.isIdle = False
man.standing = True
man.Idlecount = 0
else:
man.walkcount = 0
man.isIdle = True
man.guncount = 0
man.gunisfired = False
man.standing = True
值得注意的是,当他面向左侧时,枪图像正在工作,但当它面向右侧时,它似乎用空闲动画的第一个图像对其进行了 blit。有人可以帮帮我吗。
解决方案
if self.isIdle == True
在方法的 else 情况下丢失draw
:
class Player(object):
# [...]
def draw(self, screen):
# [...]
if not(self.standing):
# [...]
else:
if self.left:
# [...]
else:
if self.isIdle == True: # <---- this is missing
if self.Idlecount//3 >= len(IdleR):
self.Idlecount = 0
self.isIdle == False
screen.blit(IdleR[self.Idlecount//3], (self.x,self.y))
self.Idlecount += 1
if self.gunisfired == True:
if self.guncount//5 >= len(ShootR):
self.guncount = 0
self.gunisfired == False
screen.blit(ShootR[self.guncount//5], (self.x,self.y))
self.guncount += 1
推荐阅读
- angular - Chart.js 注释工作但在 Angular 5 中导致控制台错误
- json - VBA 授权 oauth2 API
- python - 在 Python 中解析子标签 XML
- c# - 如何在使用 iTextsharp 签署 pdf 时优化时间
- windows - Packer 错误消息:找不到名为 'packer-virtualbox-iso-# 的注册机器
- python - 如何删除matplotlib散点图方形标记中的圆边?
- excel - 如何从这个 Excel 表中获取我想要的值?
- unity3d - 在Unity中忽略origin.forward查找2点之间的角度
- wpf - WPF 多绑定/聚合绑定到集合
- angular - 什么是 Observables 以及它们在 HttpClient 中有何不同