views:

1870

answers:

4
+8  A: 

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.

Spencer Ruport
How would this array represent the map? As in a pixel to pixel representation?
Click Upvote
Edited. See above.
Spencer Ruport
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?
Click Upvote
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.
Spencer Ruport
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.
Click Upvote
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].
Spencer Ruport
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.
Willie Wheeler
+2  A: 
  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.

TrayMan
By a square do you mean a pixel/x,y coordinate or a 10 pixel block being a square?
Click Upvote
I mean a block of pixels. Spencer Ruport is talking about the same thing.
TrayMan
+2  A: 

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.

kgrad
+6  A: 
Willie Wheeler
Please give some details on the implementation of this hint (i.e tile based map) also!
Click Upvote
There are different ways to do it. My initial approach would be based on the fact that the Pac-Man maze never changes. With that assumption you can simply represent the maze as a 28 x 31 grid of 0's and 1's. Then just use a bitmap for the actual maze instead of generating it from the grid.
Willie Wheeler
...the 0's and 1's represent the presence or absence a wall by the way. The point of that representation is to support collision detection with the walls. The point of the bitmap is just rendering. This approach requires keeping the grid and bitmap in sync manually.
Willie Wheeler
If you want the maze to dynamically update itself based on the contents of the grid then there's more complexity involved (either represent the different kinds of corner/wall in the grid or else compute them dynamically at runtime from cell neighborhoods). I like to start simply though.
Willie Wheeler