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
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!