Results 1 to 5 of 5
  1. #1
    Bianchi60's Avatar
    Bianchi60 is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default 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);
              }
              
            }
          }
        }
        
      }
    }
    Attached Thumbnails Attached Thumbnails Tank Game !Help Please!-tankeast.jpg   Tank Game !Help Please!-tanknorth.jpg   Tank Game !Help Please!-tanksouth.jpg   Tank Game !Help Please!-tankwest.jpg  
    Last edited by Bianchi60; 05-03-2011 at 11:54 PM. Reason: Needed Tags

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  3. #3
    Bianchi60's Avatar
    Bianchi60 is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    Thanks, I'll try that.

  4. #4
    Bianchi60's Avatar
    Bianchi60 is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    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!

  5. #5
    David M. is offline Member
    Join Date
    Apr 2011
    Location
    Kansas
    Posts
    26
    Rep Power
    0

    Default

    For my sprite class I used this to warp:

    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();
    And, like sunde887 said, please edit your post to include code tags.

Similar Threads

  1. Turret tank movement
    By nav123 in forum Advanced Java
    Replies: 5
    Last Post: 03-26-2011, 09:44 PM
  2. artificial intelligence for a tank
    By newbie79 in forum New To Java
    Replies: 9
    Last Post: 12-22-2010, 04:27 PM
  3. Replies: 2
    Last Post: 08-05-2010, 04:49 AM
  4. game code for any game
    By deathnote202 in forum Java Gaming
    Replies: 4
    Last Post: 06-10-2010, 08:06 AM
  5. 2D strategy game or 2D war game
    By led1433 in forum Java 2D
    Replies: 5
    Last Post: 02-10-2009, 06:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •