I'm creating a 2D maze game in Python.

When I collide with the walls, the user can go through them but I want to be able to block him from doing that. How do I do this? Would I use an array?

I have created an outer wall before in another game, but that was just setting the bounds in the user class.

  • 2
    \$\begingroup\$ Please don't ask the same question twice on two different SE sites. \$\endgroup\$
    – Cyclops
    Commented Jan 30, 2012 at 13:31
  • \$\begingroup\$ if I was to get a decent answer then i wouldnt have to. \$\endgroup\$ Commented Jan 31, 2012 at 16:30

2 Answers 2


I assume from the picture you posted on StackOverflow that your world is tile-based, and you're after something like this. Then you can probably use the same data structure that you're already using for storing the map and rendering, when doing the collision detection in your game.

The first question I have to ask is, how does your character move - does it "teleport" between tiles, or does it move continuously in the world? The answer depends on that piece of information.

Player Teleports between Tiles

In the first case, it's extremely simple to deal with collisions. Whenever you're trying to move the character, just check if the block that he'll be moving into is a wall, and abort the move if that's the case.

Player Moves Continously

In the second case, it's a bit more complicated, but I already explained how to do this before in another question. There's a source code example in that answer, but it's written for C# and XNA so you'll have to adapt. The idea is language agnostic though:

  1. Move character in one dimension (e.g. horizontally).
  2. After the move, find all tiles that intersect the character's bounding box.
  3. For each of them, detect collisions with walls and resolve them by moving the player back by the penetration amount.
  4. Repeat the process for the other dimension (e.g. vertically).

Note: I found that doing this process separatedly for horizontal and vertical movement helped guard against a series of edge case problems, hence my suggestion.

  • \$\begingroup\$ I think for your note on number 4 you mean 'serially', not 'seperatedly', which would still imply being able to use multiple threads for the calculation. \$\endgroup\$ Commented Feb 1, 2012 at 19:05

I think that this is not only Python specific problem. You can create some bounding boxes around walls and than compute collisions with them = if user collide, shift him outside the box.

You can also use some kind of 2D array (texture with information, where user can go and where not).


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .