package mouse;
import java.util.Random;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
public class MainCanvas extends GameCanvas implements Runnable {
public static final int GAME_UI = 0; //进入用户界面
public static final int GAME_GAMING = 1; //进行游戏
public static final int GAME_END = 2; //游戏结束
public static final int GAME_STATE_NUM = 3; //状态总数
private int m_nState = GAME_UI; //存储当前的游戏状态
private boolean m_bRunning; //控制线程运行
private MyUI m_UI; //用户界面对象
private HammerSprite m_Hammer; //锤子对象
private Hole m_Hole[]; //洞穴对象数组
private MyRecord m_Record; //记录对象
private int m_nTime = 600; //游戏时间
public MainCanvas() {
super(true);
m_UI = new MyUI();
m_Record = new MyRecord("MouseRecord");
try {
//读取锤子精灵图像
Image img = Image.createImage("/hammer.png");
m_Hammer = new HammerSprite(img, 42, 40);
//创建洞穴及洞穴中的地鼠
Image imgMouse = Image.createImage("/mouse.png");
Image imgHole = Image.createImage("/hole.png");
int x = getWidth() / 2;
int y = getHeight() / 2;
m_Hole = new Hole[9];
Random rand = new Random();
m_Hole[0] = new Hole(imgHole, imgMouse, rand, x-50, y-50);
m_Hole[1] = new Hole(imgHole, imgMouse, rand, x , y-50);
m_Hole[2] = new Hole(imgHole, imgMouse, rand, x+50, y -50);
m_Hole[3] = new Hole(imgHole, imgMouse, rand, x-50 , y);
m_Hole[4] = new Hole(imgHole, imgMouse, rand, x , y);
m_Hole[5] = new Hole(imgHole, imgMouse, rand, x +50, y);
m_Hole[6] = new Hole(imgHole, imgMouse, rand, x -50, y+50);
m_Hole[7] = new Hole(imgHole, imgMouse, rand, x , y+50);
m_Hole[8] = new Hole(imgHole, imgMouse, rand, x +50, y+50);
} catch (Exception ex) {
}
setState(GAME_UI);
Start(); //启动线程
}
private void setState(int state) { //设置当前的游戏状态
if (state < 0 || state >= GAME_STATE_NUM) {
return;
}
m_nState = state;
}
public void Start() {
m_bRunning = true;
Thread thread = new Thread(this); //分配新线程
thread.start(); //线程启动
}
public void run() { //新线程自动调用此方法
//获取系统当前时间,并将时间换算成以毫秒为单位的数
long T1 = System.currentTimeMillis();
long T2 = T1;
while (m_bRunning) {
T2 = System.currentTimeMillis();
if (T2 - T1 > 100) { //间隔100毫秒
T1 = T2;
Input();
Logic();
Paint();
}
}
}
public void Stop() { //终止游戏
m_bRunning = false;
}
public void Input() {
int keyStates = getKeyStates();
switch (m_nState) {
case GAME_UI: //进入用户界面
m_UI.Input(keyStates);
if (m_UI.getState() == MyUI.UI_HIDE) {
setState(GAME_GAMING);
}
break;
case GAME_GAMING: //进入游戏
if ((keyStates & GameCanvas.GAME_A_PRESSED) != 0) {
m_Hole[0].HammerDown(m_Hammer);
} else if ((keyStates & GameCanvas.UP_PRESSED) != 0) {
m_Hole[1].HammerDown(m_Hammer);
} else if ((keyStates & GameCanvas.GAME_B_PRESSED) != 0) {
m_Hole[2].HammerDown(m_Hammer);
} else if ((keyStates & GameCanvas.LEFT_PRESSED) != 0) {
m_Hole[3].HammerDown(m_Hammer);
} else if((keyStates & GameCanvas.FIRE_PRESSED) != 0){
m_Hole[4].HammerDown(m_Hammer);
}else if((keyStates & GameCanvas.RIGHT_PRESSED) != 0){
m_Hole[5].HammerDown(m_Hammer);
}else if((keyStates & GameCanvas.GAME_C_PRESSED) != 0){
m_Hole[6].HammerDown(m_Hammer);
}else if((keyStates & GameCanvas.DOWN_PRESSED) != 0){
m_Hole[7].HammerDown(m_Hammer);
}else if((keyStates & GameCanvas.GAME_D_PRESSED) != 0){
m_Hole[8].HammerDown(m_Hammer);
}
break;
case GAME_END: //游戏结束
break;
}
}
public void Logic() {
switch (m_nState) {
case GAME_UI:
break;
case GAME_GAMING:
m_Hammer.Logic();
for (int n = 0; n < m_Hole.length; n++) {
if (m_Hole[n].Logic(m_Hammer) == true) {
//如果有地鼠被打中,则增加积分
int r = m_Record.getCurRecord();
r = r + 1;
m_Record.setCurRecord(r);
}
}
m_nTime--;
if (m_nTime <= 0) {
setState(GAME_END);
}
break;
case GAME_END:
break;
}
}
public void Paint() {
Graphics g = this.getGraphics();
//用黑色清屏
g.setColor(0xFF00FF00);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0xFF0000FF);
switch (m_nState) {
case GAME_UI: //显示界面
m_UI.Paint(g, getWidth(), getHeight());
break;
case GAME_GAMING: //显示游戏画面
for (int n = 0; n < m_Hole.length; n++) {
m_Hole[n].Paint(g);
}
m_Hammer.paint(g);
//显示文字信息
StringBuffer buffer = new StringBuffer();
buffer.append("Time:");
buffer.append(m_nTime / 10);
buffer.append(" Score:");
buffer.append(m_Record.getCurRecord());
g.drawString(buffer.toString(), 10, 10, 0);
break;
case GAME_END: //游戏结束
//显示本次得分及最高分记录
m_Record.Paint(g, getWidth(), getHeight());
break;
}
flushGraphics();
}
}