Reputation: 29
I'm coding a game in python 3 (with pygame) for a school project, but i'm struggeling with something: I'm trying to get my sprite to have an animation when moving, but it only shows a static image: when i press the left key, the sprite's image changes to a static image instead of an animation made of 3 png's. Here is that part of the code:
clock = pygame.time.Clock()
walkRight = [pygame.image.load('ressources/images/R1.png'), pygame.image.load('ressources/images/R2.png'), pygame.image.load('ressources/images/R3.png')]
walkLeft = [pygame.image.load('ressources/images/L1.png'), pygame.image.load('ressources/images/L2.png'), pygame.image.load('ressources/images/L3.png')]
walkUp = [pygame.image.load('ressources/images/U1.png'), pygame.image.load('ressources/images/U2.png'), pygame.image.load('ressources/images/U3.png')]
walkDown = [pygame.image.load('ressources/images/D1.png'), pygame.image.load('ressources/images/D2.png'), pygame.image.load('ressources/images/D3.png')]
#this is the player sprite
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = (walkDown[4//3])
self.rect = self.image.get_rect()
self.rect.centerx = width / 2
self.rect.bottom = height / 2
self.speedx = 0
self.speedy = 0
self.velocity = 2
def update(self):
self.speedx = 0
self.speedy = 0
left = False
right = False
up = False
down = False
run = False
walkCount = 4
runCount = 4
clock.tick(FPS)
keys = pygame.key.get_pressed()
#I believe this is the part that has something wrong!
if walkCount + 1 >= 9:
walkCount = 0
if (down == True and run == False):
walkCount += 1
self.image = (walkDown[walkCount//3])
elif (up == True and run == False):
walkCount += 1
self.image = (walkUp[walkCount//3])
elif (right == True and run == False):
walkCount += 1
self.image = (walkRight[walkCount//3])
elif (left == True and run == False):
walkCount += 1
self.image = (walkLeft[walkCount//3])
Upvotes: 1
Views: 127
Reputation: 210878
walkCount
is a local variable in the method update
. It is initialized by 4
every time when update
is called. So the value of walkCount
is the same at the start of the function, every time. This causes that the image seems to be static.
walkCount
has to be an attribute of the class Player
rather than a local variable:
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = (walkDown[4//3])
self.rect = self.image.get_rect()
self.rect.centerx = width / 2
self.rect.bottom = height / 2
self.speedx = 0
self.speedy = 0
self.velocity = 2
self.walkCount = 4 # <---- add attribute
def update(self):
# [...]
# walkCount = 4 <------ delete local variable
keys = pygame.key.get_pressed()
down = keys[pygame.K_DOWN]
up = keys[pygame.K_UP]
right = keys[pygame.K_RIGHT]
left = keys[pygame.K_LEFT]
run = False
if self.walkCount + 1 >= 9:
self.walkCount = 0
if (down == True and run == False):
self.walkCount += 1
self.image = (walkDown[self.walkCount//3])
elif (up == True and run == False):
self.walkCount += 1
self.image = (walkUp[self.walkCount//3])
elif (right == True and run == False):
self.walkCount += 1
self.image = (walkRight[self.walkCount//3])
elif (left == True and run == False):
self.walkCount += 1
self.image = (walkLeft[self.walkCount//3])
Upvotes: 1