Python Forum
How to change the player image, when moving
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to change the player image, when moving
#1
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:
        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()
The player starts out with the self.player_img.
There is a class for the player, which looks like this:

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)
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.
Reply
#2
Im assuming get_keys function is called every frame in your main game loop? And you blit self.image as the player?

Can you show the rest of your code?
Recommended Tutorials:
Reply
#3
(Feb-25-2020, 05:18 PM)metulburr Wrote: And you blit self.image as the player?

No. I am using Tiled Map Editor to make the game map. I load it with pytmx.
The loading of the player looks like this:

def new(self):
    self.all_sprites = pg.sprite.Group()
    self.player = pg.sprite.Sprite()
    for tile_object in self.map.tmxdata.objects:
            if tile_object.name == 'player':
                self.player = Player(self, tile_object.x, tile_object.y)
(Feb-25-2020, 05:18 PM)metulburr Wrote: Im assuming get_keys function is called every frame in your main game loop?
Can you show the rest of your code?

Well...the class "Player" has a get_keys() section and an update section:
def update(self):
        self.get_keys()
I just noticed two lines of code below that:
self.rot = (self.rot + self.rot_speed * self.game.dt) % 360
self.image = pg.transform.rotate(self.game.player_img, self.rot)
I just hashtaged these and now my player image is changing!

Smile
Thanks anyways,

Piethon
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  moving image with blit rwahdan 2 3,030 Jul-10-2019, 06:24 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020