?? game.java
字號:
package com.j2medev.chapter5;
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.rms.*;
/**
* @name Game
* @author liloi <liloi@hotmail.com>
* @version 2.0
*/
//#if ( NOKIA_S60_V1 || NOKIA_S60_V2 || NOKIA_S40_V1 || NOKIA_S40_V2)
//# public class Game extends com.nokia.mid.ui.FullCanvas implements Runnable,
//# MyGameInterface
//#else
public class Game
extends Canvas
implements Runnable, MyGameInterface
//#endif
{
//////////////////////////////////////////////////////////////////////////////
// Variables
//////////////////////////////////////////////////////////////////////////////
//#if MOTO_E680
//# public Image backImage = Image.createImage(SCREEN_WIDTH, SCREEN_HEIGHT);
//# public Graphics backG = backImage.getGraphics();
//#else
public Graphics backG;
//#endif
public Main m_Midlet; // Midlet類
protected boolean bSelectKeyDown; //在菜單動畫狀態下是否有選擇鍵按下
protected static final int REPAINT_DELAY = 64; //游戲的延遲(一楨動畫播放的時間單位毫秒)
public static int SCREEN_W_K700 = 176;
public static int SCREEN_H_K700 = 220;
/**
* 動畫變量
* 用于播放動畫編輯器制作的動畫
*/
protected PieceSprite m_DemoAnimation; //教學動畫
protected PieceSprite m_ReadyAnimation; //準備動畫
protected PieceSprite m_MenuFrameAnimation; //菜單動畫
protected PieceSprite m_CoverAnimation; //封面動畫
/**
* 聲音變量
* 用于播放聲音
*/
private MySound mySound; // 聲音類
public int soundLevel = 3; // 聲音高低
public boolean sSoundOpen = true; // 是否播放聲音
/**各種全局變量值*/
private byte gameState; // 當前游戲狀態
private int totalBonus; // 總得分
private boolean isPass; // 是否過關
public Map myMap; // 地圖對象
public int mapBase; // 地圖移動基準
public Role thisRole; // 當前角色對象
public Role roleBoy; // 創建男孩角色對象
public Role roleGirl; // 創建女孩角色對象
public Role roleNinja; // 創建忍者角色對象
int wordWidth = 0;
/**
*繪制雨或雪時用的參數
*/
//繪制雨或雪的2個方位的位置
short SnowPosx[];
short SnowPosY[];
//雨雪的尺寸
short SnowSize[];
// 用于控制是否下雨或下雪
private Random randSnow = new Random(System.currentTimeMillis());
// 用于控制雨雪量的多少
private Random randSnowCount = new Random(System.currentTimeMillis());
private int currentSnow;
/**獎品、工具、障礙和敵人的存儲隊列*/
PVector bonusVector = new PVector();
PVector toolVector = new PVector();
PVector fraiseVector = new PVector();
PVector enemyVector = new PVector();
/**各種圖片*/
private Image imgLogo1; //移動公司圖片
private Image imgLogo2; //摩動時代圖片
private Image imgCover; //封面圖片
private Image imgLoading; //讀取圖片
private Image imgMenu; //菜單圖片
private Image imgAbout; //關于圖片
private Image[] imgRoleHead = new Image[3]; // 角色的頭部圖片
private Image thisImgRoleHead; //當前頭部圖片
private Image imgSkateboard; // 滑板圖片
private Image imgForeground; // 前景圖片
private Image imgDecTop; // 頂層裝飾圖片
private Image imgDecBottom; // 底層裝飾圖片
private Image[] imgFraise = new Image[8]; // 障礙物圖片
private Image[] imgBonus = new Image[6]; // 獎品圖片
private Image[] imgTool = new Image[3]; // 工具圖片
private Imagex[] imgxEnemy = new Imagex[3]; // 敵人圖片
private Imagex[] imgxEnemyDead = new Imagex[3]; // 敵人受傷圖片
private ImageNumber inNumber; // 用于顯示數字的對象
//背景的四層結構
private Image imgSky; //天空圖片(放在最后一層)
private Image imgCloud; //云彩圖片(在天空前面)
private Image imgBuilding; //建筑物圖片(在云彩前面)
private Image imgLand; //涂鴉墻圖片(在建筑物前面)
/**背景滾動寬度*/
private int bgRockWidth = 0;
/**建筑物滾動寬度*/
private int bgBuildWidth = 0;
/**云彩滾動速度(游戲中有3朵云彩且速度不一樣)*/
private int[] bgCloudWidth = new int[3];
/**前景滾動寬度*/
private int fgRockWidth = 0;
/**線程相關*/
private boolean threadLoop; // 線程是否繼續
/**其它*/
private int loadingGuage = 0;
private int loadingGuage2 = 0;
private int MacroSpeed; // 整個游戲移動的速度
private byte currentScreen; // 當前關卡(0~5分別對應6個不同關卡;6~9對應高級關卡)
private byte currentRole; // 當前角色(在本游戲中與小局一一對應)
protected Vector m_Text;
private int V600_WORD_HEIGHT;
/** 各種字體 */
protected Font m_MenuFont;
protected Font m_HelpFont;
protected Font m_PressAnyKeyFont;
protected Font m_CreditsFont;
protected Font m_HiscoreFont;
/*幫助菜單變量*/
private int helpLines;
private int helpPages;
private int currPage;
private int m_CursorSmallDIS;
private int m_LastPosition;
private int m_Position; //used for title position indication
public boolean m_Paused = false; //pause flag
public long m_LastTime; //used for timer/delay tasks
private Imagex imgx_selector = new Imagex();
private Imagex imgx_MenuItems = new Imagex();
/*記錄存取*/
private RecordStore recordStore;
//記錄存儲數組
public byte SaveData[];
/////////////////////////////////
/// General look&feel ///
/////////////////////////////////
protected int m_Position_pop;
protected Font m_Font;
protected Font m_BoldFont;
protected Font m_PlainFont;
protected Image m_BufferedImage;
protected Image m_FadeImage;
/////////////////////////////////////////////////////////
/// Methods ///
/////////////////////////////////////////////////////////
/**
* 構造函數
* @param d Display reference
* @return none
*/
public Game() {
//#if ( NOKIA_S60_V1 || NOKIA_S60_V2|| NOKIA_S40_V1 || NOKIA_S40_V2)
//#
//#else
setFullScreenMode(true); //設置全屏
//#endif
/* set paused mode */
m_Paused = false; //用于控制循環是否暫停
/* 初始化天氣 */
SnowPosx = new short[50];
SnowPosY = new short[50];
SnowSize = new short[50];
for (int i = 0; i < 50; i++) { //繪制雨,向左向右
SnowPosx[i] = (short) (Math.abs(randSnow.nextInt()) % SCREEN_WIDTH);
SnowPosY[i] = (short) (Math.abs(randSnow.nextInt()) % SCREEN_HEIGHT);
SnowSize[i] = (short) (Math.abs(randSnow.nextInt()) % 3 + 1);
}
/* 初始化聲音 */
mySound = new MySound();
mySound.setSoundOpen(true);
mySound.setSoundLoopCount(0, -1);
mySound.setSoundLevel(100);
//#if MOTO_E398||MOTO_V600
//# V600_WORD_HEIGHT = 5;
//#else
V600_WORD_HEIGHT = 0;
//#endif
/* start us as thread */
threadLoop = true; // 控制線程循環
new Thread(this).start();
}
/**
* 存儲游戲數據
*/
public void WriteData() {
try {
SaveData[0] = currentScreen; // 存儲當前關
SaveData[1] = currentRole; // 存儲當前人物(亦即當前小局)
recordStore = RecordStore.openRecordStore("skateBoard_data", true);
if (recordStore.getNumRecords() == 0) {
recordStore.addRecord(SaveData, 0, 2);
}
else {
recordStore.setRecord(1, SaveData, 0, 2);
}
}
catch (Exception e) {
e.printStackTrace();
}
System.gc();
}
/**
* 讀取游戲數據
*/
protected void ReadData() {
try {
recordStore = RecordStore.openRecordStore("skateBoard_data", true);
if (recordStore.getNumRecords() == 0) {
return;
}
}
catch (Exception e) {
e.printStackTrace();
System.out.println("Error");
}
try {
SaveData = recordStore.getRecord(1);
currentScreen = SaveData[0];
currentRole = SaveData[1];
}
catch (Exception e1) {
e1.printStackTrace();
}
System.gc();
}
/**
* 按鍵按下的相應
* @brief indicate, that key has been pressed
* @param keyCode key code of the key, which state is to be changed
*/
protected void keyPressed(int keyCode) {
switch (gameState) {
case STATE_DEMO:
switch (keyCode) {
case KEY_2:
case KEY_UP:
case KEY_8:
case KEY_DOWN:
case KEY_5:
case KEY_FIRE:
case SOFT_KEY_LEFT:
changeState(STATE_MAINMENU);
break;
}
break;
case STATE_MAINMENU:
checkKeys(MENU_MAIN, true, keyCode);
break;
case STATE_OPTIONSMENU:
checkKeys(MENU_OPTIONS, false, keyCode);
if (keyCode == KEY_5 || keyCode == KEY_FIRE || keyCode == SOFT_KEY_LEFT) {
switch (m_Position) {
case 0:
mySound.setSoundOpen(true);
break;
case 1:
mySound.setSoundOpen(false);
break;
case 2:
if (bSelectKeyDown == false && m_CursorSmallDIS == 0 &&
bGameMenuAnimationOver()) {
bSelectKeyDown = true;
m_MenuFrameAnimation.setAnimation(1, false);
}
break;
}
}
break;
case STATE_GAMEMENU:
checkKeys(MENU_GAME, true, keyCode);
break;
case STATE_HELP:
switch (keyCode) {
case KEY_UP:
case KEY_2:
m_Position -= HELP_LINES;
if (m_Position < 0) {
m_Position = 0;
}
break;
case KEY_DOWN:
case KEY_8:
m_Position += HELP_LINES;
if (m_Position > helpLines - HELP_LINES) {
m_Position = (helpPages - 1) * HELP_LINES;
}
break;
case SOFT_KEY_RIGHT:
if (bSelectKeyDown == false && bGameMenuAnimationOver()) {
bSelectKeyDown = true;
m_MenuFrameAnimation.setAnimation(1, false);
}
break;
}
break;
case STATE_CREDITS:
if (bSelectKeyDown == false && bGameMenuAnimationOver()) {
bSelectKeyDown = true;
m_MenuFrameAnimation.setAnimation(1, false);
}
break;
case STATE_GAMEREADY:
if (bGameReadyAnimationOver()) {
m_Paused = false;
changeState(STATE_GAME);
}
else {
m_ReadyAnimation.currentFrameNr = (short) (m_ReadyAnimation.
currentAnimation.length - 2);
}
break;
case STATE_GAME:
switch (keyCode) {
case SOFT_KEY_RIGHT:
if (thisRole.getRoleStatus() != ROLESTATUS_INJURE) {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -