?? kyodaiui.java
字號(hào):
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); //當(dāng)前光標(biāo)停留的位置
Point lastSelected = new Point(); //上一次選擇的位置
boolean gameEnd = true; //當(dāng)前游戲是否已經(jīng)結(jié)束
int gameSteps; //步數(shù)
int refreshTimes; //刷新地圖的次數(shù)
long startTime = 0l; //為難度級(jí)別準(zhǔn)備的定時(shí)器
long frames = 0l; //幀數(shù)
private int SeriesHints = 0; //連擊次數(shù)
private int earseScore = 0; //消除方塊的分?jǐn)?shù)
private long lastDelTime = 0; //上次消除方塊時(shí)的時(shí)間
Font f = Font.getFont(Font.FACE_PROPORTIONAL, Font.SIZE_MEDIUM,
Font.STYLE_PLAIN); //字體
private int randomMusicIndex = 0; //隨機(jī)的音樂(lè)
private MidiPlayer mp; //播放Midi
private int limitTime = 0; //限制的時(shí)間
private int limitStep = 0; //限制的步數(shù)
public KyodaiUI(Kyodai kyodai) {
super(false);
this.kyodai = kyodai;
//隨機(jī)化地圖數(shù)據(jù)
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();
//初始化游戲開(kāi)始時(shí)的數(shù)據(jù)
gameSteps = 0;
refreshTimes = 0;
}
public void start() {
gameEnd = false;
getStartPoint(); //繪制當(dāng)前的停留點(diǎn)
if (Kyodai.musicMode > -1 && Kyodai.musicMode < 7) { //指定了音樂(lè)
InputStream is = getClass().getResourceAsStream("/sounds/" +
CV.MidiFiles[Kyodai.musicMode]);
if (is != null) {
mp = new MidiPlayer(is);
mp.play();
}
}
else if (Kyodai.musicMode == 8) { //隨機(jī)播放
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) { //判斷是否結(jié)束游戲
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) { //如果游戲已結(jié)束,返回
return;
}
if (gm.getMap()[currentPoint.x][currentPoint.y] < 1) { //如果當(dāng)前的選擇點(diǎn)無(wú)效,返回
return;
}
if (lastSelected.x != -1 && lastSelected.y != -1) { //上次選擇的點(diǎn)不為空
if (GameMap.getInstance().test(lastSelected, currentPoint)) { //當(dāng)前的兩點(diǎn)可以消除
//消除上次選擇的點(diǎn)
fillBorder(lastSelected, CV.GAMEBGCOLOR);
//消除此次選擇的點(diǎn)
fillBorder(currentPoint, CV.GAMEBGCOLOR);
//輸出緩沖區(qū)內(nèi)容
flushGraphics();
//從地圖數(shù)據(jù)中移除這兩個(gè)可以消除的點(diǎn)
gm.earse(lastSelected, currentPoint);
//判斷連擊狀態(tài)
long time = System.currentTimeMillis() - lastDelTime;
if (time > CV.SeriesTime) { //此次沒(méi)有完成連擊
earseScore += CV.SeriesScorce[SeriesHints];
SeriesHints = 0;
}
else {
if (SeriesHints == CV.SeriesScorce.length - 1) { //達(dá)到最大連擊次數(shù)
//System.out.println("達(dá)到最大連擊次數(shù)");
earseScore += CV.SeriesScorce[SeriesHints];
SeriesHints = 0;
}
else {
SeriesHints++;
//System.out.println("連擊:" + SeriesHints);
}
}
lastDelTime = System.currentTimeMillis();
//置當(dāng)前選擇點(diǎn)為空
lastSelected.x = -1;
lastSelected.y = -1;
System.gc();
if (gm.getCount() == 0) { //判斷用戶是否已經(jīng)完成游戲
if (SeriesHints != 0) {
earseScore += CV.SeriesScorce[SeriesHints];
}
gameEnd = true;
makeWinImage();
return;
}
}
else { //當(dāng)前的兩點(diǎn)不可消除
//消除上次選擇的背景
drawBorder(lastSelected, CV.GAMEBGCOLOR);
//選擇當(dāng)前的點(diǎn)
if (lastSelected.x == currentPoint.x &&
lastSelected.y == currentPoint.y) { //如果上次選擇的點(diǎn)和當(dāng)前點(diǎn)一樣,認(rèn)為是用戶取消了選擇
lastSelected.x = -1;
lastSelected.y = -1;
}
else {
drawBorder(currentPoint, CV.SELECTEDCOLOR);
lastSelected.x = currentPoint.x;
lastSelected.y = currentPoint.y;
}
System.gc();
}
}
else { //上次的選擇點(diǎn)為空
drawBorder(currentPoint, CV.SELECTEDCOLOR);
lastSelected.x = currentPoint.x;
lastSelected.y = currentPoint.y;
System.gc();
}
//重新繪制停留點(diǎn)的邊框
drawBorder(currentPoint, CV.HOVERCOLOR);
//輸出緩沖區(qū)內(nèi)容
flushGraphics();
}
private void refresh() {
if (gameEnd) { //如果游戲已結(jié)束,返回
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);
}
}
}
//重新停留點(diǎn)的選擇框
drawBorder(lastSelected, CV.GAMEBGCOLOR);
lastSelected.x = -1;
lastSelected.y = -1;
drawBorder(currentPoint, CV.HOVERCOLOR);
flushGraphics();
refreshTimes++;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -