我试图在 pygame 中编写游戏代码,但是当我尝试制作行走动画时,它只显示了一个 Sprite 。
def go_left(time):
ness_current = 1
global ness
global is_walking_left
ness_list = [ness_walking,ness_standing]
current_time = pygame.time.get_ticks()
go_left.walking_steps = 1
now = 0
cooldown = 1000
flag = 0
ness = ness_list[ness_current]
print current_time - game_loop.animation_timer
if (current_time - game_loop.animation_timer) > 200:
print 'Changing'
if ness_current == 0:
print 'Changing to sprite 1'
now = pygame.time.get_ticks()
ness_current = 1
current_time = now
elif ness_current == 1:
print 'Changing to sprite 0'
if (current_time - game_loop.animation_timer) > 200:
ness_current = 0
current_time = now
else:
'Changing to sprite 0 because of sprite reset'
ness_current = 0
current_time = now
def stop_it():
global ness
ness = pygame.image.load('nessthekid.png').convert()
ness.set_colorkey(WHITE)
car_list = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
player_list = pygame.sprite.Group()
当我尝试使用它时,它只显示一个 sprite 而不是另一个角色。我希望它每 1 或 2 秒切换一次,让它看起来像在走路。感谢您的帮助。
最佳答案
首先,我强烈建议使用类,以避免使用全局变量。
其次,当您将当前时间设置回现在 (0) 时,您将使它成为 current_time - game_loop.animation_timer 将始终为负数。这将阻止语句运行。
现在,我建议从您的代码中完全删除“if (current_time - game_loop.animation_timer) > 200:”。
这是一个让你入门的例子(显然你必须改变它才能让它为你工作)
class Ness:
def __init__(self):
self.all_images = [ness_walking,ness_standing]
self.current = 0
self.image = self.all_images[self.current]
def walk_left(self):
# Every 200 clicks
if pygame.time.get_ticks() % 200 == 0:
if self.current == 0:
self.current = 1
else:
self.current = 0
self.image = self.all_images[self.current]
关于python - 无法在pygame中制作行走动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31216872/