# How to coordinate sprites, player's position and collisions in a Bomberman-like 2D game?

I'm programming a Bomberman in Java (my first game) and I'm having problems with the player's sprites and position in the map. I'm using a 2D integer array to represent:

• Unbreakable blocks
• Explodable brick walls
• Free spaces
• Player itself (not sure if this is correct)

By now I could correctly place every background tile (40*40 each) iterating the array. Bomberman's sprites are MAX 40*40 (sometimes a little smaller). My question is, how can I correctly handle the collisions with walls? I already tried to do it, but that solution was very horribly buggy.

Thanks in advance.

• You should expand your question by showing what you have already tried. Otherwise, for example, someone might try to give you a solution, only to give you the same thing that you already did, which didn't work for your game for whatever reasons. Also, it isn't very clear what you are asking for specifically - collision detection, code design, collision response? – TravisG Aug 4 '11 at 21:01

## 2 Answers

I'd store the positions of items as integers as you are doing now in a array. Testing for a collision with a wall could be as simple as testing the map array for the wall piece, for example:

if(map[player_x+1][player_y]!=WALL)
{
// player can move right
}


If you need certain sprites to move between tiles smoothly such as bomberman himself or sliding bombs you could store an additional offset as a float.

• Don't forget bounds checking! – David Titarenco Aug 5 '11 at 6:39

I made an attempt to answer your question on your original post, on Stack Overflow:

If you are simply using the array to represent a game grid (so that the player can not be inbetween blocks other than for animation purposes), then it would be extremely efficient to simply check the space the player wants to move to (can only be 4 spaces, up, left, right, or down), see if they are passable terrain, and then allow the player to move there.

This is how I am imagining your board to look, please let me know if this is incorrect:

W W W
O P O
W O W


(A 3 by 3 visualization of the array; W represents a wall, O represents open space, and P represents the player)

for this example I'm going to assume that the top-left corner is represented by locationArray[0][0] and the bottom-right is locationArray[2][2]. X is the first array parameter, Y is the second.

The player is at locationArray[1][1]

In the above case, if the player opted to move left, you would check the array location to his left, locationArray[2][1], find it open, and allow the player to move.

If the player opted to move up, locationArray[1][0], the check would not allow the player to move, as it is impassable terrain (a wall).