3 Replies - 10219 Views - Last Post: 22 September 2010 - 09:38 AM Rate Topic: -----

#1 TedSSBM  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 36
  • Joined: 19-March 09

GridWorld custom Grid

Posted 11 May 2009 - 05:43 PM

I know that there are people that use this site that are using the GridWorld case study in their AP Comp. Sci. class, any input from them is appreciated!

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.

Is This A Good Question/Topic? 0
  • +

Replies To: GridWorld custom Grid

#2 Guest_DeadCat*


Reputation:

Re: GridWorld custom Grid

Posted 02 June 2010 - 06:29 PM

instead of using setGrid(), try using a super command:
super(new BoundedGrid<Actor>(x,y));
this always worked for me
...
woah, just realized you posted this question in march last year...
not sure if my answer still applies haha
Was This Post Helpful? 0

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: GridWorld custom Grid

Posted 02 June 2010 - 07:33 PM

More than a year old topic closed
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10183
  • View blog
  • Posts: 37,596
  • Joined: 27-December 08

Re: GridWorld custom Grid

Posted 22 September 2010 - 09:38 AM

Reopened and Moved to GridWorld.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1