in

Why isn’t my image changing with the sprite?


I’ve made it so that the image for the Frisk sprite is determined by the player input via the arrow keys in player_input(self). Also, frisk_anim(self) is meant to animate the sprite. However, the sprite isn’t being animated and the image isn’t being changed via the player input. Any idea why this could be?

import pygame
pygame.init()
screen = pygame.display.set_mode((640,480))
pygame.display.set_caption("UNDERRUNE")
clock = pygame.time.Clock()
keys_pressed = pygame.key.get_pressed() #updates which keys are being pressed

class Frisk(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        friskd1 = pygame.transform.scale2x(pygame.image.load('assets/frisk_d1.png').convert_alpha())
        friskd2 = pygame.transform.scale2x(pygame.image.load('assets/frisk_d2.png').convert_alpha())
        friskd3 = pygame.transform.scale2x(pygame.image.load('assets/frisk_d3.png').convert_alpha())
        friskd4 = pygame.transform.scale2x(pygame.image.load('assets/frisk_d4.png').convert_alpha())
        self.frisk_d = [friskd1,friskd2,friskd3,friskd4]
        friskr1 = pygame.transform.scale2x(pygame.image.load('assets/frisk_r1.png').convert_alpha())
        friskr2 = pygame.image.load('assets/frisk_r2.png').convert_alpha()
        self.frisk_r = [friskr1,friskr2,friskr1,friskr2]
        frisku1 = pygame.transform.scale2x(pygame.image.load('assets/frisk_u1.png').convert_alpha())
        frisku2 = pygame.image.load('assets/frisk_u2.png').convert_alpha()
        frisku3 = pygame.image.load('assets/frisk_u3.png').convert_alpha()
        frisku4 = pygame.image.load('assets/frisk_u4.png').convert_alpha()
        self.frisk_u = [frisku1,frisku2,frisku3,frisku4]
        friskl1 = pygame.transform.scale2x(pygame.image.load('assets/frisk_l1.png').convert_alpha())
        friskl2 = pygame.image.load('assets/frisk_l2.png').convert_alpha()
        self.frisk_l = [friskl1,friskl2,friskl1,friskl2]
        self.frisk_index = 0
        self.walkframe = 0
        self.frisk_surf = [self.frisk_d[self.walkframe],self.frisk_r[self.walkframe],self.frisk_u[self.walkframe],self.frisk_l[self.walkframe]]
        self.image = self.frisk_surf

        self.image = self.frisk_surf[self.frisk_index]
        self.rect = self.image.get_rect(center = (320,240))
        self.frisk_movedown = False

        self.vel = 5

    def player_input(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_DOWN]:
            self.rect.y += self.vel
            self.frisk_surf = 0
        if keys[pygame.K_RIGHT]:
            self.rect.x += self.vel
            self.frisk_surf = 1
        if keys[pygame.K_UP]:
            self.rect.y -= self.vel
            self.frisk_surf = 2
        if keys[pygame.K_LEFT]:
            self.rect.x -= self.vel
            self.frisk_surf = 3
        if self.frisk_surf == 1:
            print("1")

    def frisk_anim(self):
        self.walkframe += 0.25
        if self.walkframe == 4:
            self.walkframe = 0

    def update(self):
        self.player_input()
        self.frisk_anim()

# GROUPS
frisk = pygame.sprite.GroupSingle() # group contains sprite
frisk.add(Frisk())

# GAME LOOP
while True:
    screen.fill((0,0,0))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()

    frisk.draw(screen)
    frisk.update()

    pygame.display.update() # updates the display surface
    clock.tick(60) # tells pygame that this while True loop shouldn't run faster then 60 fps



Source: https://stackoverflow.com/questions/70601409/why-isnt-my-image-changing-with-the-sprite

An implmentation for PriBank core algorithm in Go

Simple and customizable React UI components