Results 1 to 5 of 5
Thread: Tank Game !Help Please!
- 05-03-2011, 08:58 PM #1
Tank Game !Help Please!
I'm in a java programming class right now, and I need help completing my game. I'm using the Location class found at collegeboard.com and all, and I've gotten the right key teleport to work. Basically, that means when I hit the edge of the map, I come out on the otherside of the map, like in Pacman when you hit that little tunnel on the edge of the map. Could someone please help me?
you need the image files I've attached as well.
Here's my code:
XML Code:import java.awt.*; import java.awt.event.*; import sun.audio.*; //import the sun.audio package import java.io.*; import java.util.ArrayList; public class SimpleDemo { private static ArrayList<Location> myBlockLocations = new ArrayList<Location>(); public static void main(String[] args) { play(); } public static void play() { //create display with each cell of size 64x64 pixels GridDisplay display = new GridDisplay(10,10); Grid<Sprite> grid = display.getSpriteGrid(); display.setTitle("Simple Demo"); //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,8)); 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,8)); myBlockLocations.add(new Location(9,9)); for (int i = 0; i < myBlockLocations.size() ; i++) display.getColorGrid().put(myBlockLocations.get(i), Color.GREEN); //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 enemy Sprite enemy = new Sprite(); enemy.setImageFileName("wolfeast.gif"); enemy.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) { hero.setImageFileName("tankeast.gif"); if (loc.getCol() + 1 == grid.getNumCols()) { //teleport hero.setImageFileName("tankeast.gif"); Location nextLocation = new Location(loc.getRow(), 0); hero.moveTo(nextLocation); } else { // normal move Location nextLocation = new Location(loc.getRow(), loc.getCol() + 1); hero.moveTo(nextLocation); //if (!nextLocation.equals(myBlockLocation)) if (!nextLocation.equals(myBlockLocations)) { hero.moveTo(nextLocation); } } } if (key == KeyEvent.VK_LEFT) { hero.setImageFileName("tankwest.gif"); if (loc.getCol() - 1 == grid.getNumCols()) { //teleport hero.setImageFileName("tankwest.gif"); Location nextLocation = new Location(loc.getRow(),9); hero.moveTo(nextLocation); } else { // normal move Location nextLocation = new Location(loc.getRow(), loc.getCol() - 1); //if (!nextLocation.equals(myBlockLocation)) { hero.moveTo(nextLocation); } } } if (key == KeyEvent.VK_UP) { hero.setImageFileName("tanknorth.gif"); if (loc.getRow() - 1 == grid.getNumRows()) { //teleport hero.setImageFileName("tanknorth.gif"); Location nextLocation = new Location(0, loc.getCol()); hero.moveTo(nextLocation); } else { // normal move Location nextLocation = new Location(loc.getRow() - 1, loc.getCol()); //if (!nextLocation.equals(blockLocation)) { hero.moveTo(nextLocation); } } } if (key == KeyEvent.VK_DOWN) { hero.setImageFileName("tanksouth.gif"); if (loc.getRow() + 1 == grid.getNumRows()) { //teleport hero.setImageFileName("tanksouth.gif"); Location nextLocation = new Location(9, loc.getCol()); hero.moveTo(nextLocation); } else { // normal move Location nextLocation = new Location(loc.getRow() + 1, loc.getCol()); //if (!nextLocation.equals(blockLocation)) { hero.moveTo(nextLocation); } } } } } }Last edited by Bianchi60; 05-03-2011 at 11:54 PM. Reason: Needed Tags
- 05-03-2011, 09:03 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Can you please post your code in code tags?
[code]
YOUR CODE HERE
[/code]
You can probably make the tank jump to the other side by setting the x or y to 0.
If it reaches max y, set the new location to x, 0. If you hit the max x move to 0, y. It's similar if you hit 0 as y or x, you would jump to max x or max y.
- 05-03-2011, 11:02 PM #3
Thanks, I'll try that.
- 05-03-2011, 11:09 PM #4
Holy crap! It worked! Thanks. See, my instructor wants us to avoid "hard-coding" if we can, but then again, he also wants the program to work, so thanks!
- 05-03-2011, 11:10 PM #5
Member
- Join Date
- Apr 2011
- Location
- Kansas
- Posts
- 26
- Rep Power
- 0
For my sprite class I used this to warp:
And, like sunde887 said, please edit your post to include code tags.Java Code:if(getXPos() < bounds.getX()) xPos = (int)bounds.getWidth() - getWidth(); if(getYPos() < bounds.getY()) yPos = (int)bounds.getHeight() - getHeight(); if(getXPos() + getWidth() > bounds.getWidth()) xPos = (int)bounds.getX(); if(getYPos() + getHeight() > bounds.getHeight()) yPos = (int)bounds.getY();
Similar Threads
-
Turret tank movement
By nav123 in forum Advanced JavaReplies: 5Last Post: 03-26-2011, 09:44 PM -
artificial intelligence for a tank
By newbie79 in forum New To JavaReplies: 9Last Post: 12-22-2010, 04:27 PM -
Implementing "Game Over" in Minesweeper game based on Gridworld framework.
By JFlash in forum New To JavaReplies: 2Last Post: 08-05-2010, 04:49 AM -
game code for any game
By deathnote202 in forum Java GamingReplies: 4Last Post: 06-10-2010, 08:06 AM -
2D strategy game or 2D war game
By led1433 in forum Java 2DReplies: 5Last Post: 02-10-2009, 06:00 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks