TankClient.java
资源名称:TankWar.rar [点击查看]
上传用户:cswwx88
上传日期:2021-03-26
资源大小:9k
文件大小:2k
源码类别:
射击游戏
开发平台:
Java
- import java.awt.*;
- import java.awt.event.*;
- public class TankClient extends Frame{
- public static final int GAME_WIDTH = 800,GAME_HEIGH = 600;
- Tank myTank = new Tank(50,50,this);
- Missile myMissile = null;
- Image offScreamImage = null;
- public static void main(String[] args) {
- new TankClient().lauchFrame();
- }
- public void lauchFrame(){
- this.setBounds(100,100,GAME_WIDTH,GAME_HEIGH);
- addWindowListener(new WindowAdapter(){
- public void windowClosing(WindowEvent e){
- System.exit(0);
- }
- });
- this.setResizable(false);
- setTitle("TankWar");
- this.setBackground(Color.orange);
- this.setVisible(true);
- new Thread(new ThrPaint()).start();
- this.addKeyListener(new keyMonitor());
- }
- /*public class windowClose extends WindowAdapter{
- public void windowClosing(WindowEvent e){
- System.exit(0);
- }
- }*/
- public void paint(Graphics g){
- myTank.draw(g);
- if(myMissile !=null) myMissile.draw(g);
- }
- public void update(Graphics g){
- if(offScreamImage == null){
- offScreamImage = this.createImage(GAME_WIDTH,GAME_HEIGH);
- }
- Graphics goffScreamImage = offScreamImage.getGraphics();
- Color c = goffScreamImage.getColor();
- goffScreamImage.setColor(Color.orange);
- goffScreamImage.fillRect(0, 0, GAME_WIDTH,GAME_HEIGH);
- goffScreamImage.setColor(c);
- paint(goffScreamImage);
- g.drawImage(offScreamImage, 0, 0, null);
- }
- public class ThrPaint implements Runnable{
- public void run(){
- try{
- while(true){
- repaint();
- Thread.sleep(30);
- }
- }catch(InterruptedException e){
- e.printStackTrace();
- }
- }
- }
- public class keyMonitor extends KeyAdapter{
- public void keyPressed(KeyEvent e){
- myTank.keyPressed( e);
- }
- public void keyReleased(KeyEvent e){
- myTank.keyReleased(e);
- }
- }
- }