package com.diaodiao;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
public class TankClient extends Frame {
/**
*
*/
private static final long serialVersionUID = 1L;
public static final int GAME_WIDTH = 800;
public static final int GAME_HEIGHT = 600;
public static int titleBarH;
Image offScreenImage = null;
private Tank tank = new Tank(50, 50, true);
private Tank enemyTank = new Tank(100, 100, false);
public List<Explode> explodes = new ArrayList<Explode>();
public void paint(Graphics g) {
g.drawString("missiles count: " + tank.getList().size(), 10, 50);
for(int i = 0; i < tank.getList().size(); i++) {
Missile m = tank.getList().get(i);
if(!m.isLive()) {
tank.getList().remove(m);
Explode explode = new Explode(enemyTank.getX(), enemyTank.getY(), this);
explodes.add(explode);
break;
}
m.draw(g);
m.hitTank(enemyTank);
}
for (int j = 0; j < explodes.size(); j++)
explodes.get(j).draw(g);
tank.draw(g);
enemyTank.draw(g);
}
public void update(Graphics g) { //double buffer
if(offScreenImage == null) {
offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
}
Graphics gOffScreen = offScreenImage.getGraphics();
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.GREEN);
gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
gOffScreen.setColor(c);
paint(gOffScreen); //往虚拟图片上画
g.drawImage(offScreenImage, 0, 0, null); //把虚拟图片贴到窗体上
}
public void lauchFrame() {
this.setLocation(300, 100);
this.setSize(GAME_WIDTH, GAME_HEIGHT);
this.setTitle("TankWar");
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.addKeyListener(new KeyMonitor());
this.setResizable(false);
this.setBackground(Color.GREEN);
setVisible(true);
new Thread(new PaintThread()).start();
new Thread(new FreeThread()).start();
}
/**
* @param args
*/
public static void main(String[] args) {
TankClient tc = new TankClient();
tc.lauchFrame();
Insets insets = tc.getInsets();
TankClient.titleBarH = insets.top + insets.bottom;
//new Thread(tc.new PaintThread()).start(); 可以这么写
}
private class PaintThread implements Runnable { //画坦克 画子弹 都是用这个线程
@Override
public void run() {
while(true) {
repaint(); //repaint--->update--->paint
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private class FreeThread implements Runnable {
@Override
public void run() {
List<Missile> missiles = tank.getList();
while(true) {
for(int i = 0; i < missiles.size() ; i++) {
missiles.get(i).freeMissile(missiles);
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private class KeyMonitor extends KeyAdapter {
@Override
public void keyReleased(KeyEvent e) {
tank.keyReleased(e);
}
@Override
public void keyPressed(KeyEvent e) {
tank.keyPressed(e);
}
}
public List<Explode> getExplodes() {
return explodes;
}
public void setExplodes(List<Explode> explodes) {
this.explodes = explodes;
}
}
package com.diaodiao;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.List;
public class Missile {
public static final int XSPEED = 10;
public static final int YSPEED = 10;
public static final int WIDTH = 10;
public static final int HEIGHT = 10;
private int x;
private int y;
Tank.Direction dir; //子弹飞行的方向
private boolean live = true;
public Missile(int x, int y, Tank.Direction dir) {
this.x = x;
this.y = y;
this.dir = dir;
}
public boolean isLive() {
return live;
}
public void setLive(boolean live) {
this.live = live;
}
public void draw(Graphics g) {
Color c = g.getColor();
g.setColor(Color.BLACK);
g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(c);
move();
}
public void move() {
switch(dir) {
case L:
x -= XSPEED;
break;
case R:
x += XSPEED;
break;
case U:
y -= YSPEED;
break;
case D:
y += YSPEED;
break;
case LU:
x -= XSPEED;
y -= YSPEED;
break;
case RU:
x += XSPEED;
y -= YSPEED;
break;
case RD:
x += XSPEED;
y += YSPEED;
break;
case LD:
x -= XSPEED;
y += YSPEED;
break;
}
}
public void freeMissile(List<Missile> missiles) {
if(x < 0 || y < 0 || x > TankClient.GAME_WIDTH || y > TankClient.GAME_HEIGHT) {
missiles.remove(this);
}
}
public Rectangle getRect() { //missile rect area
return new Rectangle(x, y, WIDTH, HEIGHT);
}
public boolean hitTank(Tank enemyTank) {
if (this.getRect().intersects(enemyTank.getRect())) {
this.setLive(false);
enemyTank.setLive(false);
return true;
}
return false;
}
}
import java.awt.*;
public class Explode {
int x, y;
private boolean live = true;
private TankClient tc ;
int[] diameter = {4, 7, 12, 18, 26, 32, 49, 30, 14, 6};
int step = 0;
public Explode(int x, int y, TankClient tc) {
this.x = x;
this.y = y;
this.tc = tc;
}
public void draw(Graphics g) {
if(!live) {
tc.explodes.remove(this);
return;
}
if(step == diameter.length) {
live = false;
step = 0;
return;
}
Color c = g.getColor();
g.setColor(Color.ORANGE);
g.fillOval(x, y, diameter[step], diameter[step]);
g.setColor(c);
step ++;
}
}
package com.diaodiao;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
public class Tank {
public static final int XSPEED = 5;
public static final int YSPEED = 5;
public static final int WIDTH = 30;
public static final int HEIGHT = 30;
private int x, y;
private boolean good;
private boolean live = true;
private boolean bL = false, bU = false, bR = false, bD = false;
enum Direction {L, LU, U, RU, R, RD, D, LD, STOP};
private Direction dir = Direction.STOP; //坦克有stop状态
private Direction ptDir = Direction.D; //子弹是没有stop状态的
private List<Missile> list = new ArrayList<Missile>();
public Tank(int x, int y, boolean good) {
this.x = x;
this.y = y;
this.good = good;
}
public Tank(){}
public List<Missile> getList() {
return list;
}
public void setList(List<Missile> list) {
this.list = list;
}
public boolean isLive() {
return live;
}
public void setLive(boolean live) {
this.live = live;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public void draw(Graphics g) {
if(!isLive()) {
return ;
}
move();
Color c = g.getColor();
if(good)
g.setColor(Color.RED);
else
g.setColor(Color.BLUE);
g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(c);
switch(ptDir) {
case L:
g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x, y + Tank.HEIGHT/2);
break;
case LU:
g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x, y);
break;
case U:
g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH/2, y);
break;
case RU:
g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH, y);
break;
case R:
g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH, y + Tank.HEIGHT/2);
break;
case RD:
g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH, y + Tank.HEIGHT);
break;
case D:
g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH/2, y + Tank.HEIGHT);
break;
case LD:
g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x, y + Tank.HEIGHT);
break;
}
}
public void move() {
switch(dir) {
case L:
x -= XSPEED;
break;
case R:
x += XSPEED;
break;
case U:
y -= YSPEED;
break;
case D:
y += YSPEED;
break;
case LU:
x -= XSPEED;
y -= YSPEED;
break;
case RU:
x += XSPEED;
y -= YSPEED;
break;
case RD:
x += XSPEED;
y += YSPEED;
break;
case LD:
x -= XSPEED;
y += YSPEED;
break;
case STOP:
break;
}
if(dir != Direction.STOP) {
this.ptDir = this.dir;
}
if(x < 0) x = 0;
if(y < TankClient.titleBarH) y = TankClient.titleBarH;
if(y + Tank.HEIGHT > TankClient.GAME_HEIGHT) y = TankClient.GAME_HEIGHT - Tank.HEIGHT;
if(x + Tank.WIDTH > TankClient.GAME_WIDTH) x = TankClient.GAME_WIDTH - Tank.WIDTH;
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch(key) {
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;
}
locateDirection();
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
switch(key) {
case KeyEvent.VK_CONTROL:
fire();
break;
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;
}
locateDirection();
}
public void locateDirection() {
if(bL && !bU && !bR && !bD) dir = Direction.L;
else if(bL && bU && !bR && !bD) dir = Direction.LU;
else if(!bL && bU && !bR && !bD) dir = Direction.U;
else if(!bL && bU && bR && !bD) dir = Direction.RU;
else if(!bL && !bU && bR && !bD) dir = Direction.R;
else if(!bL && !bU && bR && bD) dir = Direction.RD;
else if(!bL && !bU && !bR && bD) dir = Direction.D;
else if(bL && !bU && !bR && bD) dir = Direction.LD;
else if(!bL && !bU && !bR && !bD) dir = Direction.STOP;
}
public void fire() {
int x = this.x + Tank.WIDTH/2 - Missile.WIDTH/2;
int y = this.y + Tank.HEIGHT/2 - Missile.HEIGHT/2;
Missile m = new Missile(x, y, ptDir);
list.add(m);
}
public Rectangle getRect() { //tank rect area
return new Rectangle(x, y, WIDTH, HEIGHT);
}
}
http://www.ce.cn/cysc/tech/07hlw/guonei/201302/26/t20130226_21431285.shtml