坦克大战源代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
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 {
public static final int GAME_WIDTH = 800;
public static final int GAME_HEIGHT = 600;
Tank myTank = new Tank(50, 50, true, Tank.Direction.STOP, this);
List<Missile> missiles = new ArrayList<Missile>();
List<Explode> explodes = new ArrayList<Explode>();
List<Tank> tanks = new ArrayList<Tank>();
Image offScreenImage = null;
@Override
public void paint(Graphics g) {
g.drawString("missiles count:" + missiles.size(), 10, 50);
g.drawString("explodes count:" + explodes.size(), 10, 70);
g.drawString("tanks count:" + tanks.size(), 10, 90);
for(int i=0; i<missiles.size(); i++) {
Missile m = missiles.get(i);
m.hitTanks(tanks);
m.draw(g);
}
for(int i=0; i<explodes.size(); i++) {
Explode e = explodes.get(i);
e.draw(g);
}
for(int i=0; i<tanks.size(); i++) {
Tank t = tanks.get(i);
t.draw(g);
}
myTank.draw(g);
}
@Override
public void update(Graphics g) {
if(offScreenImage == null) {
offScreenImage = this.createImage(800, 600);
}
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 launchFrame() {
//生产多少地方坦克
for(int i=0; i<5; i++) {
tanks.add(new Tank(50 + 40*(i+1), 50, false, Tank.Direction.D, this));
}
this.setLocation(400, 300);
this.setSize(GAME_WIDTH, GAME_HEIGHT);
this.setTitle("TankWar");
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setResizable(false);
this.setBackground(Color.GREEN);
this.addKeyListener(new KeyMonitor());
this.setVisible(true);
new Thread(new PaintThread()).start();
}
public static void main(String[] args) {
TankClient tc = new TankClient();
unchFrame();
}
class PaintThread implements Runnable {
public void run() {
while(true) {
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class KeyMonitor extends KeyAdapter {
@Override
public void keyReleased(KeyEvent e) {
myTank.keyReleased(e);
}
@Override
public void keyPressed(KeyEvent e) {
myTank.keyPressed(e);
}
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.Random;
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;
boolean good;
int x, y;
private static Random r = new Random();
private boolean live = true;
private int step = r.nextInt(12) + 3;
TankClient tc;
boolean bL, bU, bR, bD;
enum Direction {L, LU, U, RU, R, RD, D, LD, STOP};
Direction dir = Direction.STOP;
Direction ptDir = Direction.D;
public Tank(int x, int y, boolean good) {
this.x = x;
this.y = y;
this.good = good;
}
public Tank(int x, int y, boolean good, Direction dir, TankClient tc) { this(x, y, good);
this.dir = dir;
this.tc = tc;
}
public void draw(Graphics g) {
if(!live) {
if(!good) {
tc.tanks.remove(this);
}
return;
}
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 + WIDTH/2, y + HEIGHT/2, x, y + HEIGHT/2);
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);
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);
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);
break;
case LD:
g.drawLine(x + WIDTH/2, y + HEIGHT/2, x, y + HEIGHT);
break;
}
move();
}
private void move() {
switch(dir) {
case L:
x -= XSPEED;
break;
case LU:
x -= XSPEED;
y -= YSPEED;
break;
case U:
y -= YSPEED;
break;
case RU:
x += XSPEED;
y -= YSPEED;
break;
case R:
x += XSPEED;
break;
case RD:
x += XSPEED;
y += YSPEED;
break;
case D:
y += YSPEED;
break;
case LD:
x -= XSPEED;
y += YSPEED;
break;
case STOP:
break;
}
if(dir != Direction.STOP) {
ptDir = dir;
}
if(x < 0) x = 0;
if(y < 30) y = 30;
if(x + WIDTH > TankClient.GAME_WIDTH) x = TankClient.GAME_WIDTH - WIDTH;
if(y+ HEIGHT> TankClient.GAME_HEIGHT) y= TankClient.GAME_HEIGHT- HEIGHT;
if(!good) {
if(step == 0) {
step = r.nextInt(12) + 3;
Direction[] dirs = Direction.values();
dir = dirs[r.nextInt(dirs.length)];
}
step --;
if(r.nextInt(40) > 38) this.fire();
}
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_LEFT:
bL = true;
break;
case KeyEvent.VK_UP:
bU = true;
break;
case KeyEvent.VK_RIGHT:
bR = true;
break;
case KeyEvent.VK_DOWN:
bD = true;
break;
}
locateDirection();
}
private 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 keyReleased(KeyEvent e) {
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_CONTROL:
fire();
break;
case KeyEvent.VK_LEFT:
bL = false;
break;
case KeyEvent.VK_UP:
bU = false;
break;
case KeyEvent.VK_RIGHT:
bR = false;
break;
case KeyEvent.VK_DOWN:
bD = false;
break;
}
locateDirection();
}
private Missile fire() {
int x = this.x + WIDTH/2 - Missile.WIDTH/2;
int y = this.y + HEIGHT/2 - Missile.HEIGHT/2;
Missile m = new Missile(x, y, this.good, this.ptDir, this.tc);
tc.missiles.add(m);
return m;
}
public Rectangle getRect() {
return new Rectangle(x, y, WIDTH, HEIGHT);
}
public boolean isLive() {
return live;
}
public void setLive(boolean live) {
this.live = live;
}
}
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;
TankClient tc;
int x, y;
Tank.Direction dir = Tank.Direction.R;
boolean live = true;
private boolean good;
public Missile(int x, int y, boolean good, Tank.Direction dir) { this.x = x;
this.y = y;
this.good = good;
this.dir = dir;
}
public Missile(int x, int y, boolean good, Tank.Direction dir, TankClient tc) {
this(x, y, good, dir);
this.tc = tc;
}
public void draw(Graphics g) {
if(!live) {
tc.missiles.remove(this);
return;
}
Color c = g.getColor();
g.setColor(Color.BLACK);
g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(c);
move();
}
private void move() {
switch(dir) {
case L:
x -= XSPEED;
break;
case LU:
x -= XSPEED;
y -= YSPEED;
break;
case U:
y -= YSPEED;
break;
case RU:
x += XSPEED;
y -= YSPEED;
break;
case R:
x += XSPEED;
break;
case RD:
x += XSPEED;
y += YSPEED;
break;
case D:
y += YSPEED;
break;
case LD:
x -= XSPEED;
y += YSPEED;
break;
case STOP:
break;
}
if(x< 0 || y< 0 || x> TankClient.GAME_WIDTH|| y> TankClient.GAME_HEIGHT) {
live = false;
}
}
public Rectangle getRect() {
return new Rectangle(x, y, WIDTH, HEIGHT);
}
public boolean hitTank(Tank t) {
if(this.live && t.isLive() && this.good != t.good
&&this.getRect().intersects(t.getRect())) {
this.live = false;
t.setLive(false);
tc.explodes.add(new Explode(x, y, tc));
return true;
}
return false;
}
public boolean hitTanks(List<Tank> tanks) { for(int i=0; i<tanks.size(); i++) {
if(this.hitTank(tanks.get(i))) {
return true;
}
}
return false;
}
}
import java.awt.Color;
import java.awt.Graphics;
public class Explode {
int x, y;
private int[] diameters = {4, 7, 12, 18, 26, 32, 49, 30, 14, 6};
private boolean live = true;
private TankClient tc;
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;
}
Color c = g.getColor();
g.setColor(Color.ORANGE);
g.fillOval(x, y, diameters[step], diameters[step]);
g.setColor(c);
step ++;
if(step == diameters.length) {
live = false;
}
}
}。