Results 1 to 1 of 1
Thread: Getting there, but not there yet
- 06-01-2011, 06:09 PM #1
Getting there, but not there yet
Hi guys,
Can't believe I didn't see the Game pool earlier, anyways, as I said before, AI is my biggest challenge, and I've almost got it to work. I made a new class called Enemy class, which is compatible only with this game, due how I programmed collision detection. Anyways, I know that the enemy is technically in the grid, but I can't see him, how do I make it so he's visisble?
Here's the code for the game
And here's the Enemy ClassXML Code:import java.awt.*; import java.awt.event.*; import sun.audio.*; //import the sun.audio package import java.io.*; import java.util.ArrayList; import javax.swing.JOptionPane; import java.awt.Color; public class SimpleDemo { public static ArrayList<Location> myBlockLocations = new ArrayList<Location>(); public static void main(String[] args) { play(); } public static boolean canMove(Location l) { for (int i = 0; i < myBlockLocations.size(); i++) if (myBlockLocations.get(i).equals(l)) return false; return true; } public static void play() { //create display with each cell of size 64x64 pixels new AePlayWave("CallMe.wav").start(); GridDisplay display = new GridDisplay(10,10); Enemy wolf = new Enemy(); Grid<Sprite> grid = display.getSpriteGrid(); display.setTitle("Simple Demo"); display.getColorGrid().put(new Location(1, 5), new Color(0,128,128)); //Create location for block myBlockLocations.add(new Location(0,0)); myBlockLocations.add(new Location(0,1)); myBlockLocations.add(new Location(0,2)); myBlockLocations.add(new Location(0,3)); myBlockLocations.add(new Location(0,4)); myBlockLocations.add(new Location(0,5)); myBlockLocations.add(new Location(0,6)); myBlockLocations.add(new Location(0,7)); myBlockLocations.add(new Location(0,9)); myBlockLocations.add(new Location(1,0)); myBlockLocations.add(new Location(1,4)); myBlockLocations.add(new Location(1,9)); myBlockLocations.add(new Location(2,0)); myBlockLocations.add(new Location(2,1)); myBlockLocations.add(new Location(2,4)); myBlockLocations.add(new Location(2,5)); myBlockLocations.add(new Location(2,6)); myBlockLocations.add(new Location(2,9)); myBlockLocations.add(new Location(2,2)); myBlockLocations.add(new Location(3,9)); myBlockLocations.add(new Location(3,0)); myBlockLocations.add(new Location(3,2)); myBlockLocations.add(new Location(4,2)); myBlockLocations.add(new Location(4,4)); myBlockLocations.add(new Location(4,5)); myBlockLocations.add(new Location(4,6)); myBlockLocations.add(new Location(5,0)); myBlockLocations.add(new Location(5,9)); myBlockLocations.add(new Location(6,0)); myBlockLocations.add(new Location(6,3)); myBlockLocations.add(new Location(6,4)); myBlockLocations.add(new Location(6,5)); myBlockLocations.add(new Location(6,7)); myBlockLocations.add(new Location(6,9)); myBlockLocations.add(new Location(7,0)); myBlockLocations.add(new Location(7,1)); myBlockLocations.add(new Location(7,2)); myBlockLocations.add(new Location(7,3)); myBlockLocations.add(new Location(7,7)); myBlockLocations.add(new Location(7,9)); myBlockLocations.add(new Location(8,0)); myBlockLocations.add(new Location(8,5)); myBlockLocations.add(new Location(8,7)); myBlockLocations.add(new Location(8,9)); myBlockLocations.add(new Location(9,0)); myBlockLocations.add(new Location(9,1)); myBlockLocations.add(new Location(9,2)); myBlockLocations.add(new Location(9,3)); myBlockLocations.add(new Location(9,4)); myBlockLocations.add(new Location(9,5)); myBlockLocations.add(new Location(9,6)); myBlockLocations.add(new Location(9,7)); myBlockLocations.add(new Location(9,9)); for (int i = 0; i < myBlockLocations.size() ; i++) display.getColorGrid().put(myBlockLocations.get(i), Color.GRAY); //add object to grid, which will display using file named "hero.gif" Sprite hero = new Sprite(); hero.setImageFileName("tankeast.gif"); hero.putSelfInGrid(grid, new Location(4, 0)); //Add goal Sprite goal = new Sprite(); goal.setImageFileName("disk.gif"); goal.putSelfInGrid(grid, new Location(8,1)); wolf.putSelfInGrid(grid, new Location(1,1)); while (true) { //tells the display to pause and redraw itself display.pause(100); int key = display.checkLastKeyPressed(); Location loc = hero.getLocation(); //test if right-arrow pressed if (key == KeyEvent.VK_RIGHT) { int col=0; /* if (Math.random()<.5) row = EnemyLocation.getRow() + 1; else row = EnemyLocation.getRow(); */ hero.setImageFileName("tankeast.gif"); if (loc.getCol() + 1 == grid.getNumCols()) { //teleport hero.setImageFileName("tankeast.gif"); Location nextLocation = new Location(loc.getRow(), 0); if (canMove(nextLocation)) hero.moveTo(nextLocation); } else { // normal move Location nextLocation = new Location(loc.getRow(), loc.getCol() + 1); if (canMove(nextLocation)) hero.moveTo(nextLocation); } } if (key == KeyEvent.VK_LEFT) { hero.setImageFileName("tankwest.gif"); if (loc.getCol() == 0) { //teleport hero.setImageFileName("tankwest.gif"); Location nextLocation = new Location(loc.getRow(),9); if (canMove(nextLocation)) hero.moveTo(nextLocation); } else { // normal move Location nextLocation = new Location(loc.getRow(), loc.getCol() - 1); if (canMove(nextLocation)) hero.moveTo(nextLocation); } } if (key == KeyEvent.VK_UP) { hero.setImageFileName("tanknorth.gif"); if (loc.getRow() == 0) { //teleport hero.setImageFileName("tanknorth.gif"); Location nextLocation = new Location(9, loc.getCol()); if (canMove(nextLocation)) hero.moveTo(nextLocation); } else { // normal move Location nextLocation = new Location(loc.getRow() - 1, loc.getCol()); if (canMove(nextLocation)) hero.moveTo(nextLocation); } } if (key == KeyEvent.VK_DOWN) { hero.setImageFileName("tanksouth.gif"); if (loc.getRow() == 9) { //teleport hero.setImageFileName("tanksouth.gif"); Location nextLocation = new Location(0, loc.getCol()); if (canMove(nextLocation)) hero.moveTo(nextLocation); } else { // normal move Location nextLocation = new Location(loc.getRow() + 1, loc.getCol()); if (canMove(nextLocation)) hero.moveTo(nextLocation); } } } } }
I realize that I have imported things that I don't use yet, but I just did incase I need them.XML Code:import java.awt.*; import java.awt.event.*; import sun.audio.*; //import the sun.audio package import java.io.*; import java.util.ArrayList; import javax.swing.JOptionPane; import java.awt.Color; public class Enemy { private Grid<Sprite> grid; private Location location; private static GridDisplay display; private static Sprite enemy; private static int direction; private static int delay = 250; private static final String NORTH_IMAGE = "wolfnorth.gif"; private static final String SOUTH_IMAGE = "wolfsouth.gif"; private static final String EAST_IMAGE = "wolfeast.gif"; private static final String WEST_IMAGE = "wolfwest.gif"; private static final Color LIGHT_COLOR = new Color(229, 170, 122); private static final Color DARK_COLOR = new Color(153, 114, 81); //This should put you in the grid public void putSelfInGrid(Grid<Sprite> gr, Location loc) { if (grid != null) throw new IllegalStateException( "This sprite is already contained in a grid."); Sprite sprite = gr.get(loc); if (sprite != null) sprite.removeSelfFromGrid(); grid = gr; location = loc; } //This tells the tank to move public static void move() { if (display == null) throw new RuntimeException("World not loaded yet"); Location loc = enemy.getLocation(); Location newLoc = loc.getAdjacentLocation(direction); if (!display.getSpriteGrid().isValid(newLoc)) throw new RuntimeException("Robot tried to move off the world"); if (display.getSpriteGrid().get(newLoc) != null) throw new RuntimeException("Robot tried to move into a wall"); enemy.moveTo(newLoc); display.pause(delay); } //A left turn is all this tank can do public static void turnLeft() { if (display == null) throw new RuntimeException("World not loaded yet"); direction = (direction + Location.LEFT + 360) % 360; if (direction == Location.NORTH) enemy.setImageFileName(NORTH_IMAGE); else if (direction == Location.SOUTH) enemy.setImageFileName(SOUTH_IMAGE); else if (direction == Location.EAST) enemy.setImageFileName(EAST_IMAGE); else enemy.setImageFileName(WEST_IMAGE); display.pause(delay); } public static void turnRight() { turnLeft(); turnLeft(); turnLeft(); } //this checks to see if the front is clear public static boolean frontIsClear() { if (display == null) throw new RuntimeException("World not loaded yet"); Location loc = enemy.getLocation(); Location newLoc = loc.getAdjacentLocation(direction); if (!display.getSpriteGrid().isValid(newLoc)) return false; else if (display.getSpriteGrid().get(newLoc) != null) return false; else return true; } //This makes the Enemy class part sprite public Grid<Sprite> getGrid() { return grid; } //This finds the enemy's location public Location getLocation() { return location; } public static boolean canMove(Location l) { for (int i = 0; i < SimpleDemo.myBlockLocations.size(); i++) if (SimpleDemo.myBlockLocations.get(i).equals(l)) return false; return true; } public static void setDelay(int milliseconds) { delay = milliseconds; } }


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks