?? rpgcanvas.java
字號(hào):
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; //隨機(jī)數(shù)
RPGMidlet midlet;
Vector vectormessage; //保存會(huì)話內(nèi)容
Vector vectorpicture; //保存圖畫(huà)
LayerManager lmscene1, lmscene2; //場(chǎng)景一,二管理對(duì)象
String missionmessage, curmessage; //任務(wù)消息和當(dāng)前人物會(huì)話內(nèi)容
Mainrole mrole; //游戲主角色
Load load; //加載游戲畫(huà)面
Graphics g;
Image curpicture;
private boolean catchcat = false; //判斷與貓的接觸
private boolean catchcat2 = false;
private boolean showyesorno = false; //是否顯示會(huì)話內(nèi)容
private boolean mainroleanswer = false; //人物回答
private boolean iscollide = false; //是否與老人接觸了
private int scene = 1; // check for scene //場(chǎng)景
private int inputmode = 0; //輸入模式
int screenx, screeny, lastscreenx, lastscreeny; //屏幕當(dāng)前坐標(biāo) 和上次移動(dòng)的坐標(biāo)大小
int tempx = 0, tempy = 0;
int selecty = 152; //當(dāng)進(jìn)行模式選擇時(shí),選擇光標(biāo)的位置
int key; //獲得鍵盤(pán)狀態(tài)
static int mroleProperty[]; //人物屬性數(shù)組
private int roleBloodHeight = 40; //人物血?dú)獾拈L(zhǎng)度
private boolean ismessage = false; //npc人物是否顯示對(duì)話
private boolean firstTouch = true; //完成尋找貓后,第一次與老人接觸后為false
private boolean isbeat = false; //判斷是否與野怪接觸
public static final int GAME_MENU = 0; //游戲菜單狀態(tài)
public static final int GAME_START = 1; //游戲進(jìn)行狀態(tài)
public static final int GAME_PAUSE = 2; //游戲停止?fàn)顟B(tài)
public static final int GAME_ABOUT = 8; //游戲介紹狀態(tài)
private static final int MENU_START = 3; //菜單:進(jìn)入游戲
private static final int MENU_LOAD = 4; //菜單:載入進(jìn)度
private static final int MENU_VOL = 5; //菜單:聲音播放
private static final int MENU_QUIT = 6;// 菜單: 退出
private static final int MENU_ABOUT = 7; //菜單:關(guān)于游戲
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; //判斷是否剛啟動(dòng)游戲,尚未進(jìn)入游戲狀態(tài)
private boolean gameOver = false; //游戲結(jié)束標(biāo)志
private boolean isBoss = false; //是否與boss接觸
private boolean isSoldier = false; //是否與野怪接觸
private boolean isAttack = false; // 是否攻擊選擇
private int number; //判斷接觸的是第幾個(gè)野怪2,3,4分表表示第一,二,三個(gè)野怪
private String soldierWord; //存放野怪或boss對(duì)話內(nèi)容
private int blood; //存放野怪或boss所出招數(shù)引起的血?dú)鉁p少量
private int attackWidth; //每幀招數(shù)圖片的寬
private int attackHeight; //每幀招數(shù)圖片的高
private String roleWord = ""; //人物對(duì)話內(nèi)容
private boolean roleAttack = false; // 人物招數(shù)選擇
private int attackSelecty = screenwidth - 35; //有三項(xiàng)選擇時(shí),游戲光標(biāo)位置
private int tempNum; //隨機(jī)數(shù)
private boolean roleBeat = false; //人物放招畫(huà)面
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血?dú)? private boolean warEnd = false; //判斷與boss戰(zhàn)斗是否結(jié)束
private int randX, randY; //隨機(jī)獲得坐標(biāo),用來(lái)隨機(jī)產(chǎn)生道具
private int gameState; //游戲狀態(tài)
private int menuState; //菜單狀態(tài)
private String choose1, choose2, choose3; //選擇項(xiàng)
private boolean isGold = false; //是否與黃金碰撞
private boolean isSeller = false; //是否與商人碰撞
private boolean isBuy = false; //是否買(mǎi)
private boolean isSell = false; //是否賣(mài)
private boolean forBuy = false; //選擇買(mǎi)
private boolean forSell = false; //選擇賣(mài)
private boolean sellerAsk = false; //商人詢(xún)問(wèn)買(mǎi)賣(mài)
private boolean roleChoose = false; //人物是否選擇買(mǎi)賣(mài)
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) { // 構(gòu)造函數(shù),初始類(lèi)
super(true);
this.midlet = midlet;
g = getGraphics();
r = new Random();
// t = new Thread(this);
mroleProperty = new int[] { 0, 0, 0, 0, 0 }; // 分別表示等級(jí),寶物,完成任務(wù)數(shù),血?dú)?寶物
// 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); //為什么需要加上對(duì)象參數(shù)this??當(dāng)無(wú)this時(shí)無(wú)游戲畫(huà)面
t.start();
System.out.println("Thread Start!");
}
//產(chǎn)生隨機(jī)數(shù)
int getrand(int m) {
int n = r.nextInt();
if (n < 0)
n *= -1;
n %= m;
return n;
}
public void screenmove(int increasex, int increasey) { // 屏幕移動(dòng)函數(shù)
screenx = screenx + increasex;
screeny = screeny + increasey;
lastscreenx = increasex;
lastscreeny = increasey;
}
void screenunmove() {// 屏幕負(fù)移動(dòng)函數(shù)
screenmove(-lastscreenx, -lastscreeny);
}
void draw() { // 繪畫(huà)函數(shù)
// 清屏
if (!gameOver) {
// 設(shè)置window
if (!isAttack && !roleAttack && !roleBeat && !isSelectAttack) {
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
if (scene == 1) { //加載場(chǎng)景一
lmscene1.setViewWindow(screenx, screeny, screenwidth,screenheight);
append();
if(isBossDead)load.property[1].setVisible(false);
lmscene1.paint(g, 0, 0);
drawProperty();
}
else if (scene == 2) { //加載場(chǎng)景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);
}
// 顯示對(duì)話
drawmessage();
if (showyesorno == true) {
drawlist(); //顯示選擇框
}
if (sellerAsk) { //商人詢(xún)問(wèn)框 :買(mǎi),賣(mài),放棄
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) { //角色選擇買(mǎi)商品時(shí)的選擇框:黃金-》血?dú)?,黃金-》寶物,放棄
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) { //角色選擇賣(mài)商品時(shí)的選擇框:寶物-》黃金,放棄
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時(shí)選擇框:攻擊,放棄
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) { //當(dāng)是boss的時(shí)候,根據(jù)人物等級(jí)不同,攻擊力野不相同
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 = "看招:怒??癯?quot;;
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) { //當(dāng)人物選擇攻擊時(shí),npc先進(jìn)行攻擊,在三招中隨機(jī)選擇一招
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 = "看招:怒??癯?quot;;
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) { //招數(shù)六個(gè)畫(huà)面播放完后
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); //在地圖上隨機(jī)生成相同的野怪
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]++; //人物增加等級(jí)與野怪等級(jí)相同
if (number == 3)
mroleProperty[0]++;
roleBloodHeight -= blood;
// drawRoleAttack();
}
i++;
}
if (roleAttack) { //人物進(jìn)行攻擊招數(shù)選擇,共三種
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -