I have a problem with my pygame script in which the way I check for collisions doesn't seem to work with the x axis and presents some very frustrating and downright stupid errors. The way it works is I first check for movement in the axis of choice, here is an example in the y axis. I then get a list of tiles I collide with and check to see if there are any collisions and the tile's collision index is for the top of itself:
if self.velocity.y > 0:
collide_rect = pygame.sprite.spritecollide(self, self.tile_group, False)
if collide_rect and collide_rect[0].top_collide == True:
self.position.y = collide_rect[0].rect.top + 1
self.velocity.y = 0
The last two lines set the position of the player, set at the bottom left of it's rectangle, to the top of the tile + 1 pixel to avoid jittering. I then set the y velocity to 0. But here is the code I have for the x axis:
if self.velocity.x > 0:
collide_rect = pygame.sprite.spritecollide(self, self.tile_group, False)
if collide_rect and collide_rect[0].left_collide == True:
self.rect.right = collide_rect[0].rect.left
If I run this code and collide to the left of a tile, I stop for a moment and go through the tile. If I also set the x velocity to zero, the same thing happens. If I set the x velocity to -5, the player jitters inside of the tile and in the ground as well. And worst of all, if the tile has top and side collision, the player glitches out on top of it due to how the player is technically inside the tile to avoid the horrible jittering the collision causes otherwise, and ends up being shot to the left most area of the tile. This is most frustrating as I just want to make a platformer game in pygame, like Mario or Sonic, with basic collision that makes sense.
I've searched google and stack overflow for answers to my problem, but the most I found was a tutorial for collision in the y axis, which works for me just fine. The second best was saying basically just search up proper game design docs, which is the most unhelpful advice I have heard since that's why I'm here. (BTW, I use a 2D vector system for my movement) The expected result I wish to have is the player staying in place when running up against a wall without clipping into the ground or wall visibly. Here is the link to the file I'm using to create this.
