1

So I'm working on a little christmas game, and I have 2 sprites for my character, and I want these two sprites to make a character animation while the game is running. Sprites are called "santa1" and "santa2", here is my code:

import pygame

pygame.init()

pygame.display.set_caption("Santa Dash!")
win = pygame.display.set_mode((1200, 800))
santa = pygame.image.load("santa1.png").convert_alpha()
map = pygame.image.load("santadashmap.png").convert_alpha()
x = 100
y = 500
vel = 1

#Gameloop
run = True
while run:

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        run = False

keys = pygame.key.get_pressed()

if keys[pygame.K_w]:
    y -= vel
if keys[pygame.K_s]:
    y += vel
if keys[pygame.K_a]:
    x -= vel
if keys[pygame.K_d]:
    x += vel

win.blit(map, (0, 0))
win.blit(santa, (x, y))
pygame.display.update()

Any help is greatly appreciated, and I'll be happy to give more information if you'd need it.

//Qmobss

Qmobss
  • 11
  • 5

1 Answers1

0

You can add a sprite switching mechanism. Every iteration, the sprite is changed to create a sprite animation. I added a sprite switching mechanism to your code.

import pygame

pygame.init()

pygame.display.set_caption("Santa Dash!")
win = pygame.display.set_mode((1200, 800))
santa = pygame.image.load("santa1.png").convert_alpha()
map = pygame.image.load("santadashmap.png").convert_alpha()
x = 100
y = 500
vel = 1

currentSantaFrame = "santa1.png"

#Gameloop
run = True
while run:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

        keys = pygame.key.get_pressed()

        if keys[pygame.K_w]:
            y -= vel
        if keys[pygame.K_s]:
            y += vel
        if keys[pygame.K_a]:
            x -= vel
        if keys[pygame.K_d]:
            x += vel

    santa = pygame.image.load(currentSantaFrame).convert_alpha()

    win.blit(map, (0, 0))
    win.blit(santa, (x, y))

    if (currentSantaFrame == "santa1.png"):
        currentSantaFrame = "santa2.png"

    else:
        currentSantaFrame = "santa1.png"

    pygame.display.update()

I have not tried this code, so if there is anything wrong with it, please correct me.

LeoTheDev
  • 60
  • 1
  • 7
  • Thank you! I ended up doing something almost exactly like this, except I added a pygame.time.Clock() and set it to 120 ticks to slow the animation down a little. Thank you for answering! – Qmobss Dec 01 '18 at 15:22