基于java语言的单机版坦克大战源代码

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

/****************************************************************************** 显示游戏的主界面

******************************************************************************/ import java.awt.*;

import java.awt.event.*;

import java.util.List;

import java.util.*;

/**

*

* @author 显示坦克的主窗口

*

*/

public class TankWar extends Frame{

/**

* 坦克主窗口的宽度

*/

public static final int GAME_WIDTH = 800;

/**

* 坦克主窗口的高度

*/

public static final int GAME_HEIGHT = 600;

Image offScreenImage = null;

Tank myTank = new Tank(50,50,true,Tank.Direction.STOP,this);

Wall w = new Wall(150, 200, 20, 150, this);

Blood b = new Blood();

//Tank enemyTank = new Tank(100,100,false,this);

//Explode e = new Explode(150, 150, this);

Missile m = null;

List missiles = new ArrayList();

List explodes = new ArrayList();

List tanks = new ArrayList();

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);

g.drawString("tanks life: "+myTank.getLife(), 10, 110);

if(tanks.size() <= 0) {

for(int i=1; i<=5; i++) {

tanks.add(new Tank(50+40*i, 50, false,Tank.Direction.D, this));

}

}

for(int i=0; i

Missile m = missiles.get(i);

m.hitTanks(tanks);

m.hitTank(myTank);

m.hitWall(w);

//m.hitTank(enemyTank);

m.draw(g);

}

myTank.draw(g);

myTank.eat(b);

w.draw(g);

b.draw(g);

//enemyTank.draw(g);

for(int i=0; i

Explode e = explodes.get(i);

e.draw(g);

}

for(int i=0; i

Tank t = tanks.get(i);

t.draw(g);

t.collidesWithWall(w);

t.collidesWithTank(myTank);

t.collidesWithTanks(tanks);

}

}

public void update(Graphics g) {

if(offScreenImage == null) {

offScreenImage = this.createImage(GAME_WIDTH,GAME_HEIGHT);

}

Graphics offGraphis = offScreenImage.getGraphics();

Color c = offGraphis.getColor();

offGraphis.setColor(Color.GREEN);

offGraphis.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);

offGraphis.setColor(c);

paint(offGraphis);

g.drawImage(offScreenImage, 0, 0, null);

}

public void launchFrame() {

for(int i=1; i<=10; i++) {

tanks.add(new Tank(50+40*i, 50, false,Tank.Direction.D, this));

}

//TankWar TankFrame = new TankWar();

this.setLocation(200,100);

this.setSize(GAME_WIDTH, GAME_HEIGHT);

this.setResizable(false);

this.setBackground(Color.GREEN);

this.addKeyListener(new KeyMonitor());

this.setVisible(true);

new Thread(new PaintThread()).start();

this.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

}

/**

*

* @author 启动一个线程,让其不断的重画

*

*/

private class PaintThread implements Runnable {

public void run() {

try {

while(true) {

repaint();

Thread.sleep(100);

}

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

/**

*

相关文档
最新文档