首先,我已经浏览了互联网(包括Stack Overflow)以回答我看似简单的问题,但我还没有找到适用于我的修复程序。我试图在pygame中制作一个简单的自上而下游戏,并且一直运行良好,直到我为玩家添加了一个步行动画。动画很好,只是它在某些点闪烁。
我也为玩家提供了空闲动画,但没有任何闪烁。空闲动画有2帧,步行动画有4帧,这让我怀疑问题在于帧数。
下面我将代码粘贴到我认为问题所在的代码以及此链接(https://imgur.com/P1CzyRX)到步行动画的gif。如果带有代码和图像的zip文件最好,我也非常乐意提供。最后,我知道我的一些编码习惯不是最好的,例如使用全局变量,所以我希望任何阅读我的代码的人都不会为此烦恼。非常感谢,这意味着很多!
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()
答案 0 :(得分:0)
你正在跳过一些帧,50和75.如果playerCounter
是50,那么这两个条件都是False
而你什么也没有。
elif (playerCounter > playerWalkMax) and (playerCounter < playerWalkMax * 2):
elif (playerCounter > playerWalkMax * 2) and (playerCounter < playerWalkMax * 3):
您可以通过检查playerCounter
是<= playerWalkMax
:
elif (playerCounter > playerWalkMax) and (playerCounter <= playerWalkMax * 2):
elif (playerCounter > playerWalkMax * 2) and (playerCounter <= playerWalkMax * 3):
我使用了一些打印来确定跳过哪些帧:
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