?? ufo_world.java
字號:
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 + -