I am making a tilemap platformer with pygame ad I defined the X and Y movement for the player and also a Y force that represents the gravity force to make it jump correctly. However, when my player falls off to the ground and the game begins, it doesn't move correctly neither to the right nor to the left direction due to the gravity force that keeps pushing him to the down. In addition to that, when the player jumps and i move it to the right, it's like it doesn't recognize the collision with the bottom rect of the wall (see gif below for a visual explanation of that).
I tried disabling that gravity force when the player hits the ground and enable it when the player jumps but it didn't work as expected and I've got problems managing collisions (also, it doesn't make sense to disable gravity, right?)
Here's my Player class:
class Player(pg.sprite.Sprite): def init(self, game, x, y): self.groups = game.sprites pg.sprite.Sprite.init(self, self.groups)
self.game = game self.image = pg.Surface((TILESIZE, TILESIZE)) self.image.fill(GREEN) self.rect = self.image.get_rect() self.move_left = self.move_right = False self.pos = pg.math.Vector2(x, y) self.acc = pg.math.Vector2(0, GRAVITY) self.vel = pg.math.Vector2(0, 0) def jump(self): self.vel.y -= 3 def update(self): self.vel.x = 0 if self.move_left: self.vel.x = -PLAYER_SPEED if self.move_right: self.vel.x = PLAYER_SPEED self.vel += self.acc self.pos += self.vel self.rect.x = self.pos.x * TILESIZE self.rect.y = self.pos.y * TILESIZE if self.vel.y > 0.5: self.vel.y = 0.5 hits = pg.sprite.spritecollide(self, self.game.walls, False) if hits: if self.vel.x > 0: self.pos -= self.vel self.rect.right = hits.rect.left self.vel.x = 0 if self.vel.x < 0: self.pos -= self.vel self.rect.left = hits.rect.right self.vel.x = 0 hits = pg.sprite.spritecollide(self, self.game.walls, False) if hits: if self.vel.y > 0: self.pos -= self.vel self.rect.bottom = hits.rect.top self.vel.y = 0 if self.vel.y < 0: self.pos -= self.vel self.rect.top = hits.rect.bottom self.vel.y = 0