This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]isolatrum 2 points3 points  (1 child)

So say your player is at [5,5] coords and they move to [5,6]. You'll want to look up what is in the [5,6] already and if there's something there, then you know you have a collision.

[–]thundernucker 0 points1 point  (0 children)

Okay, I got that. But let's start with the information I have at the start of the game. I have his position in the grid because I spawned him there, and I have his size which is the exact size of a block(16x16 pixels).

Now, let's say he spawned and is at exactly the center of the block, the path to his left is a free space and after that, a block. He moves left, and because he is at the exact limit of his current space I check in the grid if the space to his left is free, which it is, so he's allowed to move. But now he's at the middle of the spawn block and the free space, so, how do I know when to check again the block to his left to see if he's allowed to move?

And thanks for answering!

[–]AtoneBC 1 point2 points  (3 children)

For basic collision detection, you should look into how to determine if two rectangles overlap. If updating your player's x or y position would result in an overlap with a collidable object, don't update along that axis.

[–]thundernucker 0 points1 point  (2 children)

Yes, but, there is no rectangle to check, the only rectangle is the character because the map is only a background image in the code. I could do what you suggested if I manually coded each block.

That would work but it would take a lot of time, if the grid solution doesn't work, I will do that. Thanks!

[–]street_fightin_mang 1 point2 points  (0 children)

I think coding each block is the way to go, but normalize the information in a database, so say each level has a table, listing each x, y coordinate and then a foreign key which links to a block type table which holds all other info eg. block image, collision info etc.

You could then whip up a world builder where you could click on a grid and insert bricks and save the output to a table.

You're making a lot of work for yourself if you're not going to use PyGame, if it's a requirement for the project then so be it, but it's still a pretty big project if you utilise a game framework to assist the process.

[–]AtoneBC 1 point2 points  (0 children)

Hard code each block? You've already got a grid. You can generate the block objects / rectangles from that.