主窗口TankClient.java代碼如下:
import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.ArrayList;
public class TankClient {
public static void main(String[] args) {
new MyFrame("TankWar");
}
}
class MyFrame extends Frame {
public static final int GAME_WIDTH = 800, GAME_HEIGHT = 600;
Tank mytank = new Tank(385, 285, this);
List<Missile> ms = new ArrayList<Missile>();
Image image = null;
public MyFrame(String s) {
super(s);
Label l1 = new Label("r : 重置坦克位置");
setLayout(new FlowLayout(FlowLayout.LEFT, 20, 20));
setBackground(Color.green);
add(l1);
addWindowListener(new WinClose());
addKeyListener(new KeyControl());
this.setResizable(false);
this.setBounds(280, 100, GAME_WIDTH, GAME_HEIGHT);
this.setVisible(true);
new Thread(new Repaint()).start();
}
public void paint(Graphics g) {
g.drawString("missile :" + ms.size(), 20, 550);
for(int i = 0;i < ms.size();i++) {
Missile m1 = ms.get(i);
if(!m1.islive()) ms.remove(m1);
m1.draw(g);
}
mytank.draw(g);
}
public void update(Graphics g) {
if (image == null) {
image = this.createImage(GAME_WIDTH, GAME_HEIGHT);
}
Graphics g11 = image.getGraphics();
Color c = g11.getColor();
g11.setColor(Color.green);
g11.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
g11.setColor(c);
this.paint(g11);
g.drawImage(image, 0, 0, null);
}
/*
* public boolean isResizable() { return false; }
*/
private class Repaint implements Runnable {
public void run() {
while (true) {
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private class WinClose extends WindowAdapter {
public void windowClosing(WindowEvent e) {
setVisible(false);
System.exit(0);
}
}
private class KeyControl extends KeyAdapter {
public void keyPressed(KeyEvent e) {
mytank.key(e);
}
public void keyReleased(KeyEvent e) {
mytank.keyrelease(e);
}
}
}
坦克這個類的代碼如下 Tank.java :
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
public class Tank {
public int x, y;
public final int XTANK_SPEED = 5, YTANK_SPEED = 5, WIDTH = 30,
HEIGHT = 30;
private final Color MYTANKCOLOR = Color.blue;
enum Direction {
L, LU, U, RU, R, RD, D, LD, S
};
Direction dir = Direction.S;
static Direction ptDir = Direction.U;
private boolean bl = false, br = false, bu = false, bd = false;
MyFrame mf;
public Tank(int x, int y) {
this.x = x;
this.y = y;
}
public Tank(int x, int y, MyFrame mf) {
this(x, y);
this.mf = mf;
}
public void reset() {
x = 380;
y = 280;
}
public void draw(Graphics g) {
Color c = g.getColor();
g.setColor(MYTANKCOLOR);
g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(c);
move();
switch (ptDir) {
case L:
g.drawLine(x + WIDTH / 2, y + HEIGHT / 2, x, y + HEIGHT / 2);
g.drawLine(x, y , x + WIDTH, y);
g.drawLine(x, y + HEIGHT , x + WIDTH, y + HEIGHT);
break;
case LU:
g.drawLine(x + WIDTH / 2, y + HEIGHT / 2, x, y);
break;
case U:
g.drawLine(x + WIDTH / 2, y + HEIGHT / 2, x + WIDTH / 2, y);
g.drawLine(x, y, x, y + HEIGHT);
g.drawLine(x + WIDTH, y , x + WIDTH, y + HEIGHT);
break;
case RU:
g.drawLine(x + WIDTH / 2, y + HEIGHT / 2, x + WIDTH, y);
break;
case R:
g.drawLine(x + WIDTH / 2, y + HEIGHT / 2, x + WIDTH, y + HEIGHT / 2);
g.drawLine(x, y , x + WIDTH, y);
g.drawLine(x, y + HEIGHT , x + WIDTH, y + HEIGHT);
break;
case RD:
g.drawLine(x + WIDTH / 2, y + HEIGHT / 2, x + WIDTH, y + HEIGHT);
break;
case D:
g.drawLine(x + WIDTH / 2, y + HEIGHT / 2, x + WIDTH / 2, y + HEIGHT);
g.drawLine(x, y, x, y + HEIGHT);
g.drawLine(x + WIDTH, y , x + WIDTH, y + HEIGHT);
break;
case LD:
g.drawLine(x + WIDTH / 2, y + HEIGHT / 2, x, y + HEIGHT);
break;
}
}
public void key(KeyEvent e) {
int k = e.getKeyCode();
switch (k) {
case KeyEvent.VK_UP:
bu = true;
break;
case KeyEvent.VK_DOWN:
bd = true;
break;
case KeyEvent.VK_LEFT:
bl = true;
break;
case KeyEvent.VK_RIGHT:
br = true;
break;
case KeyEvent.VK_R:
reset();
break;
}
locate();
}
public void keyrelease(KeyEvent e) {
int k = e.getKeyCode();
switch (k) {
case KeyEvent.VK_UP:
bu = false;
break;
case KeyEvent.VK_DOWN:
bd = false;
break;
case KeyEvent.VK_LEFT:
bl = false;
break;
case KeyEvent.VK_RIGHT:
br = false;
break;
case KeyEvent.VK_Z:
fire();
break;
}
locate();
}
public void locate() {
if (!bu && !bd && bl && !br)
dir = Direction.L;
else if (bu && !bd && bl && !br)
dir = Direction.LU;
else if (bu && !bd && !bl && !br)
dir = Direction.U;
else if (bu && !bd && !bl && br)
dir = Direction.RU;
else if (!bu && !bd && !bl && br)
dir = Direction.R;
else if (!bu && bd && !bl && br)
dir = Direction.RD;
else if (!bu && bd && !bl && !br)
dir = Direction.D;
else if (!bu && bd && bl && !br)
dir = Direction.LD;
else if (!bu && !bd && !bl && !br)
dir = Direction.S;
}
public void move() {
switch (dir) {
case L:
x -= XTANK_SPEED;
break;
case LU:
x -= XTANK_SPEED;
y -= YTANK_SPEED;
break;
case U:
y -= YTANK_SPEED;
break;
case RU:
x += XTANK_SPEED;
y -= YTANK_SPEED;
break;
case R:
x += XTANK_SPEED;
break;
case RD:
x += XTANK_SPEED;
y += YTANK_SPEED;
break;
case D:
y += XTANK_SPEED;
break;
case LD:
x -= XTANK_SPEED;
y += YTANK_SPEED;
break;
}
if (this.dir != Direction.S) {
this.ptDir = this.dir;
}
}
public Missile fire() {
Missile m = new Missile(x + WIDTH / 2 - Missile.w / 2, y + HEIGHT / 2- Missile.h / 2, ptDir);
mf.ms.add(m);
return m;
}
public static void main(String[] args) {
}
}
炮彈類代碼如下
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
public class Missile {
static int x,y;
static Tank.Direction dir;
static int X_SPEED = 1,Y_SPEED = 1,w = 10, h = 10;
private boolean islive = true;
public boolean islive() {
return islive;
}
static MyFrame mf1;
public Missile(int x,int y,Tank.Direction dir) {
this.x = x;
this.y = y;
this.dir = dir;
}
public Missile(int x,int y,Tank.Direction dir,MyFrame mf1) {
this(x,y,dir);
this.mf1 = mf1;
}
public void draw(Graphics g) {
Color c = g.getColor();
g.setColor(Color.RED);
g.fillOval(x, y,w , h);
g.setColor(c);
move();
}
private void move() {
switch(dir) {
case L:
x -= X_SPEED;
break;
case LU:
x -= X_SPEED;
y -= Y_SPEED;
break;
case U:
y -= Y_SPEED;
break;
case RU:
x += X_SPEED;
y -= Y_SPEED;
break;
case R:
x += X_SPEED;
break;
case RD:
x += X_SPEED;
y += Y_SPEED;
break;
case D:
y += X_SPEED;
break;
case LD:
x -= X_SPEED;
y += Y_SPEED;
break;
}
if(x < 0 || y < 0 || x > MyFrame.GAME_WIDTH || y > MyFrame.GAME_HEIGHT) {
islive = false;
}
}
public static void main(String[] args) {
}
public String toString() {
return ("x: " + x + "y: " + y + "dir:" + dir);
}
}
public class Missile {
int x,y;
Tank.Direction dir;
int X_SPEED = 1,Y_SPEED = 1,w = 10, h = 10;
private boolean islive = true;
把Missile對象中的變量改成非靜態的