Greetings im new to pygame and to programming in general, and im trying to make a game which is a part of my school project. Im currently stuck in trying to get my character to change animation when moving, and this is the closest i have gotten to it
from sys import exit
pygame.init()
# Screen, game icon, title
screen = pygame.display.set_mode((822, 548))
pygame.display.set_caption("CSS dungeon")
icon = pygame.image.load('images/gameicon.png')
pygame.display.set_icon(icon)
# Background
background = pygame.image.load('images/map.png')
# Character
characterImg = pygame.image.load('character/character.png')
animation = ['character/character.png', 'character/character#1.png', 'character/character.png', 'character/character#2.png']
character_x = 392
character_y = 420
charactermove_x = 0
charactermove_y = 0
counter = 0
def character(x,y):
screen.blit(characterImg,(x, y))
# task 1
task1_title = pygame.image.load('images/task#1.png')
task1 = pygame.transform.scale(task1_title, (70, 20))
# task 2
task2_title = pygame.image.load('images/task#2.png')
task2 = pygame.transform.scale(task2_title, (70, 20))
# task 3
task3_title = pygame.image.load('images/task#3.png')
task3 = pygame.transform.scale(task3_title, (70, 20))
# task 4
task4_title = pygame.image.load('images/task#4.png')
task4 = pygame.transform.scale(task4_title, (70, 20))
# boss fight
boss_title = pygame.image.load('images/boss.png')
boss = pygame.transform.scale(boss_title, (100,20))
# Control the fps
clock = pygame.time.Clock()
# Game loop
while True:
screen.blit(background, (0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
# movement keybinds
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
characterImg = pygame.image.load(animation[counter])
counter = (counter + 1) % len(animation)
charactermove_x = -4
if event.key == pygame.K_RIGHT:
characterImg = pygame.image.load(animation[counter])
counter = (counter + 1) % len(animation)
charactermove_x = 4
if event.key == pygame.K_UP:
characterImg = pygame.image.load(animation[counter])
counter = (counter + 1) % len(animation)
charactermove_y = -4
if event.key == pygame.K_DOWN:
characterImg = pygame.image.load(animation[counter])
counter = (counter + 1) % len(animation)
charactermove_y = 4
if event.key == pygame.K_a:
characterImg = pygame.image.load(animation[counter])
counter = (counter + 1) % len(animation)
charactermove_x = -4
if event.key == pygame.K_d:
characterImg = pygame.image.load(animation[counter])
counter = (counter + 1) % len(animation)
charactermove_x = 4
if event.key == pygame.K_w:
characterImg = pygame.image.load(animation[counter])
counter = (counter + 1) % len(animation)
charactermove_y = -4
if event.key == pygame.K_s:
characterImg = pygame.image.load(animation[counter])
counter = (counter + 1) % len(animation)
charactermove_y = 4
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
charactermove_x = 0
if event.key == pygame.K_UP:
charactermove_y = 0
if event.key == pygame.K_a:
charactermove_x = 0
if event.key == pygame.K_w:
charactermove_y = 0
if event.key == pygame.K_RIGHT:
charactermove_x = 0
if event.key == pygame.K_DOWN:
charactermove_y = 0
if event.key == pygame.K_d:
charactermove_x = 0
if event.key == pygame.K_s:
charactermove_y = 0
character_x += charactermove_x
character_y += charactermove_y
character(character_x, character_y)
# boundries
if character_x <=25:
character_x = 25
elif character_x >= 741:
character_x = 741
if character_y <=25:
character_y = 25
elif character_y >= 420:
character_y = 420
# coordinates
print("coordinates x:",character_x, "y:",character_y)
# tasks and boss fight
screen.blit(task1, (174,320))
screen.blit(task2, (630,300))
screen.blit(task3, (118,160))
screen.blit(task4, (701,67))
screen.blit(boss, (375,48))
pygame.display.update()
clock.tick(30)
currently a frame changes only whenever i click a key, and i cant find what seems to be the problem