python - 无法在pygame中制作行走动画

标签 python pygame

我试图在 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/

相关文章:

python - 如何检测 Pygame 中的两个图像是否接触?

python - 使用 pyOpenSSL 签署大文件

python - 导入错误 : Environment variable DJANGO_SETTINGS_MODULE is undefined

python - 某些邮件通过 Sendgrid 发送电子邮件失败 - 无法获取 mx 信息 : failed to get IPs from PTR record: lookup <nil>: unrecognized address

python - 从单个 html 页面上的多个 django 应用程序模型获取数据

python - Mongoengine - 如何执行 "save new item or increment counter"操作?

python - PyGame:我的图像没有出现

python - 变色弹跳球

python - Python 3.6.2、Pygame 1.9.3 中的开始菜单错误

python - 我的暂停语句不适用于 pygame 循环