《坦克大战源代码.doc》由会员分享,可在线阅读,更多相关《坦克大战源代码.doc(8页珍藏版)》请在三一文库上搜索。
1、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 ex
2、tends 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 missiles = new ArrayList();List explodes = new ArrayList();List tanks = new ArrayList();Image offScreenImage = null;Overridepublic void
3、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; imissiles.size(); i+) Missile m = missiles.get(i);m.hitTanks(tanks);m.draw(g);for(int i=0; iexplodes.size()
4、; i+) Explode e = explodes.get(i);e.draw(g);for(int i=0; itanks.size(); i+) Tank t = tanks.get(i);t.draw(g);myTank.draw(g);Overridepublic void update(Graphics g) if(offScreenImage = null) offScreenImage = this.createImage(800, 600);Graphics gOffScreen = offScreenImage.getGraphics();Color c = gOffScr
5、een.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; i5; i+) tanks.add(new Tank(50 + 40*(i+1), 50, false, Tank.Direction.D, t
6、his);this.setLocation(400, 300);this.setSize(GAME_WIDTH, GAME_HEIGHT);this.setTitle(TankWar);this.addWindowListener(new WindowAdapter() Overridepublic void windowClosing(WindowEvent e) System.exit(0););this.setResizable(false);this.setBackground(Color.GREEN);this.addKeyListener(new KeyMonitor();this
7、.setVisible(true);new Thread(new PaintThread().start();public static void main(String args) TankClient tc = new TankClient();tc.launchFrame();class PaintThread implements Runnable public void run() while(true) repaint();try Thread.sleep(50); catch (InterruptedException e) e.printStackTrace();class K
8、eyMonitor extends KeyAdapter Overridepublic void keyReleased(KeyEvent e) myTank.keyReleased(e);Overridepublic 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
9、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
10、, 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;t
11、his.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;
12、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
13、 + 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 -= Y
14、SPEED;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 TankClient.GAME_WIDTH) x = TankClient.GAME_WIDTH -
15、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 = dirsr.nextInt(dirs.length);step -;if(r.nextInt(40) 38) this.fire();public void keyPressed(KeyEvent e) int key = e.getKeyCode();swi
16、tch (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
17、;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
18、 & !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 = fals
19、e;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);publ
20、ic 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
21、 = 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 go
22、od, 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;brea
23、k;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 TankClient.GAME_WIDTH | y TankClient.GAME_HE
24、IGHT) 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 fa
25、lse;public boolean hitTanks(List tanks) for(int i=0; itanks.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 T
26、ankClient 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, diametersstep, diametersstep);g.setColor(c);step +;if(step = diameters.length) live = false; 8