代码拉取完成,页面将自动刷新
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;
import java.util.ArrayList;
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, this);
Blood b = new Blood();
//Tank enemyTank = new Tank(100, 100, false, this);
List<Missile> missiles = new ArrayList<Missile>();
List<Explode> explodes = new ArrayList<Explode>();
List<Tank> tanks = new ArrayList<Tank>();
//Wall w1 = new Wall(250,250,250,10,this);
Wall w1 = new Wall(300,300,300,50,this);
//这是一张虚拟图片
Image offScreenImage = null;
//paint这个方法不需要被调用,一旦要被重画的时会被自动 调用
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("blood: " + myTank.getLife(), 10, 110);
//将容器中的爆炸逐个画出来
for (int i = 0; i < explodes.size(); i++) {
Explode e = explodes.get(i);
e.draw(g);
}
w1.draw(g);
myTank.draw(g);
b.draw(g);
myTank.eat(b);
myTank.collidesWithWall(w1);
myTank.collidesWithTanks(tanks);
for (int i = 0; i < tanks.size(); i++) {
tanks.get(i).draw(g);
tanks.get(i).collidesWithWall(w1);
tanks.get(i).collidesWithTanks(tanks);
}
//将容器中的炮弹逐个画出来
for (int i = 0; i < missiles.size(); i++) {
Missile m = missiles.get(i);
m.hitTanks(tanks);
m.hitTank(myTank);
m.hitWall(w1);
//m.hitWall(w2);
m.draw(g);
}
for (int i = 0; i < tanks.size(); i++) {
Tank ta = tanks.get(i);
if (!ta.isLive()) {
tanks.remove(ta);
}
}
if(tanks.size()==0){
this.launchFrame();
}
}
public void update(Graphics g) {
if (offScreenImage == null) {
offScreenImage = this.createImage(GAME_WIDTH,
GAME_HEIGHT);
}
//拿到这个图片的画笔
Graphics gOffScreen = offScreenImage.getGraphics();
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.MAGENTA);
gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
gOffScreen.setColor(c);
print(gOffScreen);
g.drawImage(offScreenImage, 0, 0, null);
}
public void launchFrame() {
for (int i = 0; i < 10; i++) {
tanks.add(new Tank(50 + 40 * (i + 1), 50, false, this));
tanks.get(i).collidesWithWall(w1);
}
this.setLocation(300, 50);
this.setSize(GAME_WIDTH, GAME_HEIGHT);
this.setTitle("TankWar");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setResizable(false);
this.setBackground(Color.CYAN);
this.addKeyListener(new KeyMonitor());
setVisible(true);
new Thread(new PaintThread()).start();
}
public static void main(String[] args) {
TankClient tc = new TankClient();
tc.launchFrame();
}
private class PaintThread implements Runnable {
public void run() {
while (true) {
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private class KeyMonitor extends KeyAdapter {
public void keyPressed(KeyEvent e) {
myTank.KyePressed(e);
}
public void keyReleased(KeyEvent e) {
myTank.kyeReleased(e);myTank.judgmentlife(e);
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。