I have narrowed down my confusion and frustration to one question:
Why can i never add or do anything to a custom grid that i make? It has been haunting me ever since i tried to do the tile game, and now its come back with a PacMan assignment!
Here is my code for the PacMan game, using GridWorld's (almost) perfect grid as a template. (Not complete at all but i am very comfortable with the rest of the code after getting over this little speed bump):
/**
* //Flowers will act as pellets
* //Flashing flowers will act as power pellets
* //
*
*
*
*/
package info.gridworld.gui;
import info.gridworld.grid.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.text.DecimalFormat;
import info.gridworld.grid.*;
import info.gridworld.grid.Location;
import info.gridworld.world.*;
import info.gridworld.actor.*;
public class FinalProject extends ActorWorld {
///////////////////The World, Main Actor, and The Grid/////////////////////////
public static ActorWorld world = new ActorWorld(new BoundedGrid(19,23));
public static Critter pacMan = new Critter();
public static Grid<Actor> grid = world.getGrid();
// ***************************************************
private int score = 0; private int lives = 3;
// ***************************************************
public FinalProject() {
// setGrid(new BoundedGrid(19,23));
setGrid(grid);
setMessage("Lives: " +lives+ " \n " +
"Score:"+score);
//for (int pelletSpace = 437 - grid.getOccupiedLocations(); pelletSpace > 0; pelletSpace--){
// world.add(getRandomEmptyLocation(), new Flower());
// }
for(int rows = grid.getNumRows(); rows <= 23; rows--){
for(int col = grid.getNumCols(); col <= 19; col--){
add(new Location(rows, col), new Rock());
}
}
}
/*public void move()
{
Grid<Actor> gr = getGrid();
if (gr == null)
return;
Location loc = getLocation();
Location next = loc.getAdjacentLocation(getDirection());
if (gr.isValid(next))
moveTo(next);
}*/
public static void main(String[] args) {
//ActorWorld world = new ActorWorld();
//ActorWorld world = new ActorWorld(new BoundedGrid(19,23));
for(int rows = grid.getNumRows(); rows <= 23; rows--){
for(int col = grid.getNumCols(); col <= 19; col--){
add(new Location(rows, col), new Rock());
}
}
Grid<Actor> grid = world.getGrid();
//World<PacMan> world = new World<PacMan>;
world.setGrid(grid);
new FinalProject().show();
// pacMan.setColor(Color.YELLOW);
// Bug inky = new Bug(); //light blue
//Bug pinky = new Bug();
//Pacman = critter
//Ghosts = bugs (Inky = light blue, Pinky = pink, Blinky = red, Clyde = Orange)
java.awt.KeyboardFocusManager.getCurrentKeyboardFocusManager()
.addKeyEventDispatcher(new java.awt.KeyEventDispatcher() {
public boolean dispatchKeyEvent(java.awt.event.KeyEvent event)
{
//if (getFocusOwner() == null) return false;
String key = javax.swing.KeyStroke.getKeyStrokeForEvent(event).toString();
if (key.equals("pressed UP"))
pacMan.setDirection(0);
if (key.equals("pressed RIGHT"))
pacMan.setDirection(90);
if (key.equals("pressed DOWN"))
pacMan.setDirection(180);
if (key.equals("pressed LEFT"))
pacMan.setDirection(270);
return true;
}
});
}
}
Why is it not letting me add anything to my custom sized grid? Do i have to install my own world, like:
public class FinalProject extends World<PacMan> {
I didn't think this was necessary, because i am going to make PacMan a critter and the ghosts bugs, just changing their images to look like the real thing (i already have the images).
This question is haunting me.

New Topic/Question
Reply



MultiQuote






|