Home > Backend Development > Python Tutorial > pygame animation sprite sheet

pygame animation sprite sheet

WBOY
Release: 2024-02-12 08:12:04
forward
704 people have browsed it

pygame animation sprite sheet

Question content

I want to create a top-down rpg in pygame using sprite sheets.

For example, I want to be able to press the spacebar to attack, which will trigger the attack animation and then return to normal

import pygame
from pygame.locals import *

pygame.init()

image = pygame.image.load("sprite_sheet.png")

clock = pygame.time.Clock()

screen =  pygame.display.set_mode((400, 250))

class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()

        self.current_animation = 0
        self.max_animation = 5

        self.animation_cooldown = 150
        self.last_animation = pygame.time.get_ticks()

        self.status = {"prev": "standing",
                       "now": "standing"}

    def animate_attack(self):
        time_now = pygame.time.get_ticks()

        if time_now - self.last_animation >= self.animation_cooldown:
            self.last_animation = pygame.time.get_ticks()
            if self.current_animation == self.max_animation:
                self.current_animation = 0

                joshua.status["now"] = joshua.status["prev"]
            else:
                self.current_animation += 1


joshua = Player()

while True:
    screen.fill(0)
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                joshua.status["prev"] = joshua.status["now"]
                joshua.status["now"] = "attacking"

    if joshua.status["now"] == "attacking":
        joshua.animate_attack()

    screen.blit(image, (0, 0), (joshua.current_animation * 64, 0, 64, 64))

    pygame.display.flip()

    clock.tick(60)
Copy after login

The code above is what I have. If I press the spacebar once, it goes through the animation and stops, but if I press the spacebar twice, it loops because of how it's programmed.

Need some help with animation, thanks


Correct answer


The problem is caused by the following call when space is pressed for the second time:

joshua.status["prev"] = joshua.status["now"]
Copy after login

This will set the "Previous" and "Now" status to "Attack". As a result, when the state is reset in the animate_attack() method, It will remain in "Attack" status:

joshua.status["now"] = joshua.status["prev"]
Copy after login

As a quick fix, make sure you only change the status if it has not been set yet:

if event.key == pygame.k_space:
    if not joshua.status["now"] == "attacking":
        joshua.status["prev"] = joshua.status["now"]
        joshua.status["now"] = "attacking"
Copy after login

As a better fix, you should encapsulate the state, This way only the player class can handle its own state, for example:

class player():
    def __init__(self):
        self.current_animation = 0
        self.max_animation = 5
        self.animation_cooldown = 150
        self.last_animation = pygame.time.get_ticks()
        self.status = "standing" # simplified state

    def attack(self):
        self.status = "attacking"

    def animate_attack(self):
        if self.status == "attacking":
            time_now = pygame.time.get_ticks()
            if time_now - self.last_animation >= self.animation_cooldown:
                self.last_animation = pygame.time.get_ticks()
                if self.current_animation == self.max_animation:
                    self.current_animation = 0
                    self.status = "standing" # reset state
                else:
                    self.current_animation += 1
Copy after login

In this way, there is no need to know any state outside the class:

while True:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                joshua.attack()

    joshua.animate_attack()

    screen.fill(0)
    screen.blit(image, (0, 0), (joshua.current_animation * 64, 0, 64, 64))
    pygame.display.flip()
    clock.tick(60)
Copy after login

The above is the detailed content of pygame animation sprite sheet. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template