如何解决Pygame角色动画在左右两侧均不起作用
我创建了一个名为JumpyMan的游戏。
我创建了游戏机制,例如左右移动,跳跃和摔倒。
由于游戏的外观不太好,我添加了一些角色动画(精灵),当您按Key D
时,动画开始并且角色朝着正确的方向移动。
但是,当我按Key A
时,角色向左移动,但动画没有开始。当我同时按下两个键时,开始播放左动画,但我的角色保持静止。
这是我的代码:https://github.com/zewutz/jumpyman(所有需要的图像都在github repo中)
import pygame
import os
from pygame import Surface
from pygame.transform import scale
pygame.init()
#Screen,title/icon and clock
screen = pygame.display.set_mode((1366,650))
pygame.display.set_caption("JumpyMan")
iconImg = pygame.image.load('resources\images\jumpyman\stationary\statright.png')
iconImg = pygame.transform.scale(iconImg,(100,128))
pygame.display.set_icon(iconImg)
clock = pygame.time.Clock()
#backgroung
backgroundImage = pygame.image.load('resources\images\Imgbackground\jumpymanbackground.jpg')
backgroundCoord = (0,-350)
#Player sprites
left = [None]*10
for picIndex in range(1,10):
left[picIndex-1] = pygame.image.load(os.path.join('resources\images\jumpyman\imgleft',"L" + str(picIndex) + ".png"))
picIndex += 1
right = [None]*10
for picIndex in range(1,10):
right[picIndex-1] = pygame.image.load(os.path.join('resources\images\jumpyman\imgright',"Run__00" + str(picIndex) + ".png"))
picIndex += 1
# Player
playerImg = pygame.image.load('resources\images\jumpyman\stationary\statright.png')
smallerImage = pygame.transform.scale(playerImg,(50,100))
x = 10
y = 460
vel_x = 5
vel_y = 15
jump = False
move_left = False
move_right = False
stepIndex = 0
def characterMovementX():
global stepIndex
if stepIndex >= 9:
stepIndex = 0
if move_left:
screen.blit(left[stepIndex],(x,y))
stepIndex += 1
elif move_right:
screen.blit(right[stepIndex],y))
stepIndex += 1
else:
screen.blit(smallerImage,y))
#Game loop
game = True
while game:
screen.blit(backgroundImage,backgroundCoord)
for event in pygame.event.get():
if event.type == pygame.QUIT:
game = False
#Move left,right
userInput = pygame.key.get_pressed()
if userInput[pygame.K_a] and x > 5 :
move_left = True
x -= vel_x
if userInput[pygame.K_d] and x < 1280:
move_right = True
x += vel_x
else:
move_left = False
move_right = False
stepIndex = 0
#Jump
if jump is False and userInput[pygame.K_SPACE]:
jump = True
if jump:
y -= vel_y
vel_y -= 1
if vel_y < -15:
jump = False
vel_y = 15
characterMovementX()
clock.tick(30)
pygame.display.update()
pygame.quit()
解决方法
该错误是由向右移动的if
-else
语句引起的:
if userInput[pygame.K_a] and x > 5 : move_left = True x -= vel_x if userInput[pygame.K_d] and x < 1280: move_right = True x += vel_x else: move_left = False move_right = False stepIndex = 0
如果播放器没有向右移动,则将执行else
的情况并将move_left
设置为Flase
,即使之前将True
设置为{
您必须使用if
-elif
-else
语句:
if userInput[pygame.K_a] and x > 5 :
move_left = True
move_right = False
x -= vel_x
elif userInput[pygame.K_d] and x < 1280:
move_left = False
move_right = True
x += vel_x
else:
move_left = False
move_right = False
stepIndex = 0
我建议在选择之前设置move_left = False
和move_right = False
:
move_left = False
move_right = False
if userInput[pygame.K_a] and x > 5 :
move_left = True
x -= vel_x
elif userInput[pygame.K_d] and x < 1280:
move_right = True
x += vel_x
else:
stepIndex = 0
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。