Need to overlap characters in 2D array.

So I'm creating a game in c++ language. I'm doing it in a simple 2D board where blocks move around in it using the key arrows. One important part of the game is that I need for blocks to get past each other and for that they need to overlap and then appear in the other side.
For example:
From this:
X X X X X X
X X X X X X
X 1 2 X X X
X X X X X X

And, if I move right, to this:
X X X X X X
X X X X X X
X X 1 X X X
X X X X X X

And then, right again, this:
X X X X X X
X X X X X X
X X 2 1 X X
X X X X X X

If you could help I would really appreciate it.
closed account (2UD8vCM9)
Does this make sense?
1
2
3
4
5
6
7
8
9
char Map[4][6] = {'X','X','X','X','X','X', 
			    'X','X','X','X','X','X',
			     'X','X','2','X','X','X',
			     'X','X','X','X','X','X'};
//First dimension = y values
//Second Dimension = x values
//So it can be thought of as Map[Y][X]
//Each time the map is redrawn, you will need to not draw the char
//that has the same X&Y as your character has. Instead of drawing that char, draw your character char 
Last edited on
closed account (L6b7X9L8)
Ah, I have come across this problem before. The solution I came to was this.


I created 'layers' of a grid, something like this..

 
int WorldSpace[3][20][20]; // 3 layers of a 20x20 grid 


The first layer was the ground layer, second layer was items, and the third was the player/monsters.

First I would draw the ground, then the items over it then the Player and NPC's. That way I would never have to overwrite values and I could build a system that would only affect each layer.

Hope that helps!
I'm sorry if I not made that clear but I do have a board consisting of a 2D array (m_arrBoard[row][col]) and I can move my blocks just fine. The problem is when one block needs to go past other it simply erases it and I don't know if there's a way to remember what block was there before and put it back there again. But thanks though.
Last edited on
Thanks Zorg that but I do have my whole game almost complete so I would have to change the whole code to use that... But that would definitely do the trick! If I all else fails that's what I will have to do.
closed account (L6b7X9L8)
It worked for me, I also had to change some code to implement this but it worked like a charm and I am glad I did it :)

You could also write an enums to control what is on the layers, something like this.

1
2
3
4
5
6
7
8
9
10
enum LAYER { GROUND, ITEMS, ENTITIES };

TileArray[GROUND][0][0] = '.'; // A grass tile
TileArray[ENTITIES][0][0] = '@'; // player

// Code here

if(TileArray[ENTITIES][0][0] != ' ') drawTile(TileArray[ENTITIES][0][0]); // Check if NPC first
else if(TileArray[ITEMS][0][0] != ' ') drawTile(TileArray[ITEMS][0][0]);  // If no NPC check items
else drawTile(TileArray[GROUND][0][0]); // No NPC's or items so draw the ground. 
Thanks I will try that!
Topic archived. No new replies allowed.