📄 xgame.java
字号:
setSoftkey(SOFTKEY_OK,SOFTKEY_BACK);
rectWidth=width-20;
rectX=width/2-rectWidth/2;
rectHeight=height-25;
rectY=height/2-rectHeight/2;
ONE_LINE_MAX_WIDTH=rectWidth-12;
}
private void logicMenuOption(){
}
private void inputMenuOption(){
switch (inputPressed) {
case INPUT_UP:
break;
case INPUT_DOWN:
break;
case INPUT_OK:
//case INPUT_FIRE:
bSoundOn=!bSoundOn;
break;
case INPUT_CANCEL:
setState(STATE_MENU_MAIN);
break;
}
repaint=true;
}
private void paintMenuOption(Graphics g){
//drawMenuBackGround(g);
g.drawImage(imgTitleBg,0,0,Graphics.TOP|Graphics.LEFT);//Graphics.VCENTER|Graphics.HCENTER);
g.setColor(0,0,0);
String strTmp;
if(bSoundOn){
strTmp="@TXT_OPTION_SOUND@: @TXT_OPTION_ON@";
}
else{
strTmp="@TXT_OPTION_SOUND@: @TXT_OPTION_OFF@";
}
g.drawString(strTmp,(width-font.stringWidth(strTmp))/2,height/2,Graphics.TOP|Graphics.LEFT);
}
// ***************process string**********************
/*
* gtkBreakString
* @para s: break the s into array
* @para maxLen: the maxle of the string in array
*/
public String[] gtkBreakString(String s, int maxWidth) {
Vector temp = new Vector();
int beginIndex = 0;
int endIndex = 0;
while (endIndex < s.length()) {
beginIndex = endIndex;
while (s.charAt(beginIndex) == '/') {
beginIndex++;
//if (s.charAt(beginIndex) == '/') {
// beginIndex++;
//}
}
endIndex = gtkBreakPoint(s, beginIndex, maxWidth);
temp.addElement(s.substring(beginIndex, endIndex));
if ((endIndex < s.length()) && (s.charAt(endIndex) == '/')) {
endIndex++;
}
}
String[] result = new String[temp.size()];
for (int i = 0; i < result.length; i++) {
result[i] = (String) (temp.elementAt(i));
//XMain.pInfo("result["+i+"]:"+result[i]);
}
return result;
}
private int gtkBreakPoint(String s, int beginIndex, int maxWidth) {
int result = beginIndex;
int nextLine = s.indexOf('/', beginIndex);
if(font.substringWidth(s, beginIndex, s.length()
- beginIndex) > maxWidth){
if(nextLine==-1){
while(font.substringWidth(s, beginIndex, result
- beginIndex)<maxWidth){
result++;
}
}
else if(font.substringWidth(s, beginIndex, nextLine
- beginIndex)<maxWidth){
result=nextLine;
}
else{
while(font.substringWidth(s, beginIndex, result
- beginIndex)<maxWidth){
result++;
}
}
}
else{
if(nextLine==-1){
result=s.length();
}
else {
result=nextLine;
}
}
return result;
}
//***************end of process string **************
private void drawMenuBackGround(Graphics g){
g.setColor(0,0,0);
g.fillRect(0,0,width,height);
}
//************************
//LevelSuccess
//************************
private final static int PEARL_VALUE=10;
private final static int STAR_VALUE=20;
private int pearlScore,starScore;
private void enterLevelSuccess(){
unloadImage();
loadImage();
setSoftkey(SOFTKEY_OK,SOFTKEY_NONE);
counter=0;
bCanContinueGame=false;
saveRecord();
}
private void inputLevelSuccess(){
switch (inputPressed) {
case INPUT_OK://restart Game
if(counter>20){
//showInfo("input:");
//gameLevel++;
//startGame();
if(gameLevel==7){
setState(STATE_GAME_PASS);
}
else{
nextLevel();
setState(STATE_GAME_START);
}
}
break;
}
//case
}
private void logicLevelSuccess(){
if(gameScore<levelScore){
if(levelScore-gameScore>10){
gameScore+=10;
}
else{
gameScore++;
}
}
repaint=true;
}
private void paintLevelSuccess(Graphics g){
drawMenuBackGround(g);
// mermaied head
g.setColor(0,102,157);
int posX,posY;
posX=rectX+(width/2-60);
posY=rectY+20;
String strTmp;
}
//******************************
//level failure
//****************************
private void enterLevelFailure(){
unloadImage();
loadImage();
setSoftkey(SOFTKEY_OK,SOFTKEY_BACK);
counter=0;
}
private void inputLevelFailure(){
switch (inputPressed) {
case INPUT_OK://restart Game
if(counter>18){
isHighScore(gameScore);
bCanContinueGame=false;
saveRecord();
gameScore=0;
bCanContinueGame=false;
startLevel();
setState(STATE_GAME_START);
}
break;
case INPUT_CANCEL:// go to menu
if(counter>18){
isHighScore(gameScore);
bCanContinueGame=false;
saveRecord();
// for(int i=0;i<highScore.length;i++){
// showInfo("inputLevelFailure highScore["+i+"]:"+highScore[i]);
// }
//menuType=0;
if(bLoaded){//can continue game
gameScore=0;
menuType=2;
}
else{//just restart game
gameScore=0;
menuType=0;
}
setState(STATE_MENU_MAIN);
}
break;
}
}
private void logicLevelFailure(){
if(gameScore<levelScore){
if(levelScore-gameScore>10){
gameScore+=10;
}
else{
gameScore++;
}
}
repaint=true;
}
private void paintLevelFailure(Graphics g){
drawMenuBackGround(g);
}
//***************
//GamePass . pass all level
//***************
private final static int STAFF_WIDTH=114,STAFF_HEIGHT=27;
private void enterGamePass(){
unloadImage();
//loadObject(OBJECT_MENU_IMG);
loadImage();
setSoftkey(SOFTKEY_OK,SOFTKEY_NONE);
counter=0;
}
private void inputGamePass(){
if(counter>10){
//if the game socre is a high score then show the score table
gameLevel=1;
scoreType=isHighScore(gameScore);
//showInfo("Game Pass gameScore"+gameScore);
// for(int i=0;i<highScore.length;i++){
// showInfo("inputLevelFailure highScore["+i+"]:"+highScore[i]);
// }
menuType=0;
bCanContinueGame=false;
setState(STATE_MENU_SCORE);
//else goto the main menu;
}
}
private int frameSing;
private void logicGamePass(){
frameSing=(frameSing+1)%8;
repaint=true;
}
private void paintGamePass(Graphics g){
g.setClip(0,0,width,height);
g.drawImage(imgTitleBg,0,0,Graphics.TOP|Graphics.LEFT);//Graphics.VCENTER|Graphics.HCENTER);
}
private void leaveGamePass(){
//showInfo("leave game pass:");
#if(${ant.handset_sound}!="none")
deleteMusic(musicPlayer);
#end
}
// ************************************
// record
//init save load
// ************************************
//Enemy save:
//1B:Type; 4B:xPos; 4B:yPos; 1B:motion;
//Prop save:
//1B:Type; 4B:xPos; 4B:yPos;
//Plant svae:
//1B:type ; 4B:xPos; 4B: yPos; 1B :motion;
private final static int RECORD_SAVE_GAME_SCORE=0;
private final static int RECORD_SAVE_HIGH_SCORE=RECORD_SAVE_GAME_SCORE+4;
private final static int RECORD_SAVE_PLAY_LEVEL=RECORD_SAVE_HIGH_SCORE+4*5;
private final static int RECORD_SAVE_SOUND=RECORD_SAVE_PLAY_LEVEL+4;//
private final static int RECORD_SAVE_INFOR_LENGTH=RECORD_SAVE_SOUND+1;
byte [][] recordInfo=new byte[1][RECORD_SAVE_INFOR_LENGTH];
// save and load record
// Map.xBeginPosInMap=0;
// Map.yBeginPosInMap
// levelsCompleted
private void saveRecord(){
//mermaid.xPos;
//mermaid.yPos;
//objectGameMermaid
// game score
if(bCanContinueGame){
recordInfo[0][RECORD_SAVE_PLAY_LEVEL]=(byte)gameLevel;
//game score
gtkIntToBytes(gameScore,recordInfo[0],RECORD_SAVE_GAME_SCORE);
}
// high score
for(int i=0;i<highScore.length;i++){
gtkIntToBytes(highScore[i],recordInfo[0],RECORD_SAVE_HIGH_SCORE+i*4);
//showInfo("save: highScore["+i+"]"+highScore[i]);
}
// Sound
//showInfo("save: gameLevel:"+gameLevel+" byte:"+recordInfo[0][RECORD_SAVE_PLAY_LEVEL]);
if(bSoundOn){
recordInfo[0][RECORD_SAVE_SOUND]=(byte)1;
}
else{
recordInfo[0][RECORD_SAVE_SOUND]=(byte)0;
}
gtkRecordSave(recordInfo,0);
}
private boolean bLoaded;
private void loadRecord(){
inputPressed=INPUT_NONE;
gtkRecordLoad(recordInfo,0);
//showInfo("Load: (byte)recordInfo[0][RECORD_SAVE_PLAY_LEVEL]:"+(byte)recordInfo[0][RECORD_SAVE_PLAY_LEVEL]);
gameLevel=(byte)recordInfo[0][RECORD_SAVE_PLAY_LEVEL];
showInfo("recordInfo length:"+recordInfo[0].length);
//get high score
for(int i=0;i<highScore.length;i++){
highScore[i]=gtkBytesToInt(recordInfo[0],RECORD_SAVE_HIGH_SCORE+i*4);
//showInfo("highScore["+i+"]:"+highScore[i]);
}
if(gameLevel<=0){
bCanContinueGame=false;
bLoaded=false;
gameScore=0;
}
else{
bCanContinueGame=true;
bLoaded=true;
// game score
gameScore=gtkBytesToInt(recordInfo[0],RECORD_SAVE_GAME_SCORE);
}
//showInfo("load recod :recordInfo[0][RECORD_SAVE_SOUND]"+recordInfo[0][RECORD_SAVE_SOUND]);
if(recordInfo[0][RECORD_SAVE_SOUND]==1){
bSoundOn=true;
}
else{
bSoundOn=false;
}
}
public byte[][] gtkRecordInitData() {
showInfo("init RECORD_SAVE_INFOR_LENGTH:"+RECORD_SAVE_INFOR_LENGTH);
// interface, record init data
byte [][] recordInfo=new byte[1][RECORD_SAVE_INFOR_LENGTH];
recordInfo[0][RECORD_SAVE_SOUND]=1;//set the sound default is true
return recordInfo;
}
public void gtkRecordLoad(byte[][] data, int start) {
showInfo("gtkRecordLoad RECORD_SAVE_INFOR_LENGTH:"+RECORD_SAVE_INFOR_LENGTH);
showInfo("data length1:"+data[0].length);
RecordStore rs = null;
try {
rs = RecordStore.openRecordStore("xx", false);
showInfo("data length111:"+data[0].length);
for (int i = 0; i < data.length; i++) {
data[i] = rs.getRecord(start + 1 + i);
}
showInfo("data length122:"+data[0].length);
} catch (RecordStoreNotFoundException e) {
byte[][] init = gtkRecordInitData();
for (int i = 0; i < data.length; i++) {
data[i] = init[start + i];
}
gtkRecordInit(init);
//e.printStackTrace();
} catch (RecordStoreException e) {
#if(!$ant.build_dist)
showError(e);
#end
//e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.closeRecordStore();
}
} catch (Exception e) {
#if(!$ant.build_dist)
showError(e);
#end
//e.printStackTrace();
}
}
//showInfo("data length2:"+data[0].length);
}
public void gtkRecordSave(byte[][] data, int start) {
RecordStore rs = null;
try {
rs = RecordStore.openRecordStore("xx", false);
for (int i = 0; i < data.length; i++) {
rs.setRecord(start + 1 + i, data[i], 0, data[i].length);
}
} catch (RecordStoreNotFoundException e) {
byte[][] init = gtkRecordInitData();
for (int i = 0; i < data.length; i++) {
init[start + i] = data[i];
}
gtkRecordInit(init);
} catch (RecordStoreException e) {
#if(!$ant.build_dist)
showError(e);
#end
} finally {
try {
if (rs != null) {
rs.closeRecordStore();
}
} catch (Exception e) {
#if(!$ant.build_dist)
showError(e);
#end
}
}
}
private void gtkRecordInit(byte[][] data) {
RecordStore rs = null;
try {
rs = RecordStore.openRecordStore("xx", true);
for (int i = 0; i < data.length; i++) {
rs.addRecord(data[i], 0, data[i].length);
}
} catch (RecordStoreException e) {
#if(!$ant.build_dist)
showError(e);
#end
} finally {
try {
if (rs != null) {
rs.closeRecordStore();
}
} catch (Exception e) {
#if(!$ant.build_dist)
showError(e);
#end
}
}
}
public static void gtkIntToBytes(int i, byte[] b, int offset) {
b[offset + 0] = (byte) ((0xFF000000 & i) >> 24);
b[offset + 1] = (byte) ((0x00FF0000 & i) >> 16);
b[offset + 2] = (byte) ((0x0000FF00 & i) >> 8);
b[offset + 3] = (byte) ((0x000000FF & i));
}
public static int gtkBytesToInt(byte[] b, int offset) {
return ((0xFF & b[offset]) << 24) | ((0xFF & b[offset + 1]) << 16)
| ((0xFF & b[offset + 2]) << 8) | (0xFF & b[offset + 3]);
}
public static void gtkShortToBytes(short i, byte[] b, int offset) {
b[offset + 0] = (byte) ((0x0000FF00 & i) >> 8);
b[offset + 1] = (byte) ((0x000000FF & i));
}
public static short gtkBytesToShort(byte[] b, int offset) {
return (short) (((0xFF & b[offset]) << 8) | (0xFF & b[offset + 1]));
}
//***************end of record************************
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -