I am a beginner programmer practicing with Python. I am trying to make a simple game, what I have so far is just adding the character sprite onto the map. What I'm trying to do is when no keys are being pressed, that the character sprite continuously switches between two animations.
When running the game, it just loads up a black screen and the game freezes if you click on the screen.
CODE:
class Character(pygame.sprite.Sprite):
clock = pygame.time.Clock()
sprite_frame = 0
def __init__(self):
self.standing_right_frame1 = pygame.image.load("C:\\...\\character1_standing_facing_right_1.png")
self.standing_right_frame2 = pygame.image.load("C:\\...\\character1_standing_facing_right_2.png")
self.standing_right = [self.standing_right_frame1, self.standing_right_frame2]
self.rect1 = self.standing_right_frame1.get_rect()
self.rect2 = self.standing_right_frame2.get_rect()
self.rect1.center = (300, 200)
self.rect2.center = (300, 200)
def update(self):
game_running = True
self.sprite_frame = 0
while game_running:
if self.sprite_frame >= len(self.standing_right):
self.sprite_frame = 0
self.character_sprite = self.standing_right[self.sprite_frame]
self.sprite_frame = self.sprite_frame + 1
def draw(self, display):
self.game_screen = display
self.game_screen.blit(self.character_sprite, self.rect)
pygame.init()
character = Character()
#GAME COLORS:
color_black = pygame.Color(0, 0, 0)
color_white = pygame.Color(255, 255, 255)
color_light_grey = pygame.Color(212, 212, 212)
color_grey = pygame.Color(150, 150, 150)
color_dark_grey = pygame.Color(100, 100, 100)
color_dark_green = pygame.Color(41, 169, 48)
#GAME INITIALIZATION
pygame.display.set_caption("My First Game")
screen_width = 800
screen_height = 600
game_screen = pygame.display.set_mode((800, 600))
game_screen.fill(color_white)
game_clock = pygame.time.Clock()
game_clock.tick(60)
game_running = True
# MAIN GAME LOOP
while game_running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
else:
character.update()
character.draw(game_screen)
You must do the update in the application loop and not in the event loop. The application loop is executed once per frame, the event loop is executed only when an event occurs. Also clear the display and update the display every frame and limit the frames per second with pygame.time.Clock.tick
.
And very importantly, don't try to animate anything with a loop inside the application loop. You don't need this inner loop at all, because the application loop runs continuously.
Also see How can I make a sprite move when key is held down and use pygame.key.get_pressed()
to detect if any
key is hold down:
class Character(pygame.sprite.Sprite):
def __init__(self):
# [...]
self.rect = self.rect1
self.character_sprite = self.standing_right_frame1
def update(self):
sprite_index = self.sprite_frame // 30
if sprite_index >= len(self.standing_right):
self.sprite_frame = 0
sprite_index = 0
self.character_sprite = self.standing_right[sprite_index]
self.sprite_frame = self.sprite_frame + 1
clock = pygame.time.Clock()
while game_running:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_running = False
keys = pygame.key.get_pressed()
if any(keys):
character.update()
game_screen.fill(color_white)
character.draw(game_screen)
pygame.display.flip()
pygame.quit()
Additionally I recommend the reading Animated sprite from few images and How do I create animated sprites using Sprite Sheets in Pygame?