Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
1  
Please don't ask the same question twice on two different SE sites. –  Cyclops Jan 30 '12 at 13:31
    
if I was to get a decent answer then i wouldnt have to. –  Richard Gibbons Jan 31 '12 at 16:30
add comment

2 Answers

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.

share|improve this answer
    
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. –  Clockwork-Muse Feb 1 '12 at 19:05
add comment

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).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.