I have been trying to code a small game with a character that moves around. For some reason, I can't use gifs. After that, I turned the gif into a bunch of pngs and with that, I tried to write some code to animate it:
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
In this line
clock.schedule_interval(animate(), 0.5)
clock.schedule_interval
takes in a callable(a function) and the interval. By adding parentheses after animate
, you're actually calling the function and passing its output. Since animate
doesn't return anything(None
), you're doing the same as this:
clock.schedule_interval(None, 0.5)
where it should be
clock.schedule_interval(animate, 0.5)