Pacman maze in Java
Asked Answered
V

3

8

So I'm building the pacman game in Java to teach myself game programming.

I have the basic game window with the pacman sprite and the ghost sprites drawn, the pacman moves with the arrow keys, doesn't move beyond the walls of the window, etc. Now I'm trying to build the maze, as in this picture:

Pacman maze

Without giving me the direct/complete solution to this, can someone guide me as to how this can be built? I'm talking only about the boundaries and the pipes('T' marks) here which you can't go through and you have to go around. Not the dots which the pacman eats yet.

Here are my questions:

1) What's the most efficient algorithm/method for creating this maze? Will it have to be drawn each time the paint() method is called or is there a way to draw it only at the start of the game and never again?

2) How will this actually be drawn to the screen? I assume the fillRect() will be used?

3) Any hints on collision detection (so the pacman/ghosts can't go through the walls) would be helpful.

4) Any hints on how the vacant space between the pipes will be calculated so the dots can be filled between them will also be very helpful.

Thanks

Vibrations answered 7/3, 2009 at 20:45 Comment(0)
P
10

I wouldn't do it that way.

I'd draw the graphical map and then create a 2D data array which represents the map. The data map would be responsible for determining collisions, eating dots, where candy is and where the ghosts are. Once all the logic for everything is handled just use the 2D array to display everything in their proper pixel coordinates over the graphical map.

For example the user is pressing the left key. First you determine that pacman is at element 3, 3. Element 3, 2 contains information denoting a wall so you can implement the code to make him ignore the command.

EDIT:

Each element would represent about where a dot could be. For example:

No, looking at the board I would say the array would look something like this.

d,d,d,d,d,d,d,d,d,d,d,d,w,w,d,d,d,d,d,d,d,d,d,d,d,d
d,w,w,w,w,d,w,w,w,w,w,d,w,w,d,w,w,w,w,w,d,w,w,w,w,d
p,w,w,w,w,d,w,w,w,w,w,d,w,w,d,w,w,w,w,w,d,w,w,w,w,p    
d,w,w,w,w,d,w,w,w,w,w,d,w,w,d,w,w,w,w,w,d,w,w,w,w,d    
d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d

And so on. You might want to pick a more flexible data structure than just characters however since some areas need to contain a bunch of different information. IE even though the ghost spawning area is blank, pacman isn't allowed in there. The movement of the ghosts and pacman is different for the side escapes, the candy spawn point is a blank spot but if you want to remain flexible you'll want to denote where this is on a per map basis.

Another thing you'll want to remember is that pacman and the ghosts are often inbetween points so containing information that represents a percentage of a space they're taking up between 1,2 and 1,3 is important for collision detection as well as determining when you want to remove dots, powerups and candy from the board.

Pothole answered 7/3, 2009 at 21:2 Comment(6)
How would this array represent the map? As in a pixel to pixel representation?Vibrations
Thanks. So if i wanted to move the pacman to 100 x, 50 y position, how would i set that in the map to show that that position is occupied? any code for that?Vibrations
change the data structure to say something like map[100,50].OccupiedBy = Characters.PacMan. Characters could be an enum with Characters.Ghost1, Characters.Ghost2 etc.Pothole
Which collection would you use for that, hashtable or linkedlist? i didn't know you could have an index like 100,50, i thought it had to be either 100/50 or "100,50" as a string.Vibrations
Oh sorry. VB programming habit. You could create a linked list or a hashmap if you want but since the map isn't going to grow I'd stick with a 2D array. I meant to put Map[100][50].Pothole
The representations of the maze and character locations should be related but different. The maze has a coarse-grained structure that doesn't require pixel-level representation. Character location requires it (or at least finer than the maze rep). So there would be scale transform involved.Raskind
Z
2
  1. You can paint the map into a BufferedImage and just drawImage that on every paint(). You'll get quite reasonable performance this way.

  2. If you are happy with the walls being solid, you can draw each square wall block with fillRect. If you wish to get the same look as in the picture, you need to figure how to draw the lines in the right way and use arcs for corners.

  3. The Pacman game map is made of squares and Pacman and the ghosts always move from one square to the neighbouring square in an animated step (i.e. you press right, the pacman moves one square to the right). That means that collision detection is easy: simply don't allow moves to squares that are not empty.

  4. I do not understand what you are trying to ask here.

Zagreus answered 7/3, 2009 at 21:2 Comment(2)
By a square do you mean a pixel/x,y coordinate or a 10 pixel block being a square?Vibrations
I mean a block of pixels. Spencer Ruport is talking about the same thing.Zagreus
M
2

1) Just to give my advice on redrawing. Something that you can do if you find redrawing the entire image is slow, is determine only the elements that have changed on the screen and redraw those. An approach for this would be the following: Determine the sprites that have moved. Determine (approximate) a rectangle around those sprites. Redraw those rectangles only. This way you are only refreshing parts of the screen and not the whole screen. This should result in an increase in performance over redrawing the entire screen.

The other answers have been reasonable for the other questions you have asked.

Mientao answered 7/3, 2009 at 21:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.