Python Can';不要在pygame中制作行走动画

Python Can';不要在pygame中制作行走动画,python,pygame,Python,Pygame,我试图用pygame编写一个游戏,但当我试图制作一个行走动画时,它只显示了一个精灵 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

我试图用pygame编写一个游戏,但当我试图制作一个行走动画时,它只显示了一个精灵

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()

当我尝试使用它时,它只显示一个精灵,而不是角色的另一个精灵。我想让它每1或2秒旋转一次,让它看起来像是在走路。非常感谢您的帮助。

首先,我强烈建议使用类,以避免使用全局变量

其次,当您将当前时间设置回现在(0)时,您将使其成为当前时间-游戏循环。动画计时器将始终为负数。这将阻止语句运行

现在,我建议从代码中完全删除“if(current_time-game_loop.animation_timer)>200:”

下面是一个让您开始的示例(显然,您必须修改它以使其适合您)


正如@Red Twoon所建议的,在课堂上把东西分开是一个很好的做法。您应该做的另一件事是不要直接依赖于get_ticks,而是使用某种与时间无关的运动/动画。 您可以在游戏循环中使用增量时间来实现这一点

游戏循环。
好了,现在当我移除它时,它根本没有改变精灵
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]
while(True):
    delta = #Get the delta time.
    handle_events();
    update(delta);
    draw(delta);

#Animation stuff.
time_to_animate_frame = 200;
time_since_last_update = 0;
...

time_since_last_update += delta;
if(time_since_last_update > time_to_animate_frame):
    time_since_last_update - time_to_animate_frame;
    #Do the animation.....