java {apt} {jot} the Battle City ...
import java.awt .*;
import javax.sway .*;
import java.awt.accident .*;
import java.util.EventListener;
public class HsTank2 extends JFrame {
HsTank2 (String title) {
this.setTitle (caption);
this.setSize (608, 630);
this.setLocation (300, 100);
this.setBackground (Color. WHITE);
MyTank mp = new MyTank ();
this.add (mp);
this.addKeyListener (mp);
new Thread (mp). start ();
}
public static void cardinal (String [] args)
HsTank2 h = new HsTank2 (
/ / main battle tank
class MyTank extends JPanel implements KeyListener, Runnable {
int x = 280, y = 280; / / initial location of the tank
int op = 1 ; / / instruction of manoeuvre of the tank
int color = 0;
int tankspeed = 8; / / tank speed
int tankbullet = 8; / / tank bullet speed
int tankfbullet = 4; / / enemy bullet speed
int shengming = 100; / / Life
int fenshu = 0;
int nandu = 5; / / Set the game difficulty
/ / bullet
int dx = 295, dy = 295;
int dx1 = 295, dy1 = -10;
int dx2 = 600, dy2 = 295;
int dx3 = 295, dy3 = 600; ;
int dx4 = -10, dy4 = 295;
/ / enemy tanks
int num = 10; / / digit of enemy tanks, can not adjust
int [ ,],[int [num];
int [] dyf = current int [num];
int [] dxf1 = new int [num];
int [] dyf1 = new int [num];
int [] dxf2 = new int [num];
int [] dyf2 = new int [num];
int [] dxf3 = new int [num];
int [] dyf3 = new int [ ,],[br> MyTank () {
for (int i = 0; i = 560)
x = x - tankspeed;
dx = dx - tankspeed;
}
if (e.getKeyCode () == KeyEvent.VK_DOWN) {
op = 3;
y = y + tankspeed;
dy = dy + tankspeed;
if (y> = 560)
y = y - tankspeed ;
dy = dy - tankspeed;
}
if (e.getKeyCode () == KeyEvent.VK_LEFT) {
op = 4;
x = x - tankspeed ;
dx = dx - tankspeed;
if (x y +8 & & dxf1-x> -10 & & dxf1-x -10 & & dyf2 - y y +2 & & dyf3 -10 & & dxf3-x
if (dxf4> x +8 & & dxf4 -10 & & dyf4 - y yf +8 & & dx1-xf> -10 & & dx1-xf xf +2 & & dx2 -10 & & dy2 - yf yf +2 & & dy3 -10 & & dx3-xf xf +8 & & dx4 -10 & & dy4 - yf = 560)
xf -;
dxf -;
;
}
break;
}
case 3: {
yf + +;
dyf + +;
for (int s = 0; s ; = 560)
yf -;
dyf - ;
}
break;
}
case 4: {
xf -;
dxf -;
for (int s = 0; s 0.5) {
for (int i = 0; i 0.5) {
for (int i = 2; i 0.5) {
for (int i = 4; i 0.5) {
for (int i = 6; i 0.5) {
for (int i = 8; i 0.5) {
if (Math.random ()> 0.5 )
opf = 1;
else
opf = 2;
} else {
if (Math.random ()> 0.5)
opf = 3;
else
opf = 4;
}
}
}
if (a% 50 == 11) {
/ / random movement of tanks
for (int i = 2; i 0.5) {
if (Math.random ()> 0.5 )
opf = 1;
else
opf = 2;
} else {
if (Math.random ()> 0.5)
opf = 3;
else
opf = 4;
}
}
}
if (a% 50 == 21) {
/ / random movement of tanks
for (int i = 4; i 0.5) {
if (Math.random ()> 0.5 )
opf = 1;
else
opf = 2;
} else {
if (Math.random ()> 0.5)
opf = 3;
else
opf = 4;
}
}
}
if (a% 50 == 31) {
/ / random movement of tanks
for (int i = 6; i 0.5) {
if (Math.random ()> 0.5 )
opf = 1;
else
opf = 2;
} else {
if (Math.random ()> 0.5)
opf = 3;
else
opf = 4;
}
}
}
if (a% 50 == 41) {
/ / random movement of tanks
for (int i = 8; i 0.5) {
if (Math.promiscuous ()> 0.5 )
opf = 1;
else
opf = 2;
} else {
if (Math.random ()> 0.5)
opf = 3;
else
opf = 4;
}
}
}
/ / redraw
if (shengming <= 0) {
/ / popup player1 victory dialog
JOptionPane.showMessageDialog (null,
. ERROR_MESSAGE);
/ / end of the game
System.exit (0);
}
this.repaint ();
}
}
}
|