Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
TankClient.java 2.60 KB
Copy Edit Raw Blame History
张聚洋 authored 2023-05-06 15:10 . v1.3
package V13;
import V10.Missile;
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.ArrayList;
import java.util.List;
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,this);
List<Missile> missiles = new ArrayList<Missile>();
Image offScreenImage = null;
public void paint(Graphics g) {
//看出容器中装了多少炮弹
g.drawString("missiles count: " + missiles.size(), 10, 50);
//将容器中的炮弹逐个画出来
for(int i = 0; i < missiles.size(); i++) {
Missile m = missiles.get(i);
m.draw(g);
}
myTank.draw(g);
}
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);
print(gOffScreen);
g.drawImage(offScreenImage, 0, 0, null);
}
public void launchFrame(){
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.GREEN);
this.setVisible(true);
this.addKeyListener(new KeyMonitor());
new Thread(new PaintThread()).start();
}
public static void main(String[] args) {
TankClient tc=new TankClient();
tc.launchFrame();
}
private class PaintThread implements Runnable {
@SuppressWarnings("InfiniteLoopStatement")
public void run() {
while (true){
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) { e.printStackTrace();
}
}
}
}
private class KeyMonitor extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e) {
myTank.keyPressed(e);
}
public void keyReleased(KeyEvent e) {
myTank.kyeReleased(e);
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化