《马士兵坦克大战经典源代码》由会员分享,可在线阅读,更多相关《马士兵坦克大战经典源代码(18页珍藏版)》请在金锄头文库上搜索。
1、package com.bjsxt.tank;import java.awt.*;import java.awt.event.*;import java.util.List;import java.util.ArrayList;/* * 这个类的作用是坦克嬉戏的主窗口 * author mashibing * */public class TankClient extends Frame /* * 整个坦克嬉戏的宽度 */public static final int GAME_WIDTH = 800;public static final int GAME_HEIGHT = 600;Tank
2、 myTank = new Tank(50, 50, true, Tank.Direction.STOP, this);Wall w1 = new Wall(100, 200, 20, 150, this), w2 = new Wall(300, 100, 300, 20, this);List explodes = new ArrayList();List missiles = new ArrayList();List tanks = new ArrayList();Image offScreenImage = null;Blood b = new Blood();public void p
3、aint(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);g.drawString(tanks life: + myTank.getLife(), 10, 110);if(tanks.size() = 0) for(int i=0; i
4、5; i+) tanks.add(new Tank(50 + 40*(i+1), 50, false, Tank.Direction.D, this);for(int i=0; imissiles.size(); i+) Missile m = missiles.get(i);m.hitTanks(tanks);m.hitTank(myTank);m.hitWall(w1);m.hitWall(w2);m.draw(g);/if(!m.isLive() missiles.remove(m);/else m.draw(g);for(int i=0; iexplodes.size(); i+) E
5、xplode e = explodes.get(i);e.draw(g);for(int i=0; itanks.size(); i+) Tank t = tanks.get(i);t.collidesWithWall(w1);t.collidesWithWall(w2);t.collidesWithTanks(tanks);t.draw(g);myTank.draw(g);myTank.eat(b);w1.draw(g);w2.draw(g);b.draw(g);public void update(Graphics g) if(offScreenImage = null) offScree
6、nImage = 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);/
7、* * 本方法显示坦克主窗口 * */public void lauchFrame() for(int i=0; i10; 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() public void windowClosing(WindowE
8、vent e) System.exit(0););this.setResizable(false);this.setBackground(Color.GREEN);this.addKeyListener(new KeyMonitor();setVisible(true);new Thread(new PaintThread().start();public static void main(String args) TankClient tc = new TankClient();tc.lauchFrame();private class PaintThread implements Runn
9、able public void run() while(true) repaint();try Thread.sleep(50); catch (InterruptedException e) e.printStackTrace();private class KeyMonitor extends KeyAdapter public void keyReleased(KeyEvent e) myTank.keyReleased(e);public void keyPressed(KeyEvent e) myTank.keyPressed(e);package com.bjsxt.tank;i
10、mport java.awt.*;import java.awt.event.*;import java.util.*;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 boolean live = true;private BloodBar bb = new BloodBar();private int life
11、 = 100;TankClient tc;private boolean good;private int x, y;private int oldX, oldY;private static Random r = new Random();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;private Direction ptDir = Direction
12、.D;private int step = r.nextInt(12) + 3;public Tank(int x, int y, boolean good) this.x = x;this.y = y;this.oldX = x;this.oldY = 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)
13、 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);if(good) bb.draw(g);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.