Tanks-bullets?

A 2-D tank game

Page 1 of 1

4 Replies - 4595 Views - Last Post: 14 December 2009 - 01:10 PM Rate Topic: -----

#1 kilogram007   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-December 09

Tanks-bullets?

Post icon  Posted 13 December 2009 - 04:28 PM

Hi guys, I'm a high school student in an AP Comp Sci class. I have taught myself some Java and C++, and so decided to try out a project for myself. I'm trying to make a tank game. I use a graphics package, ACM graphics. I find that it is simpler than raw graphics. I hope you will still be able to help me.
My program is simple enough: a tank, that is controlled by keyboard (up, down, left, right). Spacebar fires a bullet (or is supposed to).

My first problem is with the bullets. I can't seem to make the bullets work: it shows up when I press the space bar, but it just won't move, and then it won't disappear.
Secondly, I have no idea how to make other tanks behave in a way to attack the player controlled tank.
Thirdly, I don't know how to turn my tank in the direction it is facing. I have some idea of how to do this (namely, use multiple pictures and a turn(int degrees) method), but an alternative would be nice.
Here is the actual code.

import acm.graphics.GCompound;
import acm.graphics.GImage;
import acm.graphics.GPoint;


public abstract class MovingObject extends GCompound
{
	private double xvelocity, yvelocity;
	private GPoint loc;
	private GImage image;
	
	public MovingObject(String img)
	{
		this(img, 5.00, 5.00);
	}
	
	
	public MovingObject(String img, int x, int y)
	{
		this(img,5.00,5.00,x,y);
	}
	
	public MovingObject(String img, double x, double y, boolean a)
	{
		this(img,5.00,5.00,x,y);
	}
	
	public MovingObject(String img, double xv, double yv)
	{
		this(img, xv, yv, 0, 0);
	}
	
	public MovingObject(String img, double xv, double yv, int x, int y)
	{
		this(img, xv, yv, (double) x, (double) y);
	}
	
	public MovingObject(String img, double xv, double yv, double x, double y)
	{
		xvelocity = xv;
		yvelocity = yv;
		loc = new GPoint(x, y);
		image = new GImage(img, x, y);
		add(image);
	}
	
	public double getXVelocity() { return xvelocity; }
	
	public double getYVelocity() { return yvelocity; }
	
	public GPoint getLocation() 
	{
		loc = new GPoint(getX(), getY());
		return loc; 
	}
	
	public void setXVelocity(double xv) { xvelocity = xv; }
	
	public void setYVelocity(double yv) { xvelocity = yv; }
	
	public void scaleImage(double scaleFactor) { image.scale(scaleFactor); }
}



import acm.graphics.GCompound;
import acm.graphics.GImage;
import acm.graphics.GPoint;


public class StationaryObject extends GCompound
{
	GPoint loc;
	GImage image;
	
	StationaryObject(String img)
	{
		this(img,new GPoint(0,0));
	}
	
	StationaryObject(String img, int x, int y)
	{
		this(img, new GPoint(x,y));
	}
	
	StationaryObject(String img, GPoint p)
	{
		image = new GImage(img, p.getX(), p.getY());
		add(image);
	}
	
	public void scaleImage(double scaleFactor) { image.scale(scaleFactor); }
}



Those two are the two from which Tank and bullet will inherit.

import acm.graphics.GImage;

public class Tank extends MovingObject
{
	@SuppressWarnings("unused")
	private GImage body;
	
	public Tank()
	{
		this(5.00,5.00);
	}
	
	
	public Tank(int x, int y)
	{
		this(20.00,20.00,x,y);
	}
	
	public Tank(double xv, double yv)
	{
		this(xv, yv, 0, 0);
	}
	
	public Tank(double xv, double yv, int x, int y)
	{
		super("tank.jpg", xv, yv, x, y);
	}
	
	public Tank(double x, double y, boolean a)
	{
		this(20.00,20.00,x,y);
	}
	
	public Tank(double xv, double yv, double x, double y)
	{
		super("tank.jpg", xv, yv, x, y);
	}
	
	public boolean otherTankCollide(Tank o)
	{
		return false;
	}
	
	public boolean bulletCollide(Bullet b)
	{
		return false;
	}

}



public class Bullet extends MovingObject
{
	
	public Bullet()
	{
		this(5.00,5.00);
	}
	
	
	public Bullet(int x, int y)
	{
		this(5.00,5.00,x,y);
	}
	
	public Bullet(double xv, double yv)
	{
		this(xv, yv, 0, 0);
	}
	
	public Bullet(double xv, double yv, int x, int y)
	{
		super("bullet.jpg", xv, yv, x, y);
	}
	
	public boolean collide(GameScreen gamescreen)
	{
		boolean toReturn = false;
		
		if(gamescreen.collide((int)getX(), (int)getY()));
		
		return toReturn;
	}
}



This is my GameScreen class, which is basically a glorified grid:
import acm.graphics.GCompound;
import acm.graphics.GLine;


public class GameScreen extends GCompound
{
	private int xf, yf;
	
	public GameScreen()
	{
		this(1000,800);  
	}
	
	public GameScreen(int size)
	{
		this(size,size);
	}
	
	public GameScreen(int xSize, int ySize)
	{
		xf = xSize; yf = ySize;
		
		for(int x = 0; x <= 20; x++)
		{
			add(new GLine(0,yf,xf,yf));
			yf -= 50;
		}
		
		xf = xSize; yf = ySize;
		
		for(int x = 0; x <= 20; x++)
		{
			add(new GLine(xf,0,xf,yf));
			xf -= 50;
		}
		
		xf = xSize; yf = ySize;
	}
	
	public boolean upEdgeCollide(int x, int y)
	{
		boolean a = false;
		if (y <= 0)
			a = true;
		return a;
	}
	
	public boolean downEdgeCollide(int x, int y)
	{
		boolean a = false;
		if (y > yf)
			a = true;
		return a;
	}
	
	public boolean leftEdgeCollide(int x, int y)
	{
		boolean a = false;
		if (x <= 0)
			a = true;
		return a;
	}
	
	public boolean rightEdgeCollide(int x, int y)
	{
		boolean a = false;
		if (x > xf)
			a = true;
		return a;
	}
	
	public boolean collide(int x, int y)
	{
		boolean toReturn = false;
		if(upEdgeCollide(x, y) || downEdgeCollide(x, y) || leftEdgeCollide(x, y) || rightEdgeCollide(x, y))
			toReturn = true;
		return toReturn;
	}
	
	public static void main(String args[])
	{
		new GameScreen().start(args);
	}
}



This is my Environment(for lack of a better term) that runs the program.

import java.awt.event.KeyEvent;

import acm.program.GraphicsProgram;

@SuppressWarnings("serial")
public class Environment extends GraphicsProgram 
{
	
	public Tank t;
	public GameScreen g;
	public StationaryObject so;
	public Bullet bullet;
	private double vertVelocity, horizVelocity;
	private boolean fire, down, up, right, left, justFired;
	private int xDirection, yDirection;
	
	public void init()
	{
		t = new Tank();
		g = new GameScreen(1000,800);
		so = new StationaryObject("rock.gif");
		bullet = new Bullet();
		fire = down = up = right= left = false;
		xDirection = yDirection = 1;
		
		addKeyListeners();
	}
	
	public void run()
	{
		boolean over = false;
		
		t.scaleImage(0.21645021645021645021645021645022);
		t.setLocation(0,40);
		so.scaleImage(.89);
		bullet.scaleImage(.005);
		
		add(t);
		add(bullet);
		add(g);
		//add(so);
		
		while(!over)
		{
			vertVelocity = t.getYVelocity();
			horizVelocity = t.getXVelocity();
			
			if(g.upEdgeCollide((int)t.getX(), (int)t.getY()) && up)
				t.move(0,vertVelocity);
			if(g.downEdgeCollide((int) t.getX(), (int)t.getY()) && down)
				t.move(0.0, -vertVelocity);
			if(g.leftEdgeCollide((int) t.getX(), (int)t.getY()) && left)
				t.move(horizVelocity, 0);
			if(g.rightEdgeCollide((int) t.getX(), (int)t.getY()) && right)
				t.move(-horizVelocity, 0);
			
			if(up)
			{
				t.move(0, -vertVelocity);
				yDirection = -1;
			}
			if(down)
			{
				t.move(0, vertVelocity);
				yDirection = 1;
			}
			if(right)
			{
				t.move(horizVelocity, 0);
				xDirection = 1;
			}
			if(left)
			{
				t.move(-horizVelocity, 0);
				xDirection = -1;
			}
			
			if(fire)
				bulletCommandCenter();
			else
				bulletControlCenter();
			
			//This entire section, !fire and fire, is messed up.
			//Maybe just make a new method?
			/*
			if(!fire)
			{
				bxPos = t.getX();
				byPos = t.getY();
				bulletXVelocity = xDirection*vertVelocity*4;
				bulletYVelocity = yDirection*horizVelocity*4;
				bullet.setLocation(bxPos, byPos);
				bullet.setXVelocity(bulletXVelocity);
				bullet.setYVelocity(bulletYVelocity);
			}
			
			if(fire)
			{
				bullet.scaleImage(1/.005);
				bullet.move(bulletXVelocity, bulletYVelocity);
				bxPos += bulletXVelocity;
				byPos += bulletYVelocity;
				remove(bullet);
				if(bullet.collide(g))
				{
					bullet.scaleImage(.005);
					remove(bullet);
					fire = false;
				}
			}//end fire/!fire
			*/
			
			pause(100);
		}
	}
	
	public void bulletControlCenter()
	{
	}

	public void bulletCommandCenter()
	{
		double bulletXVelocity = 0, bulletYVelocity = 0;
		if(justFired)
		{
			bullet.setLocation(t.getX(), t.getY());
			bullet.scaleImage(1/.005);
			bulletXVelocity = xDirection*vertVelocity*4;
			bulletYVelocity = yDirection*horizVelocity*4;
			justFired = false;
		}
		else if(fire)
		{
			bullet.move(bulletXVelocity, bulletYVelocity);
			
			if(bullet.collide(g))
			{
				bullet.scaleImage(.005);
				remove(bullet);
				fire = false;
			}
		}
	}
	
	//Ne touche pas!
	public void keyPressed(KeyEvent k)
	{
		
		if(k.getKeyCode() == KeyEvent.VK_UP)
			up = true;
		
		if(k.getKeyCode() == KeyEvent.VK_DOWN)
			down = true;
		
		if(k.getKeyCode() == KeyEvent.VK_LEFT)
			left = true;
		
		if(k.getKeyCode() == KeyEvent.VK_RIGHT)
			right = true;
		
		if(k.getKeyCode() == KeyEvent.VK_SPACE)
		{
			fire = true;
			justFired = true;
		}
	}
	
	//Ne touche pas!
	public void keyReleased(KeyEvent k)
	{
		if(k.getKeyCode() == KeyEvent.VK_DOWN)
			down = false;
		
		if(k.getKeyCode() == KeyEvent.VK_UP)
			up = false;
		
		if(k.getKeyCode() == KeyEvent.VK_LEFT)
			left = false;
		
		if(k.getKeyCode() == KeyEvent.VK_RIGHT)
			right = false;
	}
	
	public static void main(String[] args) 
	{
		new Environment().start(args);
	}

}



I sure hope that you can help me: I have the feeling that I'm way out of my league!

Attached File(s)

  • Attached File  Tank.zip (32.57K)
    Number of downloads: 255


Is This A Good Question/Topic? 0
  • +

Replies To: Tanks-bullets?

#2 pbl   User is offline

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

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Tanks-bullets?

Posted 13 December 2009 - 08:49 PM

Don't think you might have a lot of replies :rolleyes:
You'll need a DIC who use acm graphics and I never heard of it... we are kind of pure Java vanilla flavoured here
but may be somebody else will have an answer
Was This Post Helpful? 0
  • +
  • -

#3 HunterE30633   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 21-October 09

Re: Tanks-bullets?

Posted 14 December 2009 - 12:02 AM

So, luckily I have a lot of experience in ACM programming.

in GraphicsProgram, theres a nice little method.
getElementAt(int x, int y)



Granted that your object moves no more than 1 pixel at a time, I'm sure this will work. I used it to make games like breakout (bricks - w/e, moving paddle on the bottom, rows of bricks on the top, ball bouncing and deleting bricks as they collide) and I even took a shot at making a tank game like yours

This post has been edited by HunterE30633: 14 December 2009 - 12:03 AM

Was This Post Helpful? 0
  • +
  • -

#4 kilogram007   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-December 09

Re: Tanks-bullets?

Posted 14 December 2009 - 12:52 PM

View PostHunterE30633, on 13 Dec, 2009 - 11:02 PM, said:

So, luckily I have a lot of experience in ACM programming.

in GraphicsProgram, theres a nice little method.
getElementAt(int x, int y)



Granted that your object moves no more than 1 pixel at a time, I'm sure this will work. I used it to make games like breakout (bricks - w/e, moving paddle on the bottom, rows of bricks on the top, ball bouncing and deleting bricks as they collide) and I even took a shot at making a tank game like yours



Thank you! I totally forgot about that. This means that I can create objects of the Bullet class and store it's locations and velocities in an array. Thanks again!
Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Tanks-bullets?

Posted 14 December 2009 - 01:10 PM

Moved to Game Programming :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1