KEEPING PAC-MAN INSIDE THE BOUNDARIES OF ITS ENVIRONMENT
*The procedure/code below can be applied to any situation with boundaries*
The simplest way to make ensure that Pac-Man stays within the "walls" and other obstacles on the screen is as follows:
- Create an ArrayList that stores every inaccessible location.
- When Pac-Man is moving, first 'get' the coordinates of the location he is
about to move to.
- Check to make sure that location is NOT in the list of locations that are
inaccessible. (use the ".contains()" method)
- If it is inaccessible, prevent the move; if it is a legal move, proceed there.
Creating the ArrayList
The easiest way to do this is to create a Location class in your project - it is essentially the same as in Minnow Project and Mike Ormsbee has a working version of this class in his plane project. When you create your shapes, you need to add every pixel location's coordinates to this ArrayList. (NOTE: This may not be the most efficient way to handle this, but it is easy to understand and not too difficult to implement.)
Example of Implementation
If using the method below:
// fillRect(int x, int y, int width, int height) // Fills the specified rectangle.
// fillRect(10, 20, 40, 100); // magic number representation
int x = 10;
int y = 20;
int width = 40;
int height = 100;
fillRec(x, y, width, height); // actual call
The four endpoints would be: (10,20), (50,20), (10, 120), (50, 120)
We would then want to add every pixel in that range to the ArrayList.
This can easily be achieved using two for loops.
Points that are inaccessible include:
(10, 20), (11, 20), ... (50,20)
(10, 21), (11, 21),... (50, 21)
(10, ... ), (11, ...), ... (50, ...)
(10, 120), (11, 120)..(50, 120)
-----------------------------------------------------------------------------------
design of the for loop
for(int loopX = x; loopX <= x + width; loopX++) // loops through the X coordinates { for(int loopY = y; loopY <= y + height; loopY++)// loops through the Y coordinates { filledLocs.add(new Location(loopX, loopY)); } }
NOTE how this can also be written WITHOUT BRACES because each for loop only has one line of code
// The three lines written below will compile the same as above with the braces for(int loopX = x; loopX <= x + width; loopX++) // loops through the X coordinates for(int loopY = y; loopY <= y + height; loopY++)// loops through the Y coordinates filledLocs.add(new Location(loopX, loopY));
NOTE
This assumes Location has a two parameter constructor for the X and Y coordinates
of the point and that you have already declared and initialized filledLocs to
be a new ArrayList();
------------------------------------------------------------------------------------
You would want to make a method to achieve the for loop above; note that it may depend upon the type of methods you are using to create the graphics.
The check is now very simple - you can use accessors in Location to compare the X and Y values OR make a .equals() method in the Location class (THIS IS WHAT YOU SHOULD DO!) that compares two locations, return TRUE if they correspond to the same point, false if they don't.
=======================================
FINAL NOTES
It should be quite obvious that this is NOT the most efficient way to handle this problem. We shouldn't have to insert the coordinates of interior points of the shape (rather all that should matter are points along the perimeter.) If you only allow your walker to move one pixel at a time, then only adding the perimeter points will do just fine.
*****
What I've written above is the way we will need to think in AP: designing classes
and methods to aid in problem solving. Things won't get easier, but they definitely
will only get more fun! :)