SFML community forums

Help => General => Topic started by: gl0w on June 28, 2011, 08:50:48 pm

Title: Pac man game
Post by: gl0w on June 28, 2011, 08:50:48 pm
Hello all,

Well, i need some help.

I'm starting to create a pac-man game. i have an array int board [640][480];, that will contain ,1,2,3 where 1 is a wall, 2 is empty space, and 3 is something to eat.

I have managed to insert 1 inside the array, in some specific positions, then i read then i pass them as a x, and y to draw sprites (walls) on the screen.

And now, i'm lost, i dont know how to check pacman collision with a wall or not. Should i check it trough the array, or on the design (screen).

Or i'm doing it completely wrong. And, i need some advices. Like, how to start building the game the best way for a beginner.


thanks in advance :)
Title: Pac man game
Post by: Fouf on June 28, 2011, 09:08:59 pm
Just check the piece in the array if pac man is allowed to move there, and let pac man move if it is allowed.
Title: Pac man game
Post by: slotdev on June 28, 2011, 09:10:30 pm
Check the stuff in the array.

What is on screen is just a graphical representation of the values in the array. Forget about fancy graphics, and get the game logic sorted out first.

So, you want to check what's around Pac Man. You need to know his position (X and Y) in the array, and from this you can work out what's around him with some simple maths - think about it for a while.

I would also google for some tutorials on game logic, and maybe even some example code, I am sure there is loads out there!

Ed
Title: Pac man game
Post by: MorleyDev on June 28, 2011, 09:17:24 pm
A good rule of thumb is logic and rendering should be entirely separate in the code. Logic should never read from or write to the screen and Drawing should never write to anything but the screen.

If you're breaking this rule of thumb you're usually doing something wrong, writing debug code which most likely won't be enabled in the final product, or making some very low level optimisations to crank out which are probably not needed unless you're working on pre-1995 technology

So for Pac-Man, read from the array.
Title: Pac man game
Post by: gl0w on June 29, 2011, 02:54:17 pm
Thanks for the reply. :)

Well, i think i understand what i need to do with the array.
But, i have a few more questions.

The size of the array should be equal the size of my screen, right? Or, how can i make a array represent my screen.

Because i dont understand when i will move my pacman on the screen, how it will be exactly represent the same movement on the array?

Will i use the sprite.move, or sprite.setposition ?

Thanks one more time
Title: Pac man game
Post by: Vit on June 30, 2011, 12:31:28 pm
Pac-Man is tile-based; your array should be the size of your tile grid. For example, if each of your tiles was 20x20 pixels, and your screen size was 640x480, your tile grid would be 32x24 tiles. Each element in your array would then represent a 20x20 square on the screen.
When you want to move Pac-Man on the screen, you'll need to find out which tile he'll be moving to first. For example, assuming your tile array starts from the top-left, to move right you'll need to check board[pacman.x + 1][pacman.y]. If the element is passable, you can start moving him into the tile; otherwise, do nothing (as he's trying to move into a wall).
Title: Pac man game
Post by: gl0w on July 04, 2011, 06:52:15 pm
thanks all, you helped me a lot.

When i use the pac.Move(100 * ElapsedTime, 0); how do i know how much frames will the sprite move everytime the function is called?
Title: Pac man game
Post by: Haikarainen on July 05, 2011, 12:47:26 pm
Quote from: "gl0w"
thanks all, you helped me a lot.

When i use the pac.Move(100 * ElapsedTime, 0); how do i know how much frames will the sprite move everytime the function is called?


If you by frames means pixels, then (100 * ElapsedTime)pixels.
Title: Pac man game
Post by: gl0w on July 21, 2011, 12:49:40 am
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
Code: [Select]
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

Code: [Select]
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)

Code: [Select]
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 :)
Title: Pac man game
Post by: gl0w on July 25, 2011, 02:41:23 pm
Anyone can give me some tips?

THank you!
Title: Pac man game
Post by: gl0w on August 12, 2011, 11:53:06 pm
Quote from: "Vit"
Pac-Man is tile-based; your array should be the size of your tile grid. For example, if each of your tiles was 20x20 pixels, and your screen size was 640x480, your tile grid would be 32x24 tiles. Each element in your array would then represent a 20x20 square on the screen.
When you want to move Pac-Man on the screen, you'll need to find out which tile he'll be moving to first. For example, assuming your tile array starts from the top-left, to move right you'll need to check board[pacman.x + 1][pacman.y]. If the element is passable, you can start moving him into the tile; otherwise, do nothing (as he's trying to move into a wall).


Hello,

Could you please tell me what do you mean by "board[pacman.x + 1][pacman.y]" is the position X and Y on the screen? if yes, how can i compare a float number with a integer on the board? I'm not understanding.

Could you kindly explain?
thank you.
Title: Pac man game
Post by: SCPM on August 13, 2011, 12:47:20 am
Quote from: "gl0w"
Quote from: "Vit"
Pac-Man is tile-based; your array should be the size of your tile grid. For example, if each of your tiles was 20x20 pixels, and your screen size was 640x480, your tile grid would be 32x24 tiles. Each element in your array would then represent a 20x20 square on the screen.
When you want to move Pac-Man on the screen, you'll need to find out which tile he'll be moving to first. For example, assuming your tile array starts from the top-left, to move right you'll need to check board[pacman.x + 1][pacman.y]. If the element is passable, you can start moving him into the tile; otherwise, do nothing (as he's trying to move into a wall).


Hello,

Could you please tell me what do you mean by "board[pacman.x + 1][pacman.y]" is the position X and Y on the screen? if yes, how can i compare a float number with a integer on the board? I'm not understanding.

Could you kindly explain?
thank you.


Hello:
You would do something like this:
Code: [Select]
if (board[int(PacMan.GetPosition().x/tileWidth) + 1][int(PacMan.GetPosition().y/tileHeight)] == 0)  // Where 0 denotes a passable tile
       // Code for moving PacMan to the right;

As you can see, by wrapping the PacMan.GetPostion().x/y values with int( ), you can cast the floats as int.  The formula will allow you to get the square Pac-Man is on.  For example, on a 640x480 screen with a grid of 32x24 tiles and PacMan's x/y at 100,78, PacMan is at square board[3][3].  The code will check if board[4][3] is a passable square, and if it is, allow PacMan to move there.  I hope that helps!
Title: Pac man game
Post by: gl0w on August 13, 2011, 02:36:18 pm
Quote from: "SCPM"
Quote from: "gl0w"
Quote from: "Vit"
Pac-Man is tile-based; your array should be the size of your tile grid. For example, if each of your tiles was 20x20 pixels, and your screen size was 640x480, your tile grid would be 32x24 tiles. Each element in your array would then represent a 20x20 square on the screen.
When you want to move Pac-Man on the screen, you'll need to find out which tile he'll be moving to first. For example, assuming your tile array starts from the top-left, to move right you'll need to check board[pacman.x + 1][pacman.y]. If the element is passable, you can start moving him into the tile; otherwise, do nothing (as he's trying to move into a wall).


Hello,

Could you please tell me what do you mean by "board[pacman.x + 1][pacman.y]" is the position X and Y on the screen? if yes, how can i compare a float number with a integer on the board? I'm not understanding.

Could you kindly explain?
thank you.


Hello:
You would do something like this:
Code: [Select]
if (board[int(PacMan.GetPosition().x/tileWidth) + 1][int(PacMan.GetPosition().y/tileHeight)] == 0)  // Where 0 denotes a passable tile
       // Code for moving PacMan to the right;

As you can see, by wrapping the PacMan.GetPostion().x/y values with int( ), you can cast the floats as int.  The formula will allow you to get the square Pac-Man is on.  For example, on a 640x480 screen with a grid of 32x24 tiles and PacMan's x/y at 100,78, PacMan is at square board[3][3].  The code will check if board[4][3] is a passable square, and if it is, allow PacMan to move there.  I hope that helps!


Hello SCPM,

First, thanks for your help! Actually i was already doing that.
The problem is that sometimes when converting to int (pac.GetPosition().x/BLOCKSIZE) it convers the number up, and sometimes down, so, sometimes, the pacman two tiles before hit the wall, it stops, and sometimes he dont. Because the number was rounded to 1, when the pacman is actually on position 2... do you understand what i mean?

note: i'm using the pac.Move(0, -MAX_SPEED); function to move the pacman, is that okay? or there is better option to do it?

Thank you and regards
Title: Pac man game
Post by: SCPM on August 13, 2011, 04:42:39 pm
Casting to int ought to merely truncate the number, not round it up or down.  But I think my mistake is that pac.GetPosition gets the top-left x/y coordinate of the sprite by default, when you really need the sprite's center.  You would have to use pac.GetCenter().x/y (which you would first need to transform to global coordinates).
Title: Pac man game
Post by: gl0w on August 13, 2011, 08:00:12 pm
Quote from: "SCPM"
Casting to int ought to merely truncate the number, not round it up or down.  But I think my mistake is that pac.GetPosition gets the top-left x/y coordinate of the sprite by default, when you really need the sprite's center.  You would have to use pac.GetCenter().x/y (which you would first need to transform to global coordinates).


I understand now!
But, i'm trying to undertstand how to ue the pac.getcenter, and how to transform to global coordinates, i know that first i need to use the pac.setcenter right? With something like (width/2, height/2)...

Could you please give me some tips?
Title: Pac man game
Post by: SCPM on August 13, 2011, 08:11:20 pm
The code would be pac.TransformToGlobal(pac.GetCenter()).x or y.  And you should do pac.SetCenter(halfOfPacMan'sWidth, halfOfPacMan'sHeight).  It should also be possible to just offset PacMan.GetPosition().x and y by adding the values here as well.
Title: Pac man game
Post by: gl0w on August 13, 2011, 08:58:34 pm
Quote from: "SCPM"
The code would be pac.TransformToGlobal(pac.GetCenter()).x or y.  And you should do pac.SetCenter(halfOfPacMan'sWidth, halfOfPacMan'sHeight).  It should also be possible to just offset PacMan.GetPosition().x and y by adding the values here as well.


It's not working :(

Here is what i have:

Code: [Select]
pac.SetCenter(BLOCKSIZE/2, BLOCKSIZE/2);
pac.TransformToGlobal(pac.GetCenter()).x;
pac.TransformToGlobal(pac.GetCenter()).y;

//then i should get the position on the screen and convert to the correct position on the gameboard.

PacPosX = pac.GetCenter().x/BLOCKSIZE;
PacPosY = pac.GetCenter().x/BLOCKSIZE;

// this should give me the position of the center of the pacman on the screen right?

//after this i checked for it on the gameboard.

if ((tabuleiro[PacPosX+1][PacPosY]!=WALL))
   pac.Move(MAX_SPEED, 0);


However i'm still not getting the position of the center of the pacman on the screen. :\
Title: Pac man game
Post by: SCPM on August 13, 2011, 11:33:24 pm
The code ought to be:
Code: [Select]
PacPosX = pac.TransformToGlobal(pac.GetCenter()).x/BLOCKSIZE;
PacPosY = pac.TransformToGlobal(pac.GetCenter()).y/BLOCKSIZE;
Title: Pac man game
Post by: gl0w on August 13, 2011, 11:59:21 pm
Quote from: "SCPM"
The code ought to be:
Code: [Select]
PacPosX = pac.TransformToGlobal(pac.GetCenter()).x/BLOCKSIZE;
PacPosY = pac.TransformToGlobal(pac.GetCenter()).y/BLOCKSIZE;


I have tried that, doesn't work either :( it's not getting the center position.

I have tried several ways around this, none is working. Maybe something is missing ?
Title: Pac man game
Post by: gl0w on August 14, 2011, 03:40:33 pm
Anyone know why i'm not getting the center of the sprite positon on the screen ?

Quote
pac.SetCenter(BLOCKSIZE/2, BLOCKSIZE/2);
pac.TransformToGlobal(pac.GetCenter()).x;
pac.TransformToGlobal(pac.GetCenter()).y;


I'm still getting the uper left corner of the sprite and not the center :\
Title: Pac man game
Post by: gl0w on August 17, 2011, 05:45:32 pm
Sorry for bumping this topic, i'm really trying to get my pacman game work, but, i still have the problem described below.
I'm not able to get the center position on the screen of my pacman.

Could someone from the dev team give me some tips, maybe something i'm doing wrong.


I appreciate.