1
Name:
Anonymous
2010-05-14 15:43
The Challenge:
|---->Program a 2d tank application in the language of your choice that adheres to the following :
#-The tank must rotate using the left and right directional keys.
#-The tank must loosely resemble a tank.
#-The tank must move forward or backward respectively from the direction it is facing using the up and down directional keys.
#-Pressing spacebar must fire a 'shot' in the direction the tank is facing.
#-The perspective of the user must be fixed and facing down at the tank.
#-The tank, shot, and ground must each have their own color. (all shots may have the same color)
<------You have 24 hours!------>
GET TO WORK!
39
Name:
Anonymous
2010-05-15 2:11
I am an Enterprize Java Developer
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.lang.Math;
public class tankApp extends java.applet.Applet implements MouseListener, KeyListener {
private static final long serialVersionUID = 1L;
boolean start = false;
double xpos = 0;
double ypos = 0;
final int distance = 200;
double counter = 0;
burret[] shots = new burret[3];
boolean mouseEntered;
boolean up = false;
boolean down = false;
boolean left = false;
boolean right = false;
Graphics bufferGraphics;
Image offscreen;
Image offscreen2;
Dimension dim;
Graphics2D g2;
AffineTransform aT;
AffineTransform rotate;
Rectangle2D rect = new Rectangle2D.Float(15, 15, 10, 10);
public void init() {
int i;
setBackground(Color.white);
setSize(640, 480);
dim = getSize();
offscreen = createImage(dim.width,dim.height);
bufferGraphics = offscreen.getGraphics();
offscreen2 = createImage(40,40);
for(i = 0; i < 3; i++)
{
shots[i] = new burret(0, 0, 0);
}
g2 = (Graphics2D) offscreen2.getGraphics();
xpos = 0;
ypos = 0;
addMouseListener(this);
addKeyListener(this);
repaint();
}
public void paint( Graphics g ) {
int i;
if(getSize().height != dim.height || getSize().width != dim.width)
{
System.out.println(dim + " " + getSize());
dim = getSize();
offscreen = createImage(dim.width,dim.height);
offscreen2 = createImage(40,40);
g2 = (Graphics2D) offscreen2.getGraphics();
}
bufferGraphics.clearRect(0,0,dim.width,dim.height);
g2.clearRect(0, 0, 40, 40);
g2.setColor(new Color(255, 0, 0, 10));
g2.fillRect(0,0,40,40);
g2.setColor(Color.black);
bufferGraphics.setColor(Color.BLUE);
if(!start)
{
bufferGraphics.drawString("Click to start!", dim.width/2 - 100, dim.height/2 - 20);
bufferGraphics.setColor(Color.white);
}
aT = g2.getTransform();
rotate = AffineTransform.getRotateInstance(counter,20,20);
g2.transform(rotate);
g2.draw(rect);
g2.drawLine(30, 20, 20, 20);
g2.setTransform(aT);
bufferGraphics.drawString("Angle: " + Math.round(180*counter/Math.PI), 5, dim.height - 20);
bufferGraphics.drawImage(offscreen2,(int)xpos, (int)ypos,this);
for(i=0; i < 3; i++)
{
if(shots[i].active == true)
bufferGraphics.drawRect((int)shots[i].xPos, (int)shots[i].yPos, 5, 5);
}
g.drawImage(offscreen,0,0,this);
cont();
}
private void cont() {
int i;
if(xpos > 0 - 20 && ypos > 0 - 20 && ypos < dim.height && xpos < dim.width)
{
for(i = 0; i < 3; i++)
{
if(shots[i].active == true)
{
shots[i].travel();
if(shots[i].timeCounter > distance && shots[i].active == true)
shots[i].active = false;
}
}
if(up)
{
xpos = xpos + Math.cos(counter);
ypos = ypos + Math.sin(counter);
}
if(down)
{
xpos = xpos - Math.cos(counter);
ypos = ypos - Math.sin(counter);
}
if(right)
{
counter = counter + Math.PI/180;
if(counter >= 2*Math.PI)
counter = counter - 2*Math.PI;
}
if(left)
{
counter = counter - Math.PI/180;
if(counter < 0)
counter = counter + 2*Math.PI;
}
}
else
{
xpos = dim.width / 2;
ypos = dim.height / 2;
}
repaint();
}
public void mouseClicked (MouseEvent me) {
}
public void mousePressed (MouseEvent me) {
start = true;
repaint();
}
public void mouseReleased (MouseEvent me) {
}
public void mouseEntered (MouseEvent me) {
mouseEntered = true;
repaint();
}
public void mouseExited (MouseEvent me) {
mouseEntered = false;
repaint();
}
public void update(Graphics g)
{
paint(g);
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
//System.out.println("test ");
if ( key == KeyEvent.VK_UP )
up = true;
if ( key == KeyEvent.VK_LEFT )
left = true;
if ( key == KeyEvent.VK_DOWN )
down = true;
if ( key == KeyEvent.VK_RIGHT )
right = true;
repaint();
e.consume();
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
int i;
if ( key == KeyEvent.VK_UP )
up = false;
if ( key == KeyEvent.VK_LEFT )
left = false;
if ( key == KeyEvent.VK_DOWN )
down = false;
if ( key == KeyEvent.VK_RIGHT )
right = false;
if ( key == KeyEvent.VK_SPACE)
{
for(i = 0; i < 3; i++)
{
if(shots[i].active == false)
{
shots[i] = new burret(xpos, ypos, counter);
shots[i].active = true;
i = 3;
}
}
}
}
public void keyTyped(KeyEvent e) {
}
}
public class burret {
double xPos;
double yPos;
double angle;
double xComp;
double yComp;
double timeCounter;
boolean active;
public burret(double x, double y, double a)
{
xPos = x + 20 - 2.5;
yPos = y + 20 - 2.5;
xComp = Math.cos(a);
yComp = Math.sin(a);
active = false;
}
public void travel()
{
xPos = xPos + xComp*2;
yPos = yPos + yComp*2;
timeCounter++;
}
}