# [Python/Pygame] Side scroller collision question

This topic is 2263 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

/edit:
I actually figured out why I am getting wonky behavior just after posting this. God, I love print statements. Apparently there's an issue with the way I am instantiating tile positions, however, I can't seem to find said issue in my modules. Looks like I need to do s'more digging.
/end edit

Hello!

I am currently working on a side-scroller game in Python/Pygame and had a few questions.

I have a very basic physics system set up for levels. I know there are many routes to take when figuring out a collision algorithm in a tile-based map, however, I can't seem to find one that is efficient, elegant, and actually works right.

Here's the player's update function.

 class Player(object): height = 48 def update(self, level, p): if self.velocity.x != 0 and self.velocity.y != 0: self.current_tile = level.grid[int((p.x + Tile.width / 2) / Tile.width)][int(p.y / Tile.height)] self.pos.x += self.velocity.x self.pos.y += self.velocity.y self.velocity.y += level.gravity self.velocity.x *= 0.9 if abs(self.velocity.x) < 0.01: self.velocity.x = 0 # if self.current_tile != None and self.current_tile.collides: # ground_level = self.current_tile.pos.y # else: # ground_level = 500 ground_level = 500 if self.pos.y > ground_level: self.pos.y = ground_level self.velocity.y = 0

Keep in mind this function is not finished yet. (Also, the code block formatting on this forum is messed up; in my module the scopes are normal)

Now, in each level, I have a 2D list of internal Tile objects, each containing a 'collision' flag.

I am trying to figure out vertical collision when falling at this moment.

The player's transformed position in the level (the given p) is converted to a tile in the first two lines of this update method. (Edge cases not taken into consideration yet, but not too worried about that at this point).

In the commented out lines are where I have tried a couple different things. I tried just setting the velocity.y of the player to 0. That changed the behavior, but did not work so well. The current solution you see here does not work at all.

Should I be making some sort of collision map out of level.grid, instead of using flags like this? What am I doing wrong here (or just not doing)?

Any help is appreciated, thanks

##### Share on other sites
Figured it out. I made a rookie mistake of using the player's position in relation to the screen, not in relation to level coordinates. LOL. I also re-arranged the order of operations within the player's update method.

I'll paste the code just in case anybody want to see it.

def update(self, level): if self.velocity.x != 0 and self.velocity.y != 0: self.current_tile = level.grid[ int((self.pos.x + Player.size.w / 2) / Tile.width)][ int((self.pos.y + Player.size.h) / Tile.height)] self.pos.x += self.velocity.x self.pos.y += self.velocity.y ground_level = 500 if self.current_tile != None and self.current_tile.collides: ground_level = self.current_tile.pos.y - Player.size.h self.velocity.x *= 0.9 if abs(self.velocity.x) < 0.01: self.velocity.x = 0 if self.pos.y > ground_level: self.pos.y = ground_level self.velocity.y = 0 self.velocity.y += level.gravity

So, now collision works when falling and I can actually run and jump through my levels as intended.

Next up, ceiling and wall collision!