package src;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.util.*;
import java.io.*;
public class UFOCanvas extends GameCanvas implements Runnable {
private Display display;
private boolean sleeping;
private long frameDelay;
private Random rand;
private Sprite ufoSprite;
private int ufoXSpeed, ufoYSpeed;
private Sprite[] roidSprite = new Sprite[5];//精灵数组
private long gametime;
private long gametimeoffset;
/**游戏暂停变量*/
public boolean isPaused = false;
String pauseStr = "暂停";
String continueStr = "继续";
/**游戏结束变量*/
boolean gameOver = false;
int width;
int height;
public UFOCanvas(Display d) {
super(false);
width = getWidth();
height = getHeight();
display = d;
gametime = 0;
// Set the frame rate (30 fps)
frameDelay = 33;
}
public void start() {
// Set the canvas as the current screen
display.setCurrent(this);
// Initialize the random number generator
rand = new Random();
this.gametimeoffset = System.currentTimeMillis();
// Initialize the UFO and roids sprites
ufoXSpeed = ufoYSpeed = 0;
try {
ufoSprite = new Sprite(Image.createImage("/Saucer.png"));
ufoSprite.setPosition((getWidth() - ufoSprite.getWidth()) / 2,
(getHeight() - ufoSprite.getHeight()) / 2);//初始化了飞碟的位置
Image img = Image.createImage("/Roid.png");//初始化三个小行星精灵
roidSprite[0] = new Sprite(img, 42, 35);
roidSprite[1] = new Sprite(img, 42, 35);
roidSprite[2] = new Sprite(img, 42, 35);
roidSprite[3] = new Sprite(img, 42, 35);
roidSprite[4] = new Sprite(img, 42, 35);
//没有设定特定的初始位置,使用的是缺省值(0,0)
} catch (IOException e) {
System.err.println("Failed loading images!");
}
// Start the animation thread
sleeping = false;
Thread t = new Thread(this);
t.start();
}
public void stop() {
// Stop the animation
sleeping = true;
}
public void run() {
Graphics g = getGraphics();
Font f = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_LARGE);
g.setFont(f);
// The main game loop
while (!sleeping) {
update();
this.gametime = ((System.currentTimeMillis() - gametimeoffset) / 1000);
draw(g);
try {
Thread.sleep(frameDelay);
} catch (InterruptedException ie) {
}
}
}
private void update() {
int keyState = getKeyStates();
if ((keyState & FIRE_PRESSED) != 0) { //右软键,暂停控制
isPaused = !isPaused;
}
if (!isPaused) {
// Process user input to control the UFO speed
//检查四个方向键和并以此来改变飞碟的速度
if ((keyState & LEFT_PRESSED) != 0) {
ufoXSpeed--;
} else if ((keyState & RIGHT_PRESSED) != 0) {
ufoXSpeed++;
}
if ((keyState & UP_PRESSED) != 0) {
ufoYSpeed--;
} else if ((keyState & DOWN_PRESSED) != 0) {
ufoYSpeed++;
}
ufoXSpeed = Math.min(Math.max(ufoXSpeed, -8), 8);//飞碟的速度是在-8到+8之间随机产生的
ufoYSpeed = Math.min(Math.max(ufoYSpeed, -8), 8);
// Move the UFO sprite
ufoSprite.move(ufoXSpeed, ufoYSpeed);//移动飞碟
checkBounds(ufoSprite);//检查飞碟的边界
// 更新小行星精灵的循环
for (int i = 0; i < 5; i++) {
// Move the roid sprites
roidSprite[i].move(i + 1, 1 - i);
checkBounds(roidSprite[i]);
// 每个小行星都有自己独特的动画帧方向,第二个小行星与第一个和第三个小行星的方向相反
if (i == 1) {
roidSprite[i].prevFrame();
} else {
roidSprite[i].nextFrame();
}
//检测到小行星与飞碟的冲突
if (ufoSprite.collidesWith(roidSprite[i], true)) {
// 放出报警音效
AlertType.ERROR.playSound(display);
// 重新设置飞碟精灵的位置和速度,回到屏幕中间并且速度设置为0
ufoSprite.setPosition((getWidth() - ufoSprite.getWidth()) / 2,
(getHeight() - ufoSprite.getHeight()) / 2);
ufoXSpeed = ufoYSpeed = 0;
for (int j = 0; j < 5; j++) {
roidSprite[j].setPosition(0, 0);
}//小行星精灵也要归位
this.gametime = 0;
this.gametimeoffset = System.currentTimeMillis();
// 到此说明已经game over了,不需要继续更新小行星精灵了,直到等待下一次方向键的时间触发
break;
}
}
}
}
private void draw(Graphics g) {
// 清屏
if (!isPaused) {
g.setColor(0x000000);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0x00ffff00);//为了显示计时的数字
g.drawString(String.valueOf(this.gametime), getWidth() / 2 - 15, 10, 0);
// 画飞碟精灵
ufoSprite.paint(g);
// 画小行星精灵
for (int i = 0; i < 5; i++) {
roidSprite[i].paint(g);
}
}
//绘制游戏结束
if (gameOver) {
g.drawString("Game Over", 30, 85,
Graphics.TOP | Graphics.LEFT);
}
//绘制暂停按钮文字
if (isPaused) {
g.drawString(pauseStr, width, height,
Graphics.RIGHT | Graphics.BOTTOM);
} else {
g.drawString(continueStr, width, height,
Graphics.RIGHT | Graphics.BOTTOM);
}
// Flush the offscreen graphics buffer更新双缓存
flushGraphics();
}
private void checkBounds(Sprite sprite) {
// 折返精灵
if (sprite.getX() < -sprite.getWidth()) {
sprite.setPosition(getWidth(), sprite.getY());
} else if (sprite.getX() > getWidth()) {
sprite.setPosition(-sprite.getWidth(), sprite.getY());
}
if (sprite.getY() < -sprite.getHeight()) {
sprite.setPosition(sprite.getX(), getHeight());
} else if (sprite.getY() > getHeight()) {
sprite.setPosition(sprite.getX(), -sprite.getHeight());
}
}
}