Feb-25-2020, 10:55 AM
Hello.
I am working on a little game.
I want the player image to change, when you press w or a or s or d.
But somehow the image of the player is not changing.
This is what I use to load the different player images:
The player starts out with the self.player_img.
There is a class for the player, which looks like this:
Although there is the self.image = self.game.player_img_1, it doesn't change the image, when pressing the left A. How can I fix this?
Thanks for your help.
I am working on a little game.
I want the player image to change, when you press w or a or s or d.
But somehow the image of the player is not changing.
This is what I use to load the different player images:
1 2 3 4 5 |
self .player_img = pg.image.load(path.join(player_folder, PLAYER_IMG_0)).convert_alpha() self .player_img_1 = pg.image.load(path.join(player_folder, PLAYER_IMG_1)).convert_alpha() self .player_img_2 = pg.image.load(path.join(player_folder, PLAYER_IMG_2)).convert_alpha() self .player_img_3 = pg.image.load(path.join(player_folder, PLAYER_IMG_3)).convert_alpha() self .player_img_4 = pg.image.load(path.join(player_folder, PLAYER_IMG_4)).convert_alpha() |
There is a class for the player, which looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
class Player(pg.sprite.Sprite): def __init__( self , game, x, y): self .groups = game.all_sprites pg.sprite.Sprite.__init__( self , self .groups) self .game = game self .image = game.player_img self .image_1 = game.player_img_1 self .image_2 = game.player_img_2 self .image_3 = game.player_img_3 self .image_4 = game.player_img_4 self .rect = self .image.get_rect() self .rect.center = (x, y) self .hit_rect = PLAYER_HIT_RECT self .hit_rect.center = self .rect.center self .vel = vec( 0 , 0 ) self .pos = vec(x, y) def get_keys( self ): self .vel = vec( 0 , 0 ) keys = pg.key.get_pressed() if keys[pg.K_LEFT] or keys[pg.K_a]: self .image = self .game.player_img_1 self .vel = vec( - PLAYER_SPEED / 2 , 0 ).rotate( - self .rot) |
Thanks for your help.