?? kyodaiui.java
字號:
package com.ismyway.n840_kyodai;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.io.*;
/**
* <p>Title: S60_Kyodai</p>
* <p>Description: for S60 platform</p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: www.ismyway.com</p>
* @author ZhangJian
* @version 1.0
*/
public class KyodaiUI
extends GameCanvas
implements Runnable {
Kyodai kyodai;
Graphics g;
GameMap gm = GameMap.getInstance();
Point currentPoint = new Point(0, 0); //當前光標停留的位置
Point lastSelected = new Point(); //上一次選擇的位置
boolean gameEnd = true; //當前游戲是否已經結束
int gameSteps; //步數
int refreshTimes; //刷新地圖的次數
long startTime = 0l; //為難度級別準備的定時器
long frames = 0l; //幀數
private int SeriesHints = 0; //連擊次數
private int earseScore = 0; //消除方塊的分數
private long lastDelTime = 0; //上次消除方塊時的時間
Font f = Font.getFont(Font.FACE_PROPORTIONAL, Font.SIZE_MEDIUM,
Font.STYLE_PLAIN); //字體
private int randomMusicIndex = 0; //隨機的音樂
private MidiPlayer mp; //播放Midi
private int limitTime = 0; //限制的時間
private int limitStep = 0; //限制的步數
public KyodaiUI(Kyodai kyodai) {
super(false);
this.kyodai = kyodai;
//隨機化地圖數據
java.util.Random random = new java.util.Random(System.currentTimeMillis());
int range = Math.abs(random.nextInt());
if (Kyodai.gameMode == 1) {
randomMusicIndex = range % (CV.MidiFiles.length);
range %= 4;
gm.setLevel(CV.MAX_ICONS - range);
}
else {
range %= (CV.MAX_ICONS - CV.MIN_ICONS);
gm.setLevel(range + CV.MIN_ICONS);
//gm.setLevel(2);
}
random = null;
System.gc();
//初始化游戲開始時的數據
gameSteps = 0;
refreshTimes = 0;
}
public void start() {
gameEnd = false;
getStartPoint(); //繪制當前的停留點
if (Kyodai.musicMode > -1 && Kyodai.musicMode < 7) { //指定了音樂
InputStream is = getClass().getResourceAsStream("/sounds/" +
CV.MidiFiles[Kyodai.musicMode]);
if (is != null) {
mp = new MidiPlayer(is);
mp.play();
}
}
else if (Kyodai.musicMode == 8) { //隨機播放
InputStream is = getClass().getResourceAsStream("/sounds/" +
CV.MidiFiles[randomMusicIndex]);
if (is != null) {
mp = new MidiPlayer(is);
mp.play();
}
}
if (Kyodai.gameMode == 1) {
limitTime = gm.getBlocks() * 4000;
limitStep = gm.getBlocks() * 6;
}
Thread t = new Thread(this);
startTime = System.currentTimeMillis();
t.start();
}
public void run() {
g = getGraphics();
drawGameMap();
while (!gameEnd) {
frames++;
if (Kyodai.gameMode == 1) {
long usedTime = System.currentTimeMillis() - startTime;
if (usedTime > limitTime ||
gameSteps > limitStep) { //判斷是否結束游戲
makeEndImage();
}
else {
updateInfo();
}
}
else {
updateInfo();
}
try {
Thread.sleep(80l);
}
catch (InterruptedException ie) {}
}
}
public void keyPressed(int keyCode) {
//System.out.println("keyCode = " + keyCode);
switch (keyCode) {
case CV.KEY_NUM2:
case CV.KEY_UP:
movePoint(CV.ARROW_UP);
break;
case CV.KEY_NUM4:
case CV.KEY_LEFT:
movePoint(CV.ARROW_LEFT);
break;
case CV.KEY_NUM6:
case CV.KEY_RIGHT:
movePoint(CV.ARROW_RIGHT);
break;
case CV.KEY_NUM8:
case CV.KEY_DOWN:
movePoint(CV.ARROW_DOWN);
break;
case CV.KEY_SELECT:
case CV.KEY_NUM5:
selectPoint();
break;
case CV.KEY_STAR:
refresh();
break;
case CV.KEY_POUND:
gameEnd = true;
if (mp != null) {
mp.stop();
}
Display.getDisplay(kyodai).setCurrent(new GameMenu(kyodai));
}
}
private void selectPoint() {
if (gameEnd) { //如果游戲已結束,返回
return;
}
if (gm.getMap()[currentPoint.x][currentPoint.y] < 1) { //如果當前的選擇點無效,返回
return;
}
if (lastSelected.x != -1 && lastSelected.y != -1) { //上次選擇的點不為空
if (GameMap.getInstance().test(lastSelected, currentPoint)) { //當前的兩點可以消除
//消除上次選擇的點
fillBorder(lastSelected, CV.GAMEBGCOLOR);
//消除此次選擇的點
fillBorder(currentPoint, CV.GAMEBGCOLOR);
//輸出緩沖區內容
flushGraphics();
//從地圖數據中移除這兩個可以消除的點
gm.earse(lastSelected, currentPoint);
//判斷連擊狀態
long time = System.currentTimeMillis() - lastDelTime;
if (time > CV.SeriesTime) { //此次沒有完成連擊
earseScore += CV.SeriesScorce[SeriesHints];
SeriesHints = 0;
}
else {
if (SeriesHints == CV.SeriesScorce.length - 1) { //達到最大連擊次數
//System.out.println("達到最大連擊次數");
earseScore += CV.SeriesScorce[SeriesHints];
SeriesHints = 0;
}
else {
SeriesHints++;
//System.out.println("連擊:" + SeriesHints);
}
}
lastDelTime = System.currentTimeMillis();
//置當前選擇點為空
lastSelected.x = -1;
lastSelected.y = -1;
System.gc();
if (gm.getCount() == 0) { //判斷用戶是否已經完成游戲
if (SeriesHints != 0) {
earseScore += CV.SeriesScorce[SeriesHints];
}
gameEnd = true;
makeWinImage();
return;
}
}
else { //當前的兩點不可消除
//消除上次選擇的背景
drawBorder(lastSelected, CV.GAMEBGCOLOR);
//選擇當前的點
if (lastSelected.x == currentPoint.x &&
lastSelected.y == currentPoint.y) { //如果上次選擇的點和當前點一樣,認為是用戶取消了選擇
lastSelected.x = -1;
lastSelected.y = -1;
}
else {
drawBorder(currentPoint, CV.SELECTEDCOLOR);
lastSelected.x = currentPoint.x;
lastSelected.y = currentPoint.y;
}
System.gc();
}
}
else { //上次的選擇點為空
drawBorder(currentPoint, CV.SELECTEDCOLOR);
lastSelected.x = currentPoint.x;
lastSelected.y = currentPoint.y;
System.gc();
}
//重新繪制停留點的邊框
drawBorder(currentPoint, CV.HOVERCOLOR);
//輸出緩沖區內容
flushGraphics();
}
private void refresh() {
if (gameEnd) { //如果游戲已結束,返回
return;
}
gm.refresh();
for (int row = 0; row < CV.ROW; row++) {
for (int col = 0; col < CV.COLUMN; col++) {
if (gm.getMap()[row][col] != 0) {
int index = gm.getMap()[row][col] - 1;
int x = CV.SPACING +
col * (CV.SPACING + CV.ICON_WIDTH);
int y = CV.SPACING +
row * (CV.SPACING + CV.ICON_HEIGHT);
Kyodai.sprites[index].setPosition(x, y);
Kyodai.sprites[index].paint(g);
}
}
}
//重新停留點的選擇框
drawBorder(lastSelected, CV.GAMEBGCOLOR);
lastSelected.x = -1;
lastSelected.y = -1;
drawBorder(currentPoint, CV.HOVERCOLOR);
flushGraphics();
refreshTimes++;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -