Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Pac man game  (Read 12685 times)

0 Members and 1 Guest are viewing this topic.

gl0w

  • Newbie
  • *
  • Posts: 20
    • View Profile
Pac man game
« 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 :)

Fouf

  • Newbie
  • *
  • Posts: 23
    • View Profile
Pac man game
« Reply #1 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.
- Fouf

slotdev

  • Sr. Member
  • ****
  • Posts: 385
    • View Profile
Pac man game
« Reply #2 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
SFML 2.1

MorleyDev

  • Full Member
  • ***
  • Posts: 219
  • "It is not enough for code to work."
    • View Profile
    • http://www.morleydev.co.uk/
Pac man game
« Reply #3 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.
UnitTest11 - A unit testing library in C++ written to take advantage of C++11.

All code is guilty until proven innocent, unworthy until tested, and pointless without singular and well-defined purpose.

gl0w

  • Newbie
  • *
  • Posts: 20
    • View Profile
Pac man game
« Reply #4 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

Vit

  • Newbie
  • *
  • Posts: 14
    • View Profile
Pac man game
« Reply #5 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).

gl0w

  • Newbie
  • *
  • Posts: 20
    • View Profile
Pac man game
« Reply #6 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?

Haikarainen

  • Guest
Pac man game
« Reply #7 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.

gl0w

  • Newbie
  • *
  • Posts: 20
    • View Profile
Pac man game
« Reply #8 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 :)

gl0w

  • Newbie
  • *
  • Posts: 20
    • View Profile
Pac man game
« Reply #9 on: July 25, 2011, 02:41:23 pm »
Anyone can give me some tips?

THank you!

gl0w

  • Newbie
  • *
  • Posts: 20
    • View Profile
Pac man game
« Reply #10 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.

SCPM

  • Newbie
  • *
  • Posts: 11
    • View Profile
Pac man game
« Reply #11 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!

gl0w

  • Newbie
  • *
  • Posts: 20
    • View Profile
Pac man game
« Reply #12 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

SCPM

  • Newbie
  • *
  • Posts: 11
    • View Profile
Pac man game
« Reply #13 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).

gl0w

  • Newbie
  • *
  • Posts: 20
    • View Profile
Pac man game
« Reply #14 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?