?? rpgcanvas.java
字號:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Random;
import java.util.Vector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.media.MediaException;
public class RPGCanvas extends GameCanvas implements Runnable, CommandListener {
private int screenwidth = getWidth(); //屏幕寬
private int screenheight = getHeight(); //屏幕高
private int imgbackgroundwidth = 512; //地圖背景寬
private int imgbackgroundheight = 512; //地圖背景高
Thread t; //游戲線程
Random r; //隨機數
RPGMidlet midlet;
Vector vectormessage; //保存會話內容
Vector vectorpicture; //保存圖畫
LayerManager lmscene1, lmscene2; //場景一,二管理對象
String missionmessage, curmessage; //任務消息和當前人物會話內容
Mainrole mrole; //游戲主角色
Load load; //加載游戲畫面
Graphics g;
Image curpicture;
private boolean catchcat = false; //判斷與貓的接觸
private boolean catchcat2 = false;
private boolean showyesorno = false; //是否顯示會話內容
private boolean mainroleanswer = false; //人物回答
private boolean iscollide = false; //是否與老人接觸了
private int scene = 1; // check for scene //場景
private int inputmode = 0; //輸入模式
int screenx, screeny, lastscreenx, lastscreeny; //屏幕當前坐標 和上次移動的坐標大小
int tempx = 0, tempy = 0;
int selecty = 152; //當進行模式選擇時,選擇光標的位置
int key; //獲得鍵盤狀態
static int mroleProperty[]; //人物屬性數組
private int roleBloodHeight = 40; //人物血氣的長度
private boolean ismessage = false; //npc人物是否顯示對話
private boolean firstTouch = true; //完成尋找貓后,第一次與老人接觸后為false
private boolean isbeat = false; //判斷是否與野怪接觸
public static final int GAME_MENU = 0; //游戲菜單狀態
public static final int GAME_START = 1; //游戲進行狀態
public static final int GAME_PAUSE = 2; //游戲停止狀態
public static final int GAME_ABOUT = 8; //游戲介紹狀態
private static final int MENU_START = 3; //菜單:進入游戲
private static final int MENU_LOAD = 4; //菜單:載入進度
private static final int MENU_VOL = 5; //菜單:聲音播放
private static final int MENU_QUIT = 6;// 菜單: 退出
private static final int MENU_ABOUT = 7; //菜單:關于游戲
private static final int MENU_SAVE = 8;// 菜單:保存游戲
// public static int DEFAULT_FPS = 10;
//
// private int interval = 1000 / DEFAULT_FPS;
public Command exitGame = new Command("退出", Command.EXIT, 1);
public Command mainMenu = new Command("菜單", Command.OK, 1);
public Command backMenu = new Command("返回菜單", Command.BACK, 2);
public Command backGame = new Command("返回游戲", Command.BACK, 2);
private Font font = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD,
Font.SIZE_SMALL);
private int fHeight = font.getHeight();
private int gameAboutY = 0;
public SaveStateRecord record = null;
public AudioPlayer bgPlayer;
// public AudioPlayer beatPlayer;
public boolean isVolOn = false;
private boolean isFirstStart; //判斷是否剛啟動游戲,尚未進入游戲狀態
private boolean gameOver = false; //游戲結束標志
private boolean isBoss = false; //是否與boss接觸
private boolean isSoldier = false; //是否與野怪接觸
private boolean isAttack = false; // 是否攻擊選擇
private int number; //判斷接觸的是第幾個野怪2,3,4分表表示第一,二,三個野怪
private String soldierWord; //存放野怪或boss對話內容
private int blood; //存放野怪或boss所出招數引起的血氣減少量
private int attackWidth; //每幀招數圖片的寬
private int attackHeight; //每幀招數圖片的高
private String roleWord = ""; //人物對話內容
private boolean roleAttack = false; // 人物招數選擇
private int attackSelecty = screenwidth - 35; //有三項選擇時,游戲光標位置
private int tempNum; //隨機數
private boolean roleBeat = false; //人物放招畫面
private boolean isBossDead = false; //判斷boss死亡
private boolean isFood = false; //判斷與食物接觸
private boolean isSword = false; //判斷與寶劍接觸
private boolean isChoose = false; //判斷是否攻擊boss
private boolean isBossAttack = false; //boss攻擊
private int bossBlood = 30; //boss血氣
private boolean warEnd = false; //判斷與boss戰斗是否結束
private int randX, randY; //隨機獲得坐標,用來隨機產生道具
private int gameState; //游戲狀態
private int menuState; //菜單狀態
private String choose1, choose2, choose3; //選擇項
private boolean isGold = false; //是否與黃金碰撞
private boolean isSeller = false; //是否與商人碰撞
private boolean isBuy = false; //是否買
private boolean isSell = false; //是否賣
private boolean forBuy = false; //選擇買
private boolean forSell = false; //選擇賣
private boolean sellerAsk = false; //商人詢問買賣
private boolean roleChoose = false; //人物是否選擇買賣
private int tempH = 0; //游戲介紹的總高度
private String pMessage;
private boolean isProperty = false;
private Image pImage = null;
private boolean isJump = false;
private boolean isSelectAttack = false;
private boolean isRoleAttack = false;
protected RPGCanvas(RPGMidlet midlet) { // 構造函數,初始類
super(true);
this.midlet = midlet;
g = getGraphics();
r = new Random();
// t = new Thread(this);
mroleProperty = new int[] { 0, 0, 0, 0, 0 }; // 分別表示等級,寶物,完成任務數,血氣,寶物
// mroleProperty = new int[] { 1, 1, 1, 1, 1 }; // 為什么只有mroleproperty【4】才等于1
vectormessage = new Vector();
vectorpicture = new Vector();
load = new Load();
curmessage = null;
missionmessage = null;
curpicture = null;
if (scene == 1)
mrole = new Mainrole(load.imgrole, 16, 24, 50, 50, 80, 153);
else if (scene == 2) {
mrole = new Mainrole(load.imgrole, 16, 24, 80, 153);
inputmode = 2;
}
lmscene1 = new LayerManager();
lmscene2 = new LayerManager();
setCommandListener(this);
record = new SaveStateRecord("RPGRecord2");
bgPlayer = new AudioPlayer("bg.mid", "audio/midi");
// beatPlayer = new AudioPlayer("beat.mmf", "application/vnd.smaf");
isFirstStart = true;
gameState = GAME_MENU;
menuState = MENU_START;
try {
// addendshowtalk();
load.p.start();
} catch (MediaException e) {
e.printStackTrace();
}
// t.start();
}
public void stop() {
t = null;
}
public void start(){
t = new Thread(this); //為什么需要加上對象參數this??當無this時無游戲畫面
t.start();
System.out.println("Thread Start!");
}
//產生隨機數
int getrand(int m) {
int n = r.nextInt();
if (n < 0)
n *= -1;
n %= m;
return n;
}
public void screenmove(int increasex, int increasey) { // 屏幕移動函數
screenx = screenx + increasex;
screeny = screeny + increasey;
lastscreenx = increasex;
lastscreeny = increasey;
}
void screenunmove() {// 屏幕負移動函數
screenmove(-lastscreenx, -lastscreeny);
}
void draw() { // 繪畫函數
// 清屏
if (!gameOver) {
// 設置window
if (!isAttack && !roleAttack && !roleBeat && !isSelectAttack) {
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
if (scene == 1) { //加載場景一
lmscene1.setViewWindow(screenx, screeny, screenwidth,screenheight);
append();
if(isBossDead)load.property[1].setVisible(false);
lmscene1.paint(g, 0, 0);
drawProperty();
}
else if (scene == 2) { //加載場景2
mrole.setPosition(mrole.roomx, mrole.roomy);
lmscene2.setViewWindow(0, 0, screenwidth, screenheight);
append2();
lmscene2.paint(g, 0, 0);
}
}
if (missionmessage != null) {
g.setColor(255, 255, 255);
g.drawString(missionmessage, 0, screenheight, g.BOTTOM| g.LEFT);
}
// 顯示對話
drawmessage();
if (showyesorno == true) {
drawlist(); //顯示選擇框
}
if (sellerAsk) { //商人詢問框 :買,賣,放棄
g.setColor(0, 0, 100);
g.fillRect(0, getHeight() - 40, getWidth(), 40);
g.setColor(255, 255, 128);
g.drawImage(load.sellerbig, 0, screenheight - 40, g.TOP
| g.LEFT);
g.setColor(255, 255, 255);
g.drawString(choose1, 60, getHeight() - 40, 0);
g.drawString(choose2, 60, getHeight() - 30, 0);
g.drawString(choose3, 60, screenheight - 20, 0);
g.setColor(255, 255, 0);
// g.fillArc(45, attackSelecty, 5, 5, 0, 360);
g.drawImage(load.selectIcon,40, attackSelecty ,g.TOP|g.LEFT );
Font ff = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
Font.SIZE_SMALL);
g.setFont(ff);
g.setColor(255, 255, 255);
g.drawString("Push FIRE", screenwidth, screenheight, g.BOTTOM
| g.RIGHT);
// sellerAsk = false;
roleChoose = true;
}
if (isBuy) { //角色選擇買商品時的選擇框:黃金-》血氣,黃金-》寶物,放棄
g.setColor(0, 0, 100);
g.fillRect(0, getHeight() - 57, getWidth(), 57);
g.drawImage(load.sellerbig, 0, getHeight() - 57, 0);
g.setColor(255, 255, 255);
g.drawString(choose1, 60, getHeight() - 40, 0);
g.drawString(choose2, 60, getHeight() - 30, 0);
g.drawString(choose3, 60, getHeight() - 20, 0);
g.setColor(255, 255, 0);
// g.fillArc(42, attackSelecty, 5, 5, 0, 360);
g.drawImage(load.selectIcon, 40, attackSelecty, g.TOP|g.LEFT);
forBuy = true;
}
if (isSell) { //角色選擇賣商品時的選擇框:寶物-》黃金,放棄
g.setColor(0, 0, 100);
g.fillRect(0, getHeight() - 40, getWidth(), 40);
g.drawImage(load.sellerbig, 0, getHeight() - 40, 0);
g.setColor(255, 255, 255);
g.drawString(choose1, 60, getHeight() - 30, 0);
g.drawString(choose2, 60, getHeight() - 20, 0);
g.setColor(255, 255, 0);
// g.fillArc(42, selecty, 5, 5, 0, 360);
g.drawImage(load.selectIcon, 40, selecty, g.TOP|g.LEFT);
forSell = true;
}
if (isChoose) { //人物碰到npc時選擇框:攻擊,放棄
g.setColor(0, 0, 100);
g.fillRect(0, getHeight() - 57, getWidth(), 57);
g.drawImage(load.hero, 0, getHeight() - 57, 0);
g.setColor(255, 255, 128);
g.drawString(choose1, 70, getHeight() - 30, 0);
g.drawString(choose2, 70, getHeight() - 20, 0);
g.setColor(255, 255, 0);
// g.fillArc(52, selecty, 5, 5, 0, 360);
g.drawImage(load.selectIcon, 50, selecty, g.TOP|g.LEFT);
Font ff = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
Font.SIZE_SMALL);
g.setFont(ff);
g.setColor(255, 255, 255);
g.drawString("Push FIRE", screenwidth, screenheight, g.BOTTOM
| g.RIGHT);
g.setColor(0);
g.drawRoundRect(0, screenheight - 65, 40, 5, 4, 2);
g.setColor(0xB84909);
g.fillRoundRect(0, screenheight - 65, roleBloodHeight, 5, 4, 2);
g.setColor(255);
}
if(isJump){
g.setColor(255,255,255);
g.fillRect((screenwidth - attackWidth) >> 1,
(screenheight - attackHeight) >> 1, attackWidth, attackHeight);
// g.setClip(mrole.x, mrole.y, 70, 64);
// g.drawImage(load.heroJump, mrole.x - 70 * i, mrole.y, g.TOP|g.LEFT);
g.setClip((screenwidth - attackWidth) >> 1,
(screenheight - attackHeight) >> 1, attackWidth,attackHeight);
g.drawImage(load.heroJump, screenwidth / 2 - attackWidth* i,
screenheight >> 1, g.HCENTER | g.VCENTER);
g.setClip(0, 0, screenwidth, screenheight);
i ++;
if(i == 6){
isJump = false;
isSelectAttack = false;
i = 0;
inputmode = 0;
if (!warEnd) { //當是boss的時候,根據人物等級不同,攻擊力野不相同
if (mroleProperty[0] <= 5)
bossBlood -= 5;
else if (mroleProperty[0] >= 11)
bossBlood -= 10;
else
bossBlood -= mroleProperty[0];
if (bossBlood >= 0) {
isBossAttack = true;
tempNum = getrand(4);
switch (tempNum) {
case 0:
soldierWord = "看招:一擊即中";
blood = 4;
attackWidth = 29;
attackHeight = 32;
break;
case 1:
soldierWord = "看招:所向披靡";
attackWidth = 101;
attackHeight = 76;
blood = 3;
break;
case 2:
soldierWord = "看招:怒海狂潮";
blood = 5;
attackWidth = 80;
attackHeight = 80;
break;
case 3:
soldierWord = "看招:天翻地覆";
blood = 10;
attackWidth = 78;
attackHeight = 98;
break;
default:
break;
}
} else
isBossDead = true;
}
}
g.setClip(0, 0, screenwidth, screenheight);
}
if (isAttack) { //當人物選擇攻擊時,npc先進行攻擊,在三招中隨機選擇一招
switch (tempNum) {
case 0:
soldierWord = "看招:一擊即中";
blood = 4;
attackWidth = 29;
attackHeight = 32;
break;
case 1:
soldierWord = "看招:所向披靡";
attackWidth = 101;
attackHeight = 76;
blood = 3;
break;
case 2:
soldierWord = "看招:怒海狂潮";
blood = 5;
attackWidth = 80;
attackHeight = 80;
break;
default:
break;
}
g.setColor(255, 255, 255);
g.fillRect(0, 0, screenwidth, screenheight);
g.setColor(0, 0, 100);
g.fillRect(0, 0, getWidth(), 50);
g.drawImage(load.soldierbig, 0, 0, 0);
Font f = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
Font.SIZE_MEDIUM);
g.setFont(f);
g.setColor(255, 255, 255);
g.drawString(soldierWord, 37, 0, 0);
g.setClip((screenwidth - attackWidth) >> 1,
(screenheight - attackHeight) >> 1, attackWidth,attackHeight);
g.drawImage(load.attack[tempNum], screenwidth / 2 - attackWidth* i,
screenheight >> 1, g.HCENTER | g.VCENTER);
g.setClip(0, 0, screenwidth, screenheight);
if (i == 6) { //招數六個畫面播放完后
isAttack = false;
isSoldier = false;
// roleAttack = true;
// inputmode = 3;
isChoose = true;
inputmode = 1;
choose1 = "普通攻擊";
choose2 = "魔法攻擊";
isSelectAttack = true;
i = -1;
load.npc[number].setVisible(false);
randX = getrand(480);
randY = getrand(450); //在地圖上隨機生成相同的野怪
load.npc[number] = new Npc(load.imgsoldier, 16, 24,
load.npc[number].name, load.npc[number].message1,
randX, randY, load.npc[number].message2,
load.npc[number].img);
mroleProperty[0]++; //人物增加等級與野怪等級相同
if (number == 3)
mroleProperty[0]++;
roleBloodHeight -= blood;
// drawRoleAttack();
}
i++;
}
if (roleAttack) { //人物進行攻擊招數選擇,共三種
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -