Hello guys,
I have some progress in my pacman game.
But it's not working as expected yet.
I'm working on the pacman labyrinth.
This is what i have for check if the pacman hits the wall or not.
(not the entire code)
#define BLOCKSIZE 32 //size of my pacman sprite
#define MATRIX_WIDTH 640/BLOCKSIZE // 640/480 is the size of my screen
#define MATRIX_HEIGHT 480/BLOCKSIZE
int tabuleiro [MATRIX_WIDTH][MATRIX_HEIGHT]; //board
so i load a map from a file with 0, and 1 into the array
void loadMap(int number)
{
int x=0;
int y=0;
string line;
ifstream map;
switch(number)
{
case 1:
map.open("mapa1.txt");
while(map && y<MATRIX_HEIGHT)
{
getline(map,line);
while(x<MATRIX_WIDTH)
{
tabuleiro[x][y]=line[x]-'0';
x++;
}
y++;
x=0;
}
}
map.close();
}
i draw a wall in the screen from the position on the array
for(int x=0;x<MATRIX_WIDTH;x++)
for(int y=0;y<MATRIX_HEIGHT;y++)
if(tabuleiro[x][y] == 1)
{
Wall.SetPosition((float)(x*BLOCKSIZE),(float)(y*BLOCKSIZE));
App.Draw(Wall);
}
then i check if the pacman goes into a wall by a position of the pacman, and checking if the position in the array have a 1 (wall)
void movePac(int direction, float ElapsedTime)
{
// Desenhar image pac correspondente no ecrã
setImage(direction);
int x= (int)(pac.GetPosition().x/BLOCKSIZE +1);
int y= (int)(pac.GetPosition().y/BLOCKSIZE +1);
float x_x= (pac.GetPosition().x/BLOCKSIZE);
float y_y= (pac.GetPosition().y/BLOCKSIZE);
std::stringstream stream;
stream << "x:" << x_x << "-y:" << y_y << "Wall: " << tabuleiro[x][y];
string st;
stream>>st;
textoPontos.SetText(st);
textoPontos.SetPosition(300, 400);
switch (direction)
{
case UP:
if ((tabuleiro[x][y-1]!=WALL)) //
pac.Move(0, -MAX_SPEED * ElapsedTime);
break;
case DOWN:
if ((tabuleiro[x+1][y]!=WALL))
{
pac.Move(0, MAX_SPEED * ElapsedTime);
}
break;
case RIGHT:
if ((tabuleiro[x+1][y]!=WALL))
if (pac.GetPosition().x + 35 < 640)
pac.Move(MAX_SPEED * ElapsedTime, 0);
//pac.SetPosition(pac.GetPosition().x + 1, pac.GetPosition().y);
else
pac.SetPosition(0, pac.GetPosition().y);
break;
case LEFT:
if ((tabuleiro[x-1][y]!=WALL))
if (pac.GetPosition().x + 35 > 0)
pac.Move(-MAX_SPEED * ElapsedTime, 0);
else
pac.SetPosition(640, pac.GetPosition().y);
break;
}
}
what happens is that sometimes it detects a wall and stops, and sometimes he dont. And, sometimes he stops a position after the wall.
Well, maybe there is a better way to do it...
I appreciate your help