?? gamecanvas.java
字號:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Font;
import javax.microedition.rms.RecordStore;
import CustomlUtil.Configration;
import CustomlUtil.Monster;
import CustomlUtil.TiledLayer;
import CustomlUtil.ImageSet;
public class GameCanvas extends Canvas implements Runnable
{
//--------------定義線程--------------------------
private Thread th = null;
private GameMid GM = null;
private int i_Speed = 0;
private boolean is_Run = false;
private int i_num;
//-------------定義屏幕寬高-----------------------
private int i_lcdWidth = 0;
private int i_lcdHeight = 0;
//-------------定義屏幕中心坐標--------------------
private int i_lcdCenterX = 0;
private int i_lcdCenterY = 0;
//-------------定義地圖-----------------------
//-------共計22層,每層為11×11的大小---------------
private TiledLayer map; //游戲地圖
private byte mapArray[] = new byte[121];
byte b_star[] = {5,9,5,9,0,1,1,10,10,9,0,9,9,9,5,10,0,1,6,3,4,
6,1,10,9,10,1,10,5,9,3,0,5,0,5,8,1,10,9,10,5,4,5,5,};//每層地圖的入口
byte b_end[] = {5,1,1,0,0,9,10,9,0,9,9,9,4,9,1,0,7,4,6,7,0,9,9,
10,1,10,4,10,5,9,7,0,5,6,1,10,9,10,5,4,5,6,5,6};//每層地圖的出口
//---------定義變量表示游戲狀態--------------------
private byte SS_NOW = 0;
private byte CurrentState = 0; //當前游戲中的狀態
//---------定義英雄的相關信息---------------------
private byte HeroLv = 0; //英雄等級
private int HeroLp = 0;//英雄生命值
private int HeroAc = 0;//英雄攻擊力
private int HeroDc = 0;//英雄防御力
private int HeroExp = 0;//經驗值
private int HeroMoney = 0; //錢幣
private byte RedKey = 0;//紅鑰匙的數量
private byte YellowKey = 0;//黃鑰匙的數量
private byte BlueKey = 0;//藍鑰匙的數量
private byte Steps[] = {0,1,2,1}; //英雄在地圖上前進的幀序列
private byte stepIndex = 0;//英雄當前幀的序號
private byte b_x,b_y; //英雄在地圖數組中的位置
private byte Direction; //英雄的朝向;
private boolean is_Move = true; //定義英雄是否可以移動
private byte b_Dx,b_Dy;
//----------定義菜單選項--------------------------
private byte b_Selected = 0;
//-------------游戲中用的圖片---------------------
private Image img_Logo; //LOGO
private Image img_Title; //TITLE
private Image img_AnyKey; //PressAnyKey
private Image img_jtl; //向左箭頭
private Image img_jtr; //向右箭頭
private Image img_Hero; //英雄圖片
private Image img_Map; //地圖圖片
//-------------定義當前關卡-----------------
private byte b_Stage = 0;
private byte maxStage = 0; //到過的最高樓層;
//------------定義游戲Intro界面Y軸偏移量-------
private int offestY = 0;
private int IntroHeight = 0;
//----------定義游戲存儲對象---------
private RecordStore rs;
private boolean is_Loaded = false;//判斷游戲是否加載完畢
//----------定義計時器--------------
private long StartTime = 0;
private long UsedTime = 0;
//定義一些變量用于控制游戲情節進展
private String Message,Msg;
private boolean hasLuoPan = false; //是否得到了風之羅盤
private boolean hasBaoDian = false; //是否得到怪物寶典
private boolean is_DialogedForAngle = false; //是否與天使對過話
private byte b_DialogIndexForAngle = 0;
private boolean hasLatinCross = false; //是否拿到了十字架
private boolean is_DialogedForJack = false;//是否與JACK對過話;
private byte b_DialogIndexForJack = 0;
private boolean hasPickax = false; //是否拿到了鎬
private boolean is_DialogedForLevelUp = false; //是否與老頭NPC對話
private boolean is_DialogedForLevelUp15 = false;
private byte b_DialogIndexForLevelUp = 0;
private boolean hasExp = false;
private boolean is_DialogedForBuyKey = false; //是否與商人NPC對話
private boolean is_DialogedForBuyKey15 = false;
private byte b_DialogIndexForBuyKey = 0;
private boolean hasMoney = false;
private boolean is_DialogedForPrincess = false;//是否與公主對過話;
private byte b_DialogIndexForPrincess = 0;
private boolean is_DialogedForBeelzeBub = false;//是否與魔王對過話;
private byte b_DialogIndexForBeelzeBub = 0;
private boolean BeelzeBubIsDead = false; //是否已經殺死了魔王
public GameCanvas(GameMid GM)
{
SS_NOW = Configration.SS_LOGO;
LoadImage();
i_lcdWidth = this.getWidth();
i_lcdHeight = this.getHeight();
offestY = i_lcdHeight - 30;
i_lcdCenterX = i_lcdWidth / 2;
i_lcdCenterY = i_lcdHeight / 2;
this.GM = GM;
i_num = 1;
is_Run = true;
i_Speed = 100;
th = new Thread(this);
th.start();
}
// =====================游戲規則===========================
private boolean CanMove()
{
switch(getFront())
{
case -1://老頭NPC
if(b_Stage == 2 || b_Stage == 15)
{
DialogForLevelUp();
}
CurrentState = Configration.LEVELUP;
break;
case -2://財主NPC
if(b_Stage == 2 || b_Stage == 15)
{
DialogForBuyKey();
}
CurrentState = Configration.BUYKEY;
break;
case -3://商店NPC
CurrentState = Configration.SHOPING;
break;
case -4://天使NPC
CurrentState = Configration.ANGEL;
DialogForAngle();
break;
case -36:
CurrentState = Configration.PRINCESS;
DialogForPrincess();
break;
case -37:
CurrentState = Configration.JACK;
DialogForJack();
break;
case 77:
hasBaoDian = true;
setValue();
Msg = "得到怪物寶典,可以通過數字鍵3查看當前怪物的屬性!";
break;
case 78:
hasLuoPan = true;
setValue();
Msg = "得到風之羅盤,可以通過數字鍵1快速到達指定的樓層!";
break;
case 79:
RedKey++;
BlueKey++;
YellowKey++;
Msg = "所有鑰匙加1";
i_num = 1;
setValue();
break;
case 81:
hasPickax = true;
b_DialogIndexForJack = 0;
is_DialogedForJack = false;
setValue();
Msg = "獲得嵌著紅寶石的榔頭";
i_num = 1;
break;
case 82:
HeroMoney += 300;
Msg = "金幣增加300";
i_num = 1;
setValue();
break;
case 83:
if(YellowKey > 0)
{
YellowKey--;
setValue();
}
break;
case 84:
if(BlueKey > 0)
{
BlueKey--;
setValue();
}
break;
case 85:
if(RedKey > 0)
{
RedKey--;
setValue();
if(b_Stage == 16)
{
CurrentState = Configration.BEELZEBUB;
DialogForBeelzeBub();
}
}
break;
case 87:
YellowKey++;
setValue();
Msg = "獲得一把黃鑰匙";
i_num = 1;
break;
case 88:
BlueKey++;
setValue();
Msg = "獲得一把藍鑰匙";
i_num = 1;
break;
case 89:
RedKey++;
setValue();
Msg = "獲得一把紅鑰匙";
i_num = 1;
break;
case 90:
HeroDc += 3;
setValue();
Msg = "提升3點防御力";
i_num = 1;
break;
case 91:
HeroAc += 3;
setValue();
Msg = "提升3點攻擊力";
i_num = 1;
break;
case 92:
HeroLp += 200;
setValue();
Msg = "生命值提升200點";
i_num = 1;
break;
case 93:
HeroLp += 500;
setValue();
Msg = "生命值提升500點";
i_num = 1;
break;
case 94:
if(maxStage < b_Stage)
{
maxStage = b_Stage;
}
SaveMap();
b_Stage++;
b_x = b_star[b_Stage * 2];
b_y = b_star[b_Stage * 2 + 1];
initStage();
CurrentState = Configration.STAGECHANGED;
i_num = 1;
break;
case 95:
SaveMap();
b_Stage--;
b_x = b_end[b_Stage * 2];
b_y = b_end[b_Stage * 2 + 1];
initStage();
CurrentState = Configration.STAGECHANGED;
i_num = 1;
break;
case 96://鐵門
setValue();
break;
case 103:
HeroAc += 10;
setValue();
Msg = "攻擊提升10點";
i_num = 1;
break;
case 104:
HeroAc += 40;
setValue();
Msg = "攻擊提升40點";
i_num = 1;
break;
case 107:
HeroAc += 80;
setValue();
Msg = "攻擊提升80點";
i_num = 1;
break;
case 108:
HeroDc += 10;
setValue();
Msg = "防御提升10點";
i_num = 1;
break;
case 109:
HeroDc += 85;
setValue();
Msg = "防御提升85點";
i_num = 1;
break;
case 112:
HeroDc += 100;
setValue();
Msg = "防御提升100點";
i_num = 1;
break;
case 113:
HeroLvUp(1);
setValue();
Msg = "獲得小飛羽等級提升1";
i_num = 1;
break;
case 115:
HeroLvUp(3);
setValue();
Msg = "獲得大飛羽等級提升3";
i_num = 1;
break;
case 116:
hasLatinCross = true;
b_DialogIndexForAngle = 0;
is_DialogedForAngle = false;
setValue();
Msg = "獲得光明十字架";
i_num = 1;
break;
case 117:
HeroLp *= 2;
setValue();
Msg = "獲得超級血瓶生命值翻倍";
i_num = 1;
break;
case 120:
return true;
default:
byte f = getFront();
byte index = (byte)(-f - 5);
if(f > -36 && f < -4)
{
if(f == -15)
{
int i = HeroLp / 4;
HeroLp -= i;
if(Fight(Configration.Mn[index]))
{
setValue();
}
else
{
HeroLp += i;
}
}
else if(f == -21)
{
int i = HeroLp / 3;
HeroLp -= i;
if(b_Stage == 21)
{
index = 31;
}
if(Fight(Configration.Mn[index]))
{
setValue();
}
else
{
HeroLp += i;
}
}
else if(f == -34)
{
switch(b_Stage)
{
case 19:
CurrentState = Configration.BEELZEBUB;
DialogForBeelzeBub();
break;
case 21:
if(Fight(Configration.Mn[35]))
{
setValue();
Msg = "冥靈魔王:啊......怎么可能,我怎么可能會被你打敗呢!不,不要這樣.........";
BeelzeBubIsDead = true;
}
break;
}
}
else
{
if(f == -22 && b_Stage == 17)
{
index = 32;
}
else if(f == -25 && b_Stage == 20)
{
index = 33;
}
else if(f == -35 && b_Stage == 19)
{
index = 34;
}
if(Fight(Configration.Mn[index]))
{
setValue();
}
}
}
break;
}
return false;
}
private void CloseStore() //關閉數據存儲記錄
{
try
{
rs.closeRecordStore();
}
catch (Exception e)
{
e.printStackTrace();
}
}
private void OpenStore() //打開數據存儲記錄
{
try
{
rs = RecordStore.openRecordStore("Game1.Save",false);
}
catch (Exception e)
{
try
{
rs = RecordStore.openRecordStore("Game1.Save",true);
byte temp[] = ImageSet.LoadMap("/Map/Map.Lib", 2662);
rs.addRecord(temp, 0, temp.length);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(baos);
os.writeByte(1);
os.writeInt(1000);
os.writeInt(10);
os.writeInt(10);
os.writeInt(0);
os.writeInt(0);
os.writeByte(0);
os.writeByte(0);
os.writeByte(0);
os.writeByte(5);
os.writeByte(9);
os.writeByte(0);
os.writeLong(0);
os.writeBoolean(false);
os.writeBoolean(false);
os.writeBoolean(false);
os.writeByte(0);
os.writeBoolean(false);
os.writeBoolean(false);
os.writeByte(0);
os.writeBoolean(false);
os.writeBoolean(false);
os.writeBoolean(false);
os.writeByte(0);
os.writeBoolean(false);
os.writeBoolean(false);
os.writeBoolean(false);
os.writeByte(0);
os.writeBoolean(false);
os.writeBoolean(false);
os.writeByte(0);
os.writeBoolean(false);
os.writeByte(0);
os.writeBoolean(false);
os.close();
byte data[] = baos.toByteArray();
rs.addRecord(data, 0, data.length);
rs.addRecord(temp, 0, temp.length);
}
catch (Exception e1)
{
e1.printStackTrace();
}
}
}
private void DialogForAngle() //處理與天使的對話
{
if(!hasLatinCross)
{
if(b_DialogIndexForAngle >= Configration.DialogForAngle1.length - 1)
{
b_DialogIndexForAngle =(byte)(Configration.DialogForAngle1.length - 1);
is_Move = true;
if(!is_DialogedForAngle)
{
is_DialogedForAngle = true;
YellowKey++;
RedKey++;
BlueKey++;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -