⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ufo_world.java

📁 一个完美的applet游戏
💻 JAVA
📖 第 1 页 / 共 2 页
字号:


import java.awt.*;
import java.applet.*;
import java.util.Vector;


/**
 * <p>Title: UFO_Play</p>
 *
 * <p>Description: 基于applet的攻击UFO的小游戏中的主控类,实现了world和runnable接口</p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: 鹿児島大学</p>
 * @author 柴 智
 * @version 1.0
 */
public class UFO_World implements Runnable, World {


    private Thread game = null; //程序的主线程
    private boolean game_over = true; //用来判断游戏结束与否

    private Vector UV = new Vector(); //定义UFO向量,即一个UFO集合
    private Vector EV = new Vector(); //定义爆炸向量,即一个爆炸集合

    private int NU = 1; //UFO的数目
    private int score = 0; //玩家所得分数

    //要控制的applet;
    private BaseApplet baseApplet = null;
    //要加载的画布
    private UFO_Canvas ufo_canvas;
    //要加载的声音
    private PlayAudio playAudio;
    //要加载的文本显示
    private PlayText playText;


    public UFO_World(BaseApplet baseApplet) {
        this.baseApplet = baseApplet;
    }

    //UFO_Attack类的初始化
    public void init() {
        System.out.println("UFO Attack init.....");
        //初始化游戏画布
        this.ufo_canvas = new UFO_Canvas(this.baseApplet);
        this.playAudio = new PlayAudio();
        this.playText= new PlayText(this.baseApplet);
    }

    public void start() {
        // 使用十字型光标
        this.getFrame(this.baseApplet).setCursor(Frame.
                                                 CROSSHAIR_CURSOR);
        //获取窗口的尺寸
        this.ufo_canvas.setWindow_size(this.baseApplet.size());
        //生成缓冲区
        this.ufo_canvas.setBuffer(null);
        this.playText.setBuffer(null);
        Image buffer = this.baseApplet.createImage(this.ufo_canvas.
                getWindow_size().width, this.ufo_canvas.getWindow_size().height);
        this.ufo_canvas.setBuffer(buffer);
        this.playText.setBuffer(buffer);

        this.ufo_canvas.setBackdrop(null);
        Image backdrop = this.baseApplet.createImage(this.ufo_canvas.
                getWindow_size().width,
                this.ufo_canvas.getWindow_size().height);
        this.ufo_canvas.setBackdrop(backdrop);
        //用背景色来填充缓冲区
        this.ufo_canvas.setBuf_g(buffer.getGraphics());
        this.ufo_canvas.getBuf_g().setColor(ufo_canvas.getBgColor());
        this.ufo_canvas.getBuf_g().fillRect(0, 0,
                                            this.ufo_canvas.getWindow_size().
                                            width,
                                            this.ufo_canvas.getWindow_size().
                                            height);

        // 显示初始化信息
        this.playText.set_say_font(this.playText.getFont());
        this.playText.set_say_mode(this.playText.CENTER);
        this.playText.set_say_style(this.playText.SHADOW);
        this.playText.say("UFO", 10, 80);
        this.playText.say("ATTACK");
        this.playText.set_say_font(this.playText.getFont_s());
        this.playText.set_say_style(this.playText.NORMAL);
        this.playText.say("");
        this.playText.say("Click to start");
        this.playText.say("a game");

        //将缓冲绘制到屏幕上
        Graphics g = this.baseApplet.getGraphics();
        g.drawImage(buffer, 0, 0, this.baseApplet);
        // 初始化导弹发射架
        this.ufo_canvas.setMouse_x(this.ufo_canvas.getWindow_size().width / 2);
        this.ufo_canvas.setL(new Launcher(this.ufo_canvas));
        this.ufo_canvas.getL().set_color(this.ufo_canvas.getGunColor());
        // 初始化导弹
        this.ufo_canvas.setM(new Missile(this.ufo_canvas));
        this.ufo_canvas.getM().set_color(this.ufo_canvas.getGunColor());
        // 加载声音文件
        if (this.playAudio.getExplosion() == null) {
            this.playAudio.setExplosion(this.baseApplet.getAudioClip(this.
                    baseApplet.getCodeBase(), "explosion.au"));
        }
        if (this.playAudio.getNewufo() == null) {
            this.playAudio.setNewufo(this.baseApplet.getAudioClip(this.
                    baseApplet.getCodeBase(), "sonar.au"));
        }
        if (this.playAudio.getMissile_launch() == null) {
            this.playAudio.setMissile_launch(this.baseApplet.getAudioClip(this.
                    baseApplet.getCodeBase(), "rocket.au"));
        }
        game_over = true;
        //声音播放
        this.playAudio.getNewufo().play();
        this.playAudio.getMissile_launch().play();
        this.playAudio.getExplosion().play();
    }

    public void stop() {
        // 如果线程正在运行,强行令其停止
        if (game != null) {
            game.stop();
            game = null; // and eliminate the thread
        }
        // 重新设置光标形状
        getFrame(this.baseApplet).setCursor(Frame.DEFAULT_CURSOR);

    }

    //处理鼠标移动事件
    public boolean mouseMove(Event e, int x, int y) {
        // 返回鼠标所在位置的X坐标
        this.ufo_canvas.setMouse_x(x);
        return true;
    }

    // 处理鼠标的按下事件
    public boolean mouseDown(Event e, int x, int y) {
        //游戏结束时所做的相应处理
        if (game_over) {
            game_over = false;
            if (game != null) {
                game.stop();
                game = null;
            }
            NU = 1;
            score = 0;
            this.ufo_canvas.getM().active(false);
            UV.removeAllElements();
            EV.removeAllElements();
            //新建一个线程
            game = new Thread(this);
            game.setPriority(Thread.MIN_PRIORITY);
            //线程启动
            game.start();

            this.ufo_canvas.getBuf_g().dispose();

            return true;
        }
        //如果游戏没有结束并且导弹没有被发射,则发射导弹
        if (this.ufo_canvas.getM() != null && !this.ufo_canvas.getM().active()) {
            this.playAudio.getMissile_launch().stop();
            this.playAudio.getMissile_launch().play();
            this.ufo_canvas.getM().set_pos(ufo_canvas.getL().px,
                                           ufo_canvas.getL().py);
            this.ufo_canvas.getM().active(true);
        }
        return true;
    }

    public void destroy() {
        // 如果线程正在运行,强行令其停止
      if (game != null) {
          game.stop();
          game = null; // and eliminate the thread
      }
      // 重新设置光标形状
      getFrame(this.baseApplet).setCursor(Frame.DEFAULT_CURSOR);
    }

    public void paint(Graphics g) {
        if (this.ufo_canvas.getBuffer() != null) {
            g.drawImage(ufo_canvas.getBuffer(), 0, 0, this.baseApplet);
        }
    }

    public void run() {
        // 定义本地变量和对象
        UFO U;
        Explosion E;
        long count = 0;
        long ti = 0;

        // 等待图片装载完毕
        Graphics g = this.baseApplet.getGraphics();
        g.setColor(Color.red);
        g.drawString("Starting Game...", 20, 20);

        while (this.ufo_canvas.getTracker().checkAll(true) == false) {
            if ((ti++ % 2) == 0) {
                g.setColor(Color.red);
            } else {
                g.setColor(Color.green);
            }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -