pygame animation sprite sheet
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)
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"]
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"]
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"
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
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)
The above is the detailed content of pygame animation sprite sheet. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...
