📄 tankclient.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
public class TankClient extends Frame {
public static final int GAME_WIDTH = 800;
public static final int GAME_HEIGHT = 600;
Tank myTank = new Tank(750, 550, true, Direction.STOP, this);
List<Expolde> expoldes = new ArrayList<Expolde>();
List<Missile> missiles = new ArrayList<Missile>();
List<Tank> tanks = new ArrayList<Tank>();
Image offScreenImage = null;
Bood b = new Bood();
Wall w1 = new Wall(200, 150, 300, 8,this),
w2 = new Wall(250, 200, 200, 8,this),
w3 = new Wall(160, 250, 380, 8,this),
w4 = new Wall(535, 250, 8, 200,this),
w5 = new Wall(350, 120, 8, 400,this),
w6 = new Wall(530, 435, 8, 15,this),
w7 = new Wall(520, 420, 8, 15,this);
public void paint(Graphics g) {
g.drawImage(imgs.get("BI"), 0, 0, null);
g.drawString("剩余坦克的数量 : " + tanks.size() , 30, 50);
g.drawString(" 生命值 : " + myTank.getLife() , 30, 70);
if(tanks.size() ==0) {
for(int i = 0; i < 15; i++) {
tanks.add(new Tank( 50 + 40*(i+1),50, false, Direction.D, this));
}
}
for (int i = 0; i < missiles.size(); i++) {
Missile m = missiles.get(i);
// if(!m.isLive()) missiles.remove(m);
// else m.draw(g);
m.draw(g);
m.hitTanks(tanks);
m.hitTank(myTank);
m.hitWall(w1); m.hitWall(w2); m.hitWall(w3); m.hitWall(w4);
m.hitWall(w5); m.hitWall(w6); m.hitWall(w7);
}
for(int i = 0; i < expoldes.size(); i++) {
Expolde e = expoldes.get(i);
e.draw(g);
}
for(int i = 0; i< tanks.size(); i++) {
Tank t = tanks.get(i);
t.draw(g);
t.TankHitWall(w1); t.TankHitWall(w2); t.TankHitWall(w3); t.TankHitWall(w4);
t.TankHitWall(w5); t.TankHitWall(w6); t.TankHitWall(w7);
t.TankHitTank(tanks);
}
b.draw(g);
myTank.draw(g);
// 设置自己的坦克穿墙
// myTank.TankHitWall(w1); myTank.TankHitWall(w2); myTank.TankHitWall(w3);
// myTank.TankHitWall(w4); myTank.TankHitWall(w5); myTank.TankHitWall(w6); myTank.TankHitWall(w7);
w1.draw(g); w2.draw(g); w3.draw(g); w4.draw(g); w5.draw(g); w6.draw(g); w7.draw(g);
myTank.TankHitTank(tanks);
myTank.eat(b);
}
private static Toolkit tkIm = Toolkit.getDefaultToolkit();
private static Image[] backImages = null;
private static Map<String,Image> imgs = new HashMap<String,Image>();
static {
backImages = new Image[] {
tkIm.getImage(TankClient.class.getClassLoader().getResource("images/caodi-1.jpg")),
};
imgs.put("BI", backImages[0]);
}
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.green);
goffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
goffScreen.setColor(c);
paint(goffScreen);
g.drawImage(offScreenImage, 0, 0, null);
}
public void lauchFrame() {
for(int i = 0; i < 20; i++) {
tanks.add(new Tank( 50 + 40*(i+1),50, false, Direction.D, this));
}
setLayout(new FlowLayout());
setBounds(40, 30, GAME_WIDTH, GAME_HEIGHT);
//this.setBackground(Color.green);
setTitle("TankWar");
setResizable(false);
addKeyListener(new keyMonitor());
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setVisible(true);
new Thread(new paintFrame()).start();
}
private class paintFrame implements Runnable {
public void run() {
while (true) {
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class keyMonitor extends KeyAdapter {
public void keyReleased(KeyEvent e) {
myTank.keyReleased(e);
}
public void keyPressed(KeyEvent e) {
myTank.keyPressed(e);
}
}
public static void main(String[] args) {
TankClient tc = new TankClient();
tc.lauchFrame();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -