我正在使用pygame在Python中制作一个小游戏,我试图让我的角色在屏幕上向不同的方向移动,以响应用户按下键盘上的方向箭头。角色进入的每个方向都有3个精灵,应该连续显示。问题是,即使角色确实向上移动了根据我按下键盘箭头向上&下。只有向上箭头的精灵有效,如果我试图将角色向下移动它会移动,但它会保持在我向上移动时的前一个精灵状态(即根本没有显示向下的精灵)。这很奇怪,因为up和amp;的代码精灵是完全相同的&但只有其中一个在另一个上面工作!我已经包含了我认为相关的代码部分,我们非常感谢任何帮助。
pygame.init()
screen = pygame.display.set_mode((900,70))
finished = False
x = 450 - 35/2
y = 650 #initial co-ordiantes of the player
backSprites = ["data/hero/walk-back1.png", "data/hero/walk-back2.png", "data/hero/stand-back.png"]
back_index = 0
backSpritesModified1 = [pygame.image.load(filename) for filename in backSprites]
backSpritesModified2 = [pygame.transform.scale((filename), (45, 50) for filename in backSpritesModified1]
backSpritesModified3 = [(filename).convert_alpha() for filename in backSpritesModified2]
#These previous lines work perfectly when the up button is pressed
frontSprites = ["data/hero/walk-front1.png", "data/hero/walk-front2.png", "data/hero/stand-front.png"]
front_index = 0
frontSpritesModified1 = [pygame.image.load(filename) for filename in frontSprites]
frontSpritesModified2 = [pygame.transform.scale((filename), (45, 50) for filename in frontSpritesModified1]
frontSpritesModified3 = [(filename).convert_alpha() for filename in frontSpritesModified2]
#as you can see it's written in the same syntax as the previous part but doesn,t work
代码的以下部分包含在while循环中,就像我说的那样,它是唯一相关的部分所以我只包含它们
while finished == False #while the game isn't finished yet
for event in pygame.event.get():
if event.type == pygame.QUIT:
finished = True
pressedKeys = pygame.key.get_pressed()
if pressedKeys[pygame.K_UP] == 1:
y -= 5
back_index = (back_index + 1) %len(backSpritesModified3)
if pressedKeys[pygame.K_DOWN] == 1:
y += 5
front_index = (front_index + 1) %len(frontSpritesModified3)
#not working even though it's the same syntax as the previous one!
答案 0 :(得分:0)
这是一个没有图像的最小工作示例,但是您将在屏幕上看到文本,显示正在播放的动画以及它所在的帧。这应该可以帮助您了解如何基于实体状态在动画之间进行切换。有很多方法可以做到这一点。此外,如果没有看到代码的其他部分,很难看出问题出在哪里。
import pygame
pygame.init()
screen = pygame.display.set_mode((800,600))
clock = pygame.time.Clock()
font = pygame.font.SysFont(pygame.font.get_default_font(), 25)
running = True;
anim_idle = ['idle-1']
anim_up = ['up-1', 'up-2', 'up-3']
anim_down = ['down-1', 'down-2', 'down-3']
anim_dict = {'up': anim_up, 'down': anim_down, 'idle': anim_idle}
anim_speed = 0.25# each frame of the animation lasts 1/4 of a second
current_anim_name = 'idle'
current_anim = anim_dict.get(current_anim_name)
current_anim_frame = 0
current_frame_time = 0
def change_anim(anim_name):
global current_anim_name, current_anim, current_anim_frame, current_frame_time
current_anim_name = anim_name
current_anim = anim_dict.get(anim_name)
current_anim_frame = 0
current_frame_time = 0
while running:
dt = clock.tick() * 0.001# delta time in seconds
current_frame_time = current_frame_time + dt
if current_frame_time >= anim_speed:
current_anim_frame = (current_anim_frame + 1)%len(current_anim)
current_frame_time = 0
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
if event.key == pygame.K_UP:
change_anim('up')
elif event.key == pygame.K_DOWN:
change_anim('down')
elif event.key == pygame.K_LEFT:
pass
elif event.key == pygame.K_RIGHT:
pass
elif event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
change_anim('idle')
elif event.key == pygame.K_DOWN:
change_anim('idle')
elif event.key == pygame.K_LEFT:
pass
elif event.key == pygame.K_RIGHT:
pass
screen.fill((0,0,0))
screen.blit(font.render(current_anim_name + ' :: frame ' + current_anim[current_anim_frame], True, (255,255,255)), (0,0))
pygame.display.flip()
pygame.quit()