-
|
|
Looking for help setting up a pacman grid
|
Alright, I have tried avoiding asking for help as long as I can, but I have to admit I am stuck. I have studied the original Pacman more than I have ever intended for this project. I know it runs off of a grid. Were each grid section is marked as either 0 = wall 1 = pill 2 = power pill or something of the sorts so I would end up with say
00000000000000
02111111111120
00000000000000
for example. Now I know I can set up a 2d array, to store those value. But here is my first problem. Say I upload the background as a sprite. How do I divide each section into either a 0,1,2?
Then I know I need to add in collision but I am thinking it will look something roughly like
if(arraypossition x,y = 0)
{
//stop the stupid pacman sprite
}
like I said roughly...
but before I can worry about that I need to fix the first problem.
Any suggestions, good websites/textbooks to go read, tutorials to go look at, or something.
Thanks for all your help!
|
|
-
-
- (5798)
-
premium membership
MVP
-
Posts
3,118
|
Re: Looking for help setting up a pacman grid
|
I actually have a little sample I wrote called " Looks Level to Me" where I show one approach of going about that. In that sample I also provide a short sample of doing a PacMan level (although the code is commented out). Might be of some help to you.
|
|
-
|
|
Re: Looking for help setting up a pacman grid
|
Thanks for the reply! I'm downloading the source code now so I will let you know how that goes. Just wanted to let you know I have studied most of the tutorials on your website and they are great. Thanks for the hard work and advice!
|
|
-
-
- (1229)
-
premium membership
-
Posts
345
|
Re: Looking for help setting up a pacman grid
|
One thing that many beginners always do is write their PacMan code like you did:
Stahlkid:if(arraypossition x,y = 0)
{
//stop the stupid pacman sprite
}
which works if you always want your PacMan to instantaneously move from cell to cell in huge jumps.
The real trick is figuring out how to move from one grid cell to another smoothly.
To do this, you have to increment your pacmans movement little by little BUT only actually check the surrounding gridsquares if you are ALIGNED with the gridsquares, otherwise (for the most part) ignore them. The simplest way to check if your pacman is aligned with the grid is to use the MODULUS operator.
For example, lets assume that your grid is made up of 40x40 pixel squares and your pacman moves incrementally 1 pixel per update. To check if you are at an intersection (aligned with the grid), write your code like this:
| if (pacPosX % 40 == 0 && pacPosY % 40 == 0) |
| { |
| //pacman is aligned with the grid |
| int tileX = pacPosX / 40; |
| int tileY = pacPosY / 40; |
| //check the grid now to see which directions are available |
| //if ( grid(tileX, tileY) == ?)//... do your grid checks here |
| } |
| |
The % is the modulus operator. It performs the division problem of (pacPosX / 40) and returns the REMAINDER of the division problem. Checking if the remainder is ZERO tells you that the position is perfectly aligned and therefore a good time to check the grid to see where pacman can go next.
EDIT: added pacman tile positions tileX, tileY to code
Frighten your friends and family with: Scare MeHelp customers using GRENADES?!?: GoonyCru: Day One BASIC Programming Tutorial
|
|
-
|
|
Re: Looking for help setting up a pacman grid
|
Alright so after having some time to work through this; this is what I have came up with.
I created a 2d array that loads all the differnt sprites needed. 0 = wall, 1 = blank space, 2 = pill, and 3 = power pill
| public static int[,] background = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
| {0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0}, |
| {0, 3, 0, 0, 2, 0, 0, 0, 2, 0, 2, 0, 0, 0, 2, 0, 0, 3, 0}, |
etc..
then I search through my array loading the correct tile based on its number in the spot I desire.
| public void Draw(SpriteBatch theSpriteBatch) |
| { |
| for (int i = 0; i < 21; i++) |
| { |
| for (int j = 0; j < background.Length / 21; j++) |
| { |
| if(background[i,j] == 0) |
| { |
| Position = new Vector2(j * blocksize, i * blocksize); |
| theSpriteBatch.Draw(tileSprite, Position, Color.White); |
| } |
| if(background[i,j] == 2) |
| { |
| Position = new Vector2(j * blocksize, i * blocksize); |
| theSpriteBatch.Draw(pillSprite, Position, Color.White); |
| } |
| if(background[i,j] == 3) |
| { |
| Position = new Vector2(j * blocksize, i * blocksize); |
| theSpriteBatch.Draw(powerpillSprite, Position, Color.White); |
| } |
| } |
| } |
| } |
alright so now I have a pretty map.
So here are the next couple problems I can think of, and after that the rest should be messing around with the AI.
First of all, am I even in the write ballpark for how I am loading in the grid?
If not, any suggestions?
If so, I started messing around with loading in the pacmanSprite and trying to make it move based on keyboard commands but when I do something like...
Position =
new Vector2(x = x-19,y);
to move left, it goes really fast...how could I slow this down.
Then of course the big problem I am having issues with, I need the pacmanSprite to move across the 2d array I just created. So that I can check for collision. How would I make the pacmanSprites x,y corridinates corrispond with the background[i,j] so I can check if there is a wall,pill,etc.
Any input is great!
Thanks
|
|
-
-
- (1229)
-
premium membership
-
Posts
345
|
Re: Looking for help setting up a pacman grid
|
Stahlkid: I need the pacmanSprite to move across the 2d array I just created. So that I can check for collision. How would I make the pacmanSprites x,y corridinates corrispond with the background[i,j] so I can check if there is a wall,pill,etc.
Did you even read the post I posted above yours?
Stahlkid: it goes really fast...how could I slow this down.
perhaps by using smaller increments? (x=x-10)
By the way, when you see that you are rewriting lots of code such as this line:
| Position = new Vector2(j * blocksize, i * blocksize); |
Save yourself the trouble and just do it once, like so:
| for (int j = 0; j < background.Length / 21; j++) |
| { |
| Position = new Vector2(j * blocksize, i * blocksize); |
| if(background[i,j] == 0) |
| { |
| theSpriteBatch.Draw(tileSprite, Position, Color.White); |
| } |
| if(background[i,j] == 2) |
| { |
| theSpriteBatch.Draw(pillSprite, Position, Color.White); |
| } |
| if(background[i,j] == 3) |
| { |
| theSpriteBatch.Draw(powerpillSprite, Position, Color.White); |
| } |
| } |
| |
| |
Frighten your friends and family with: Scare MeHelp customers using GRENADES?!?: GoonyCru: Day One BASIC Programming Tutorial
|
|
-
-
- (42)
-
premium membership
-
Posts
16
|
Re: Looking for help setting up a pacman grid
|
You mentioned AI briefly.
This Gamasutra article may or may not be any interest to you (I found it fascinating tho, YMMV).
http://www.gamasutra.com/view/feature/3938/the_pacman_dossier.php
It gives a very detailed description of how the Ghost AI functioned in the original Pacman arcade game.
Even if you're creating the game in your own image rather than a faithful reproduction of the original, it may give you some ideas.
|
|
-
|
|
Re: Looking for help setting up a pacman grid
|
So this is what I have figured out since my last post.
| if (aCurrentKeyboardState.IsKeyDown(Keys.Left) == true) |
| { |
| bool noCollides = true; |
| |
| for (int i = 0; i < 3; i++) |
| { |
| if (pacPosition.X - 19 == wallposX[i] && pacPosition.Y == wallposY[i]) |
| { |
| noCollides = false; |
| } |
| } |
| if (noCollides == true) |
| { |
| pacPosition = new Vector2(x = x-1, y); |
| } |
| } |
basically I am searching through 2 arrays of positions that are walls, then the pacman character in this case is always checking if the tile next to it (-19) is a valid tile or not. Now I understand how to use the %19 as suggested to make sure that the pacman character is perfectly in a tile, and my collision only works if the tiles are perfectly aligned, but my issue now is actually moving the pacman sprite. If I use (x = x - 19, y) which I assume moves you pixels per second or so I will always stay with in the grid but I will also zoom across the screen at the speed of light. The current (x = x -1,y) is the correct speed I would like to go but the problem with moving 1 pixel at a time is that you can simple hit "up" to move one pixel up and then your are no longer lined up with the grid making all of my collision pointless. Any ideas on how to move the pacman sprite from 19x19 square at a time keeping it perfectly in line with the grid but still allowing it to appear to move at a slower speed?
|
|
-
-
- (1229)
-
premium membership
-
Posts
345
|
Re: Looking for help setting up a pacman grid
|
I think you almost have it, but you are missing one important point!
You only check the grid positions and CHECK PLAYER INPUT, IF AND WHEN ITS ALIGNED.
The code you posted looks like you may be checking grid positions ALL of the time.
Also, you need to set a direction of travel for PacMan and just keep moving him in that direction until he is aligned with a gridsquare. THEN, check for possible directions and compare those directions to the players input.
And you only need ONE two dimensional array. I'm not sure what you mean by "2 arrays", but you only need ONE array that you index with X and Y coordinates. I call it 'grid' in the code below.
Also, in the code below, I compare the grid positions against 'wall'. I'm mostly using 'wall' as pseudo-code. You need to determine what value 'wall' equals, perhaps zero as you previously stated: 0 = wall, 1 = blank space, 2 = pill, and 3 = power pill
Make sure you are doing it something like this:
| if (pacPosition.X % 19 == 0 && pacPosition.Y % 19 == 0) |
| { |
| //pacman is aligned with the grid so it's OK to check grid positions now |
| //check left/right |
| if (aCurrentKeyboardState.IsKeyDown(Keys.Left) == true) |
| { |
| if (grid[pacPosition.X % 19 - 1, pacPosition.Y % 19] != wall) |
| xDir = -1; |
| } |
| else if (aCurrentKeyboardState.IsKeyDown(Keys.Right) == true) |
| { |
| if (grid[pacPosition.X % 19 + 1, pacPosition.Y % 19] != wall) |
| xDir = 1; |
| } |
| else |
| xDir = 0; |
| //now check up/down |
| if (aCurrentKeyboardState.IsKeyDown(Keys.Up) == true) |
| { |
| if (grid[pacPosition.X % 19, pacPosition.Y % 19 - 1] != wall) |
| yDir = -1; |
| } |
| else if (aCurrentKeyboardState.IsKeyDown(Keys.Down) == true) |
| { |
| if (grid[pacPosition.X % 19, pacPosition.Y % 19 + 1] != wall) |
| yDir = 1; |
| } |
| else |
| yDir = 0; |
| } |
| |
| //elsewhere... |
| |
| //increment pacmans position |
| pacPosition += new Vector2(xDir, yDir); |
| //where xDir = -1, 0, or 1 and |
| // yDir = -1, 0, or 1 |
Note: there may be errors in this logic, but it should help get the point across.
Frighten your friends and family with: Scare MeHelp customers using GRENADES?!?: GoonyCru: Day One BASIC Programming Tutorial
|
|
-
|
|
Re: Looking for help setting up a pacman grid
|
I had to change the logic around a little to match my code but just wanted to let you know, I now move across the grid at the right speed, all of my collision works and I have even started working on clearing the pills when you pass them. Thanks a lot for all your help!
|
|
-
-
- (1229)
-
premium membership
-
Posts
345
|
Re: Looking for help setting up a pacman grid
|
Glad to be of help.
I'm sending you my payment details by email. You should be recieving it shortly. :)
And don't forget to mark 'correct' answers as such in your threads when you feel your questions have been answered.
Good day...
Frighten your friends and family with: Scare MeHelp customers using GRENADES?!?: GoonyCru: Day One BASIC Programming Tutorial
|
|
|