简体   繁体   English

Pygame 零播放器 Animation 代码不工作

[英]Pygame Zero Player Animation Code Is Not Working

I have been trying to code a small game with a character that moves around.我一直在尝试编写一个带有四处移动角色的小游戏。 For some reason, I can't use gifs.出于某种原因,我不能使用 gif。 After that, I turned the gif into a bunch of pngs and with that, I tried to write some code to animate it:在那之后,我把 gif 变成了一堆 png,然后我试着写一些代码来动画它:

def animate():
    global frame
    curr_img = "player-walk" + str(frame)
    player.image = curr_img
    frame += 1
    if frame > 2:
        frame = 1

When I tried this code, I would get an error as soon as I called it.当我尝试这段代码时,我一调用它就会得到一个错误。 Here is the full code that I wrote:这是我写的完整代码:

import pgzrun as pgzero
import pygame

player = Actor("player")
frame = 1

WIDTH = 660
HEIGHT = 450

def move_player():
    if keyboard.w:
        player.y -= 2
        clock.schedule_interval(animate(), 0.5)
    if keyboard.s:
        player.y += 2
    if keyboard.a:
        player.x -= 2
    if keyboard.d:
        player.x += 2

def animate():
    global frame
    curr_img = "player-walk" + str(frame)
    player.image = curr_img
    frame += 1
    if frame > 2:
        frame = 1

def draw():
    screen.clear()
    player.draw()

def update():
    move_player()

pgzero.go()

Error is in this link: mystb.in/LitFillEquilibrium.sql此链接中有错误:mystb.in/LitFillEquilibrium.sql

In this line在这一行

clock.schedule_interval(animate(), 0.5)

clock.schedule_interval takes in a callable(a function) and the interval. clock.schedule_interval接受一个可调用的(一个函数)和间隔。 By adding parentheses after animate , you're actually calling the function and passing its output.通过在animate之后添加括号,您实际上是在调用 function 并传递其 output。 Since animate doesn't return anything( None ), you're doing the same as this:由于animate不返回任何内容( None ),因此您正在执行与此相同的操作:

clock.schedule_interval(None, 0.5)

where it should be它应该在哪里

clock.schedule_interval(animate, 0.5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM