# Player won't move correctly due to gravity force pushing him into the ground

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 a gif of the issue i'm facing with:

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[0].rect.left
self.vel.x = 0
if self.vel.x < 0:
self.pos -= self.vel
self.rect.left = hits[0].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[0].rect.top
self.vel.y = 0
if self.vel.y < 0:
self.pos -= self.vel
self.rect.top = hits[0].rect.bottom
self.vel.y = 0


You seem to assume the player will only collide with walls when it is moving to the side. I think this assumption causes the shaky sliding as the player also collides with the floor whenever it walks. You may want to rename your game.walls to something like game.obstacles to reduce the chance of confusing yourself. ツ
• self.pos -= self.vel moves the player back, even if it only collides with the floor. – 魔大农 May 6 '20 at 2:16