Pac-man game, making walls

For walls, I was thinking that I would just create a ton of rectangles that represent the walls, then cycle through them and see if the player collides with them. The problems I am having are

  1. creating all of these rectangles without writing the same bunch of code for every rectangle, I though about just a for (int i = 0; i < NUMBER_OF_WALLS; i++) but I couldn’t find a way to do this since inside of the loop it would have to check to see what wall you were on anyways to determine the wall coordinates, which would make this pointless.

  2. The second problem is updating to check for collisions with ghosts and players. It seems very reduntant to write collision code for every single rectangle.

Any suggestions as to do those? And if not, is there a way I could make the walls without using rectangles?

p.s. I am using Java2d still, not Slick or LWJGL.

Thanks,

-Nathan

I would make the the board a tile map, represented by a 2D array. That should get you started :wink:

And index them. For example when you hit tile with index of 3, the pac can go futher except up. And so on.

However there’s tricky part when you translating pac’s position into tile array. What will you use as coordinate? pac’s left bottom, center, left up or else?

As for collision detection, I would create a mob class with the code for creating the rectangles and then subclass it into player and ghost classes to handle the collisions.

Thanks, guys. I ended up just using a 2D array as Ra4king suggested, since I have already made a game using that for the map, so it was easy to do again.

If you’re interested in seeing what I have so far, go to http://natekramber.co.cc/ and click Pac-Man on the top :slight_smile:

Here’s the 2D array that I made, took FOREVER -

int[][] level = {
			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},
			{0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,},
			{0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,},
			{0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,},
			{0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,},
			{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,},
			{0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0,},
			{0,1,0,0,0,0,1,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1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,},
			{0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,},
			{0,1,1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,1,1,0,},
			{0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,},
			{0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,},
			{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,},
			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,}
		};

-Nathan

p.s. yes I know that the turning is quite off, I am working on it

Hehehehe after many many tries, i got em all: 246 points ;D

:smiley:

Why I only scored 244 points?!

@OP: don’t use image’s rectangle as collision base, since the pac has different size of frames. Look here

http://dl.dropbox.com/u/54138920/img/pac.png

use pac’s coordinate.

Why would I need to do that? After I put the ghost box in the middle, the only areas that Pac-man will be able to move in will be one pac-wide.

-Nathan

Ck ck ck. When I play pac-man, I’ll take every posible route to achieve victory.