?? rpgcanvas.java
字號(hào):
Image tempimage = npc.img;
vectorpicture.addElement(tempimage);
npc.pause = true;
}
}
}
//與倉(cāng)庫(kù)入口碰撞,進(jìn)入場(chǎng)景二
if (mrole.collidesWith(load.imgmap3, true)) {
scene = 2;
inputmode = 2;
mrole.roomx = 80;
mrole.roomy = 153;
removeCommand(mainMenu);
// load.maproom1layer2.setVisible(true);
// load.maproom1layer1.setVisible(true);
// load.maproom1ground.setVisible(true);
}
}
}
//與老人的對(duì)話
public void drawlist() {
g.setColor(0, 0, 100);
g.fillRect(0, getHeight() - 40, getWidth(), 40);
g.setColor(255, 255, 128);
g.drawString(choose1, 50, getHeight() - 30, 0);
g.drawString(choose2, 50, getHeight() - 20, 0);
g.setColor(255, 255, 0);
g.fillArc(45, selecty, 5, 5, 0, 360);
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);
// if((key&FIRE_PRESSED)!=0)showyesorno=false;
}
//在場(chǎng)景一加載游戲畫面
public void append() {
lmscene1.append(mrole);
lmscene1.append(load.npc[0]);
lmscene1.append(load.npc[1]);
lmscene1.append(load.npc[2]);
lmscene1.append(load.npc[3]);
lmscene1.append(load.npc[4]);
lmscene1.append(load.npc[5]);
lmscene1.append(load.property[0]);
lmscene1.append(load.property[1]);
lmscene1.append(load.property[2]);
lmscene1.append(load.property[3]);
lmscene1.append(load.property[5]);
lmscene1.append(load.imgmap1);
lmscene1.append(load.imgmap2);
lmscene1.append(load.imgmap3);
lmscene1.append(load.hourse);
lmscene1.append(load.mapbackground);
}
//場(chǎng)景二加載游戲畫面
public void append2() {
lmscene2.append(mrole);
if (isBossDead && (mroleProperty[0] >= 2))
lmscene2.append(load.property[4]);
// lmscene2.append(load.flagLayer);
lmscene2.append(load.maproom1layer2);
lmscene2.append(load.maproom1layer1);
lmscene2.append(load.maproom1ground);
}
// 輸入函數(shù),負(fù)責(zé)主角和屏幕的移動(dòng)
// 主線成,
int i = 0;
public void run() {
// long startTime = System.currentTimeMillis();
Thread currentThread = Thread.currentThread();
while (currentThread == t) {
input(); //獲取輸入
switch (gameState) {
case GAME_START:
addCommand(mainMenu);
nextmessage();
collidetest(); //碰撞檢測(cè)
draw(); //繪制畫面
break;
case GAME_PAUSE:
drawPause(); //游戲暫停
break;
case GAME_MENU:
drawMenu(); //游戲菜單
break;
case GAME_ABOUT:
drawMenuAbout(); //游戲介紹
break;
}
this.flushGraphics();
try {
Thread.sleep(150);
} catch (InterruptedException e) {
e.printStackTrace();
}
// long time2 = System.currentTimeMillis() - startTime;
// if (time2 < interval) {
// try {
// // t.sleep(interval - time2); //刷新太快,游戲人物滑步
// Thread.sleep(500);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// } else
// Thread.yield();
}
}
//繪制菜單
public void drawMenu() {
g.setColor(255, 255, 255); // 白色, g.setColor(255);代表藍(lán)色,相當(dāng)于(0,0,255) 與1.0不同
g.fillRect(0, 0, screenwidth, screenheight);
g.setColor(0);
int menuOffset = (screenheight - font.getHeight() * 6) / 2;
g.setFont(font);
g.setClip(0, 0, screenwidth, screenheight);
// g.drawImage(image, 0, 0, g.LEFT | g.TOP);
int item0Color = 0;
int item1Color = 0;
int item2Color = 0;
int item3Color = 0;
int item4Color = 0;
int item5Color = 0;
switch (menuState) { //使相應(yīng)菜單狀態(tài)顏色變紅
case MENU_START:
item0Color = 255 << 16;
break;
case MENU_LOAD:
item1Color = 255 << 16;
break;
case MENU_VOL:
item2Color = 255 << 16;
break;
case MENU_QUIT:
item3Color = 255 << 16;
break;
case MENU_ABOUT:
item4Color = 255 << 16;
break;
case MENU_SAVE:
item5Color = 255 << 16;
break;
}
g.setColor(item0Color);
g.drawString("新游戲", (screenwidth - font.stringWidth("新游戲")) / 2,
menuOffset + font.getHeight() * 0, g.LEFT | g.TOP);
g.setColor(item1Color);
g.drawString("載入進(jìn)度", (screenwidth - font.stringWidth("載入進(jìn)度")) / 2,
menuOffset + font.getHeight() * 1, g.LEFT | g.TOP);
g.setColor(item2Color);
if (isVolOn) {
g.drawString("關(guān)閉聲音", (screenwidth - font.stringWidth("關(guān)閉聲音")) / 2,
menuOffset + font.getHeight() * 2, g.LEFT | g.TOP);
} else {
g.drawString("打開聲音", (screenwidth - font.stringWidth("打開聲音")) / 2,
menuOffset + font.getHeight() * 2, g.LEFT | g.TOP);
}
g.setColor(item3Color);
g.drawString("退出", (screenwidth - font.stringWidth("退出")) / 2,
menuOffset + font.getHeight() * 3, g.LEFT | g.TOP);
g.setColor(item4Color);
g.drawString("關(guān)于游戲", (screenwidth - font.stringWidth("關(guān)于游戲")) / 2,
menuOffset + font.getHeight() * 4, g.LEFT | g.TOP);
g.setColor(item5Color);
g.drawString("保存游戲", (screenwidth - font.stringWidth("保存游戲")) / 2,
menuOffset + font.getHeight() * 5, g.LEFT | g.TOP);
}
public void drawPause() {
}
//根據(jù)設(shè)定大小,自動(dòng)換行輸出的字符串
final int CharacterNumber = 8;
public Vector getSubsection(String str) {
Vector vector = new Vector();
while (!str.equals("")) {
if (str.length() > CharacterNumber) {
vector.addElement(str.substring(0, CharacterNumber));
str = str.substring(CharacterNumber);
} else {
vector.addElement(str);
str = "";
}
}
return vector;
}
//游戲介紹
public void drawMenuAbout() {
int i = 0;
tempH = 0;
g.setColor(255,255,255);
g.fillRect(0, 0, screenwidth, screenheight);
g.setColor(0);
String gameAbout = "游戲背景:人生百態(tài),事竟千相,"
+ "在這個(gè)世界里,跟我們一起并行存在著各種各樣的人,"
+ "發(fā)生著各種各樣的事," + "如果不去發(fā)現(xiàn),你永遠(yuǎn)不知道這個(gè)世界有多大,"
+ "不去體會(huì),永遠(yuǎn)不懂得什么是酸甜苦辣。"
+ "就讓一切從現(xiàn)在開始探索," + "一切從現(xiàn)在開始展現(xiàn)……\n";
String gameOperation="游戲操作:上、下、左、右按鍵分別控制人物的"
+"上、下、左、右運(yùn)動(dòng)或者上、下、左、右選擇\n";
String gameFinish="游戲結(jié)束:當(dāng)游戲人物血小于等于0時(shí)游戲失敗結(jié)束,"
+"當(dāng)游戲人物殺死boss并達(dá)到等級(jí)2后"
+"農(nóng)場(chǎng)倉(cāng)庫(kù)刷新出一面旗子,找到旗子,游戲勝利結(jié)束。\n";
g.setFont(font);
int hWidth = screenwidth / 2;
Vector vector = getSubsection(gameAbout);
for ( i = 0; i < vector.size(); i++) {
g.drawString((String) vector.elementAt(i), hWidth, gameAboutY + 5 + fHeight * i,
Graphics.TOP | Graphics.HCENTER);
}
tempH = 5 + fHeight * i;
vector = getSubsection(gameOperation);
for(i = 0; i < vector.size(); i++){
g.drawString((String) vector.elementAt(i), hWidth, gameAboutY + 5 + tempH + fHeight * i,
Graphics.TOP | Graphics.HCENTER);
}
tempH = 5 + tempH + fHeight * i;
vector = getSubsection(gameFinish);
for(i = 0; i < vector.size(); i++){
g.drawString((String) vector.elementAt(i), hWidth, gameAboutY + 5 + tempH + fHeight * i,
Graphics.TOP | Graphics.HCENTER);
}
tempH = 5 + tempH + fHeight * i;
vector = null;
i = 0;
removeCommand(backGame);
this.addCommand(backMenu);
}
//聲音播放
public void playVol() {
if (isVolOn) {
if (bgPlayer == null) {
bgPlayer = new AudioPlayer("bg.mid", "audio/midi", true);
bgPlayer.setLoop();
bgPlayer.play();
}
} else {
if (bgPlayer != null) {
bgPlayer.stop();
bgPlayer.close();
bgPlayer = null;
System.gc();
}
}
}
//加載游戲進(jìn)度
public void loadGame() {
// System.out.println("enter loadGame()");
removeCommand(backGame);
addCommand(mainMenu);
byte[] rec = record.getRecord(1);
// System.out.println(rec[1]);
ByteArrayInputStream bais = new ByteArrayInputStream(rec);
DataInputStream dis = new DataInputStream(bais);
try {
// isVolOn = dis.readBoolean();
// missionmessage=dis.readUTF();
// curmessage=dis.readUTF();
catchcat = dis.readBoolean();
catchcat2 = dis.readBoolean();
showyesorno = dis.readBoolean();
mainroleanswer = dis.readBoolean();
inputmode = dis.readInt();
iscollide = dis.readBoolean();
screenx = dis.readInt();
screeny = dis.readInt();
tempx = dis.readInt();
tempy = dis.readInt();
lastscreenx = dis.readInt();
lastscreeny = dis.readInt();
selecty = dis.readInt();
for (int i = 0; i < 5; i++)
mroleProperty[i] = dis.readInt();
roleBloodHeight = dis.readInt();
ismessage = dis.readBoolean();
firstTouch = dis.readBoolean();
isbeat = dis.readBoolean();
isFirstStart = dis.readBoolean();
isBossDead = dis.readBoolean();
mrole.x = dis.readInt();
mrole.y = dis.readInt();
load.property[0].setVisible(dis.readBoolean());
load.property[1].setVisible(dis.readBoolean());
load.npc[2].setVisible(dis.readBoolean());
} catch (IOException e) {
System.out.println("Error in loadGame() methon");
e.printStackTrace();
}
}
//保存游戲狀態(tài)
public void saveGameState() {
removeCommand(backGame);
addCommand(mainMenu);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try {
// dos.writeBoolean(isVolOn);
// dos.writeUTF(missionmessage);
// dos.writeUTF(curmessage);
dos.writeBoolean(catchcat);
dos.writeBoolean(catchcat2);
dos.writeBoolean(showyesorno);
dos.writeBoolean(mainroleanswer);
dos.writeInt(inputmode);
dos.writeBoolean(iscollide);
dos.writeInt(screenx);
dos.writeInt(screeny);
dos.writeInt(tempx);
dos.writeInt(tempy);
dos.writeInt(lastscreenx);
dos.writeInt(lastscreeny);
dos.writeInt(selecty);
for (int i = 0; i < 5; i++)
dos.writeInt(mroleProperty[i]);
dos.writeInt(roleBloodHeight);
dos.writeBoolean(ismessage);
dos.writeBoolean(firstTouch);
dos.writeBoolean(isbeat);
dos.writeBoolean(isFirstStart);
dos.writeBoolean(isBossDead);
dos.writeInt(mrole.x);
dos.writeInt(mrole.y);
dos.writeBoolean(load.property[0].isVisible());
dos.writeBoolean(load.property[1].isVisible());
dos.writeBoolean(load.npc[2].isVisible());
if(record.IsEmpty())this.record.addRecord(baos.toByteArray());
else
this.record.SetRecord(1, baos.toByteArray());
dos.close();
baos.close();
// System.out.println("saveGameState !!");
} catch (IOException e) {
System.out.println("Error in saveGameState().");
e.printStackTrace();
}
}
//重新初始化游戲
private void reinit() {
catchcat = false;
catchcat2 = false;
showyesorno = false;
mainroleanswer = false;
scene = 1;
inputmode = 0;
iscollide = false;
selecty = 152;
isVolOn = false;
curmessage = null;
missionmessage = null;
curpicture = null;
mrole.setFrame(1);
mrole.x = 50;
mrole.y = 50; // 為什么 此處用mrole.setPosition(50,50)
// 不能重設(shè)英雄位置,是否此時(shí)設(shè)的是英雄父類的x和y坐標(biāo)
mroleProperty[0] = 0;
mroleProperty[1] = 0;
mroleProperty[2] = 0;
roleBloodHeight = 40;
attackSelecty = screenwidth - 35;
bossBlood = 30;
warEnd = false;
menuState = MENU_START;
isGold = false;
isSeller = false;
isBuy = false;
isSell = false;
forBuy = false;
forSell = false;
sellerAsk = false;
roleChoose = false;
}
//游戲輸入
void input() {
key = getKeyStates();
if (gameState == GAME_PAUSE)
gameState = GAME_START;
if (gameState == GAME_MENU) { //當(dāng)游戲在菜單狀態(tài)下
switch (key) {
case UP_PRESSED:
switch (menuState) {
case MENU_START:
if (isFirstStart)
menuState = MENU_ABOUT;
else
menuState = MENU_SAVE;
break;
case MENU_LOAD:
menuState = MENU_START;
break;
case MENU_VOL:
menuState = MENU_LOAD;
break;
case MENU_QUIT:
menuState = MENU_VOL;
break;
case MENU_ABOUT:
menuState = MENU_QUIT;
break;
case MENU_SAVE:
menuState = MENU_ABOUT;
break;
}
break;
case DOWN_PRESSED:
switch (menuState) {
case MENU_START:
menuState = MENU_LOAD;
break;
case MENU_LOAD:
menuState = MENU_VOL;
break;
case MENU_VOL:
menuState = MENU_QUIT;
break;
case MENU_QUIT:
menuState = MENU_ABOUT;
break;
case MENU_ABOUT:
if (isFirstStart)
menuState = MENU_START;
else
menuState = MENU_SAVE;
break;
case MENU_SAVE:
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -