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!
Name:
Anonymous2010-05-14 18:26
Disclaimer: this code has many examples of "bad practices" because I was 100% focused on minimize coding time while meeting the requirements
import java.awt.*;
import java.applet.*;
import java.util.*;
import java.awt.event.*;
import java.awt.image.*;
public class a extends Applet implements Runnable,KeyListener{
double x,y;
double r,sdx,sdy;
boolean left,right,down,up;
double sx=-5,sy=-5;
int TANK_SIZE=20;
BufferedImage buf;
public void init(){
x=getWidth()/2;
y=getHeight()/2;
addKeyListener(this);
buf=new BufferedImage(getWidth(),getHeight(),BufferedImage.TYPE_3BYTE_BGR);
new Thread(this).start();
}
public void update(Graphics g){
paint(buf.getGraphics());
g.drawImage(buf, 0,0,null);
}
public void paint(Graphics g){
int[] xpoints=new int[8];
int[] ypoints=new int[8];
for(int i=0;i<4;i++){
xpoints[i]=(int)(x+TANK_SIZE*Math.cos(r+i*Math.PI/2));
ypoints[i]=(int)(y+TANK_SIZE*Math.sin(r+i*Math.PI/2));
}
xpoints[4]=(4*xpoints[0]+5*xpoints[3])/9;
ypoints[4]=(4*ypoints[0]+5*ypoints[3])/9;
xpoints[5]=(int)(x+1.5*TANK_SIZE*Math.cos(r-3*Math.PI/11));
ypoints[5]=(int)(y+1.5*TANK_SIZE*Math.sin(r-3*Math.PI/11));
xpoints[6]=(int)(x+1.5*TANK_SIZE*Math.cos(r-3*Math.PI/13));
ypoints[6]=(int)(y+1.5*TANK_SIZE*Math.sin(r-3*Math.PI/13));
xpoints[7]=(5*xpoints[0]+4*xpoints[3])/9;
ypoints[7]=(5*ypoints[0]+4*ypoints[3])/9;
g.setColor(Color.gray);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.green);
g.fillPolygon(xpoints,ypoints,8);
if(left)
r-=.1;
if(right)
r+=.1;
double dx=Math.cos(r-Math.PI/4);
double dy=Math.sin(r-Math.PI/4);
if(up){
x+=dx*10;
y+=dy*10;
}
if(down){
x-=dx*10;
y-=dy*10;
}
g.setColor(Color.yellow);
if(sx!=-5&&sy!=-5){
sx+=sdx;
sy+=sdy;
}
if(sx<-3||sx>getWidth()||sy<-3||sy>getWidth()){
sx=-5;
sy=-5;
}
g.fillOval((int)sx-1, (int)sy-1,3, 3);
}
public void run() {
while(true){
Graphics g=getGraphics();
if(g!=null)
update(getGraphics());
try{
Thread.sleep(30);
}catch(Exception e){}
}
}
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()){
case KeyEvent.VK_UP:
up=true;
break;
case KeyEvent.VK_DOWN:
down=true;
break;
case KeyEvent.VK_LEFT:
left=true;
break;
case KeyEvent.VK_RIGHT:
right=true;
break;
case KeyEvent.VK_SPACE:
if(sx==-5&&sy==-5){
sx=x;
sy=y;
sdx=Math.cos(r-Math.PI/4)*20;
sdy=Math.sin(r-Math.PI/4)*20;
}
break;
}
}
public void keyReleased(KeyEvent e) {
switch(e.getKeyCode()){
case KeyEvent.VK_UP:
up=false;
break;
case KeyEvent.VK_DOWN:
down=false;
break;
case KeyEvent.VK_LEFT:
left=false;
break;
case KeyEvent.VK_RIGHT:
right=false;
break;
}
}
public void keyTyped(KeyEvent e){}
}