如何解决我在使用 pygame 动画时遇到问题
我对编码非常陌生,我在 pygame 中的动画遇到了一些问题,我只能看到静止图像,而不是移动。
import pygame
import os
pygame.init()
Stationary = pygame.image.load(os.path.join("Assets","stationary.png"))
left = [pygame.image.load(os.path.join("Assets","L1.png")),pygame.image.load(os.path.join("Assets","L2.png")),"L3.png")),"L4.png"))
]
right = [pygame.image.load(os.path.join("Assets","R1.png")),"R2.png")),"R3.png")),"R4.png"))
]
WIDTH,HEIGHT = 500,300
screen = pygame.display.set_mode((WIDTH,HEIGHT))
BLACK = 0,0
vel = 0.5
stepIndex = 0
clock = pygame.time.Clock()
FPS = 60
move_left = False
move_right = False
x = 50
y = 250
def draw_screen():
global stepIndex
screen.fill(BLACK)
if stepIndex >= 8:
stepIndex = 0
if move_left:
screen.blit(left[stepIndex//2],(x,y))
elif move_right:
screen.blit(right[stepIndex//2],y))
else:
screen.blit(Stationary,y))
running = True
while running:
clock.tick(FPS)
draw_screen()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys_pressed = pygame.key.get_pressed()
if keys_pressed[pygame.K_d]:
x += vel
move_right = True
move_left = False
elif keys_pressed[pygame.K_a]:
x -= vel
move_right = False
move_left = True
else:
move_left = False
move_right = False
stepIndex = 0
pygame.display.update()
pygame.quit()
我已经盯着我的代码看了大约 2 个小时,并在互联网上搜索解决方案并得到了这个,我什至查看了我的谷歌搜索的第二页只是为了找到我的问题的一些答案。
解决方法
您错过了递增 stepIndex
:
def draw_screen():
global stepIndex
screen.fill(BLACK)
stepIndex += 1 # <--- this is missing
if stepIndex >= 8:
stepIndex = 0
if move_left:
screen.blit(left[stepIndex//2],(x,y))
elif move_right:
screen.blit(right[stepIndex//2],y))
else:
screen.blit(Stationary,y))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。