📄 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 + -