How to change the player image, when moving - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: How to change the player image, when moving (/thread-24650.html) |
How to change the player image, when moving - Piethon - Feb-25-2020 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. RE: How to change the player image, when moving - metulburr - Feb-25-2020 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? RE: How to change the player image, when moving - Piethon - Feb-26-2020 (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? 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! Thanks anyways, Piethon |