坦克大战1.0
程序员文章站
2023-12-23 19:13:27
...
import javax.sound.midi.Soundbank;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
/**
* @author 许泽鑫
* @data 2019/11/9 - 21:13
*/
public class TankClient extends JFrame {
public static final int GAME_WIDTH = 800;
public static final int GAME_HEIGHT = 600;
public TankClient(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(GAME_WIDTH,GAME_HEIGHT);
setLocationRelativeTo(null);
setResizable(false);
setBackground(Color.GREEN);
addKeyListener(new KeyMonitor());
setVisible(true);
new Thread(new PaintThread()).start();
}
Tank t1 = new Tank(50,50);
PaoDan p1 = new PaoDan(50,50,Tank.Direction.r);
@Override
public void paint(Graphics g){
t1.draw(g);
p1.draw(g);
}
private class PaintThread implements Runnable{
@Override
public void run() {
while (true){
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Image a = null;
@Override
public void update(Graphics g){
if(a == null){
a = this.createImage(GAME_WIDTH,GAME_HEIGHT);
}
Graphics g1 = a.getGraphics();
Color c= g1.getColor();
g1.setColor(Color.green);
g1.fillRect(0,0,GAME_WIDTH,GAME_HEIGHT);
paint(g1);
g.drawImage(a,0,0,null);
}
@Override
public void repaint(){
super.repaint();
update(getGraphics());
}
private class KeyMonitor extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e){
t1.tankRun(e);
// if(key == KeyEvent.VK_RIGHT){
// x += 5;
// }
// if(key == KeyEvent.VK_LEFT){
// x -= 5;
// }
// if(key == KeyEvent.VK_UP){
// y -= 5;
// }
// if(key == KeyEvent.VK_DOWN){
// y += 5;
// }
}
@Override
public void keyReleased(KeyEvent e) {
super.keyReleased(e);
t1.keyReleased(e);
}
}
public static void main(String[] args) {
new TankClient();
}
}