[英]Loaded image sequence flickers but only sometimes (Pygame)
First off, I have scoured the internet (including Stack Overflow) for an answer to my seemingly simple issue, but I have not found a fix that has worked for me. 首先,我搜寻了互联网(包括Stack Overflow)以解决我看似简单的问题,但是我没有找到适合我的解决方案。 I am attempting to make a simple top down game in pygame, and all worked well until I added in a walking animation for the player.
我试图在pygame中制作一个简单的自上而下的游戏,并且在我为播放器添加了行走动画之前,所有方法都运行良好。 The animation is fine except that it flickers at certain points.
除了在某些点闪烁外,动画还不错。
I have an idle animation for the player as well, but there is no flickering whatsoever. 我也为播放器设置了一个空闲动画,但是没有任何闪烁现象。 The idle animation has 2 frames, and the walking animations has 4, which brings me to suspect the problem lies within the number of frames.
空闲动画有2帧,行走动画有4帧,这使我怀疑问题在于帧数之内。
Below I have pasted the code where I believe the problem lies along with this link ( https://imgur.com/P1CzyRX ) to a gif of the walking animation. 下面,我将代码粘贴到了我认为问题所在的位置,并将此链接( https://imgur.com/P1CzyRX )粘贴到了行走动画的gif上。 If a zip file with the code and images would be best, I am more than happy to provide that as well.
如果最好是带有代码和图像的zip文件,我也很乐意提供。 Lastly, I am aware that some of my coding habits are not the best, such as using global variables, so I hope anyone reading through my code isn't too bothered by that.
最后,我知道我的一些编码习惯不是最好的,例如使用全局变量,所以我希望任何阅读我的代码的人都不会对此感到困扰。 Thank you so much, it means a lot!
非常感谢,这很重要!
class Player:
def drawPlayer(self):
global playerCounter, playerWalking
if playerWalking == "false":
if player1Facing == "front":
player1.loopIdle(playerIdleFrontAnim)
if player1Facing == "back":
player1.loopIdle(playerIdleBackAnim)
if player1Facing == "right":
player1.loopIdle(playerIdleRightAnim)
if player1Facing == "left":
player1.loopIdle(playerIdleLeftAnim)
elif playerWalking == "true":
if player1Facing == "front":
player1.loopWalking(playerWalkFrontAnim)
if player1Facing == "back":
player1.loopWalking(playerWalkBackAnim)
if player1Facing == "right":
player1.loopWalking(playerWalkRightAnim)
if player1Facing == "left":
player1.loopWalking(playerWalkLeftAnim)
def loopWalking(self, animation):
global playerCounter, currentPlayerStance
if playerCounter <= playerWalkMax:
screen.blit(animation[0], (player1X, player1Y))
currentPlayerStance = "upright"
elif (playerCounter > playerWalkMax) and (playerCounter < playerWalkMax * 2):
screen.blit(animation[1], (player1X, player1Y))
currentPlayerStance = "crouch"
elif (playerCounter > (playerWalkMax * 2)) and (playerCounter < playerWalkMax * 3):
screen.blit(animation[2], (player1X, player1Y))
currentPlayerStance = "upright"
elif playerCounter > playerWalkMax * 3:
screen.blit(animation[3], (player1X, player1Y))
currentPlayerStance = "crouch"
if playerCounter >= playerWalkMax * 4:
playerCounter = 0
playerCounter += 1
def loopIdle(self, animation):
global playerCounter, currentPlayerStance
if playerCounter <= playerIdleMax:
screen.blit(animation[0], (player1X, player1Y))
currentPlayerStance = "upright"
elif playerCounter > playerIdleMax:
screen.blit(animation[1], (player1X, player1Y))
currentPlayerStance = "crouch"
if playerCounter >= (playerIdleMax * 2):
playerCounter = 0
playerCounter += 1
player1 = Player()
done = 0
clock = pygame.time.Clock()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = 1
elif event.type == pygame.KEYDOWN:
player1XSpeed = 0
player1YSpeed = 0
if event.key == pygame.K_ESCAPE:
done = 1
elif event.key == pygame.K_LEFT:
if player1Facing != "left":
player1Facing = "left"
player1XSpeed = -player1SpeedVal
playerWalking = "true"
elif event.key == pygame.K_RIGHT:
if player1Facing != "right":
player1Facing = "right"
player1XSpeed = player1SpeedVal
playerWalking = "true"
elif event.key == pygame.K_UP:
if player1Facing != "back":
player1Facing = "back"
player1YSpeed = -player1SpeedVal
playerWalking = "true"
elif event.key == pygame.K_DOWN:
if player1Facing != "front":
player1Facing = "front"
player1YSpeed = player1SpeedVal
playerWalking = "true"
elif event.type == pygame.KEYUP:
playerWalking = "false"
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
player1XSpeed = 0
elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
player1YSpeed = 0
# Update player1 movement
player1X += player1XSpeed
player1Y += player1YSpeed
screen.fill(GRAY)
if drawPlayer1 == "true":
player1.loopWalking(playerWalkRightAnim)
clock.tick(60)
pygame.display.flip()
pygame.quit()
You're skipping some frames, 50 and 75. If playerCounter
is 50, both of these conditions are False
and you blit nothing. 您跳过了一些帧,分别为50和75。如果
playerCounter
为50,则这两个条件均为False
并且不作任何处理。
elif (playerCounter > playerWalkMax) and (playerCounter < playerWalkMax * 2):
elif (playerCounter > playerWalkMax * 2) and (playerCounter < playerWalkMax * 3):
You can fix the code by checking if the playerCounter
is <= playerWalkMax
: 您可以通过检查
playerCounter
是否为<= playerWalkMax
来修复代码:
elif (playerCounter > playerWalkMax) and (playerCounter <= playerWalkMax * 2):
elif (playerCounter > playerWalkMax * 2) and (playerCounter <= playerWalkMax * 3):
I used some prints to figure out which frames were skipped: 我使用了一些照片来找出哪些帧被跳过了:
def loopWalking(self, animation):
global playerCounter
if playerCounter <= playerWalkMax:
screen.blit(animation[0], (player1X, player1Y))
print('blit 0', end=' ')
elif (playerCounter > playerWalkMax) and (playerCounter < playerWalkMax * 2):
screen.blit(animation[1], (player1X, player1Y))
print('blit 1', end=' ')
elif (playerCounter > playerWalkMax * 2) and (playerCounter < playerWalkMax * 3):
screen.blit(animation[2], (player1X, player1Y))
print('blit 2', end=' ')
elif playerCounter > playerWalkMax * 3:
screen.blit(animation[3], (player1X, player1Y))
if playerCounter >= playerWalkMax * 4:
playerCounter = 0
print('blit 3', end=' ')
print(playerCounter)
playerCounter += 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.