rpgcanvas.java
来自「一个J2ME的RPG游戏」· Java 代码 · 共 1,841 行 · 第 1/4 页
JAVA
1,841 行
g.setColor(0, 0, 100);
g.fillRect(0, getHeight() - 57, getWidth(), 57);
g.drawImage(load.hero, 0, getHeight() - 57, 0);
g.setColor(255, 255, 255);
// private String roleWord=" 选招: 星星燎燃 烈火溅射 深海狂啸";
g.drawString("选招: ", 50, getHeight() - 57, 0);
g.drawString(" 星星燎燃", 70, getHeight() - 40, 0);
g.drawString(" 烈火溅射", 70, getHeight() - 30, 0);
g.drawString(" 深海狂啸", 70, getHeight() - 20, 0);
g.setColor(255, 255, 0);
// g.fillArc(52, attackSelecty, 5, 5, 0, 360);
g.drawImage(load.selectIcon, 50, attackSelecty, g.TOP|g.LEFT);
isRoleAttack = true;
}
if (roleBeat) { //人物的攻击画面
g.setColor(255, 255, 255);
g.fillRect(0, screenheight - 57, screenheight, 57);
g.setColor(0, 0, 100);
g.fillRect(0, getHeight() - 57, getWidth(), 57);
g.drawImage(load.hero, 0, getHeight() - 57, 0);
g.setColor(255, 255, 255);
g.drawString("看招: " + roleWord, 50, getHeight() - 30, 0);
System.out.println(roleWord);
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 == 4) { //5幅动画播放完后
i = -1;
roleBeat = false;
isChoose = false;
inputmode = 0;
for(int j = 0 ; j < 6; j++)
g.drawString("获得补血草", screenwidth>>1, screenheight>>1, g.TOP|g.LEFT);
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;
}
}
i++;
}
//当人物血气小于等于0时,游戏结束界面
if (roleBloodHeight <= 0) {
g.setColor(0);
g.drawString("Game Over!!", screenwidth >> 1,
(screenheight >> 1) - 20, g.TOP | g.HCENTER);
g.drawString("You Died In the War!!", screenwidth >> 1,
screenheight >> 1, g.TOP | g.HCENTER);
t = null;
addCommand(exitGame);
}
//boss对话
if (isProperty) {
// soldierWord = "我是守护神坛的使者,只有通过我这一关,你才能继续前进。";
g.setColor(0, 0, 100);
g.fillRect(0, screenheight-57, getWidth(), 57);
g.drawImage(pImage, 0,screenheight-57, 0);
Font f = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
Font.SIZE_MEDIUM);
g.setFont(f);
g.setColor(255, 255, 255);
if (pMessage.length() <= getWidth() / 17) {
g.drawString(pMessage, 57, screenheight-57, 0);
} else {
int length = pMessage.length();
int len = getWidth() / 17;
int y = screenheight-57;
int offset = 0;
while (length > 0) {
g.drawSubstring(pMessage, offset, len, 57, y, 0);
y += 17;
offset += getWidth() / 17;
length -= getWidth() / 17;
if (length > getWidth() / 17) {
len = getWidth() / 17;
} else {
len = length;
}
}
}
g.setColor(255, 255, 255);
g.drawString("Push FIRE", screenwidth, screenheight, g.BOTTOM | g.RIGHT);
}
//boss攻击画面
if (isBossAttack) {
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;
}
g.setColor(255, 255, 255);
g.fillRect(0, 0, screenwidth, screenheight);
g.setColor(0, 0, 100);
g.fillRect(0, 0, getWidth(), 57);
g.drawImage(load.bossface, 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, 50, 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);
g.setColor(0);
g.drawRoundRect(0, 60, 30, 5, 4, 2);
g.setColor(0xB84909);
g.fillRoundRect(0, 60, bossBlood, 5, 4, 2);
g.setColor(255);
if (i == 6) {
isBossAttack = false;
// isBossDead = true;
load.property[1].setVisible(false);
isBoss = false;
// roleAttack = true;
// inputmode = 3;
isChoose = true ;
inputmode = 1;
choose1 = "普通攻击";
choose2 = "魔法攻击";
isSelectAttack = true;
i = -1;
roleBloodHeight -= blood;
if (roleBloodHeight <= 0)
warEnd = true;
}
i++;
}
}
//找到胜利旗帜时,游戏成功结束界面
else if (gameOver) {
t = null;
g.drawString("You Win The Game!!", screenwidth >> 1,
screenheight >> 1, g.TOP | g.HCENTER);
addCommand(exitGame);
}
}
public void nextmessage() {
if (vectormessage.size() > 0 && vectorpicture.size() > 0) {
curpicture = (Image) vectorpicture.elementAt(0);
curmessage = (String) vectormessage.elementAt(0);
}
if (Math.abs(tempx - mrole.x) > 4 || Math.abs(tempy - mrole.y) > 4) {
curmessage = null;
vectormessage.removeAllElements();
vectorpicture.removeAllElements();
showyesorno = false;
iscollide = false;
}
}
//人物属性介绍
public void drawProperty() {
g.drawString("等级", 0, 0, g.TOP | g.LEFT);
g.drawString("宝物", 30, 0, g.TOP | g.LEFT);
g.drawString("任务", 60, 0, g.TOP | g.LEFT);
g.drawString("血气", 90, 0, g.TOP | g.LEFT);
g.drawString("黄金", 130, 0, g.TOP | g.LEFT);
for (int i = 0; i < 3; i++)
g.drawString(Integer.toString(mroleProperty[i]), i * 30, 20, g.TOP
| g.LEFT);
g.setColor(0);
g.drawRoundRect(90, 25, 40, 5, 4, 2);
g.setColor(0xB84909);
g.fillRoundRect(90, 25, roleBloodHeight, 5, 4, 2);
g.setColor(255, 255, 255);
g.drawString(Integer.toString(mroleProperty[4]), 140, 20, g.TOP
| g.LEFT);
}
// 绘制对话函数
public void drawmessage() {
if (ismessage) {
// System.out.println("in drawmessage");
if (curmessage != null) {
// /////*********************/////////
g.setColor(0, 0, 100);
g.fillRect(0, getHeight() - 40, getWidth(), 40);
// //////////****************////////////
g.drawImage(curpicture, 0, getHeight() - 40, 0);
Font f = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
Font.SIZE_MEDIUM);
g.setFont(f);
g.setColor(255, 255, 255);
if (curmessage.length() <= getWidth() / 12) {
g.drawString(curmessage, 37, getHeight() - 40, 0);
} else {
int length = curmessage.length();
int len = getWidth() / 12;
int y = getHeight() - 40;
int offset = 0;
while (length > 0) {
g.drawSubstring(curmessage, offset, len, 37, y, 0);
y += 12;
offset += getWidth() / 12;
length -= getWidth() / 12;
if (length > getWidth() / 12) {
len = getWidth() / 12;
} else {
len = length;
}
}
}
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);
}
}
}
// 碰撞检测
void collidetest() {
// mrole.setPosition(mrole.roomx, mrole.roomy);
if (scene == 2) { //场景二
mrole.setPosition(mrole.roomx, mrole.roomy);
//与返回场景一的门口碰撞
if (mrole.collidesWith(load.maproom1layer2, false)) {
scene = 1;
inputmode = 0;
mrole.x = mrole.x;
mrole.y = mrole.y + 4;
}
//与家具碰撞
else if (mrole.collidesWith(load.maproom1layer1, true)) {
mrole.unmoveroom();
}
if (isBossDead && (mroleProperty[0] >= 2)) //与红旗碰撞,代表游戏的胜利
if (mrole.collidesWith(load.property[4], true))
gameOver = true;
}
//场景一
else if (scene == 1) {
mrole.setPosition(mrole.x, mrole.y);
for (int i = 0; i < load.property.length; i++) {
if(i == 4)continue;
Property ptemp = load.property[i];
if (mrole.collidesWith(ptemp, true)) {
isProperty = true;
// inputmode = 3;
screenunmove();
mrole.unmove();
switch (i) {
// 与猫碰撞
case 0:
mrole.setPosition(mrole.x, mrole.y);
pMessage = "喵,喵~~~~~";
pImage = load.cat;
if (mainroleanswer)
catchcat = true;
else
catchcat = false;
// else catchcat = false;
// 与boss碰撞
break;
case 1:
isBoss = true;
pMessage = "我是守护神坛的使者,只有通过我这一关,你才能继续前进。";
pImage = load.bossface;
break;
// 与食物碰撞
case 2:
isFood = true;
pMessage = "食物:增加3点血气";
pImage = load.food;
break;
// 与宝剑碰撞
case 3:
isSword = true;
pMessage = "宝剑:增加英雄攻击力";
pImage = load.sword;
break;
// 与黄金碰撞
case 5:
pMessage = "黄金:增加黄金数";
pImage = load.gold;
isGold = true;
break;
default:
break;
}
}
}
//与农场建筑物碰撞
if (mrole.collidesWith(load.imgmap1, true)) {
screenunmove();
mrole.unmove();
mrole.setPosition(mrole.x, mrole.y);
}
//与商店碰撞
else if (mrole.collidesWith(load.hourse, true)) {
screenunmove();
mrole.unmove();
mrole.setPosition(mrole.x, mrole.y);
}
//与npc碰撞
// for (int i = 0; i < load.vectornpc.size(); i++) {
// Npc npc = (Npc) load.vectornpc.elementAt(i);
for (int i = 0; i < load.npc.length; i++) {
Npc npc = (Npc) load.npc[i];
if (mrole.collidesWith(npc, true)) {
tempx = mrole.x;
tempy = mrole.y;
mrole.unmove();
screenunmove();
mrole.setPosition(mrole.x, mrole.y);
ismessage = true;
if (npc.name == "老人" && catchcat2 == true) {
String temp2 = npc.name + ":" + npc.message2;// 加入npc对话
vectormessage.addElement(temp2);
Image tempimage2 = npc.img;
vectorpicture.addElement(tempimage2);
missionmessage = "任务完成";
if (firstTouch) {
mroleProperty[2]++;
firstTouch = false;
}
} else if (npc.name == "老人") {
String temp = npc.name + ":" + npc.message1;// 加入npc对话
vectormessage.addElement(temp);
Image tempimage = npc.img;
vectorpicture.addElement(tempimage);
iscollide = true;
} else if (npc.name == "战士" || npc.name == "战士2"
|| npc.name == "战士3") {
// isChoose = true;
isSoldier = true;
// inputmode = 1;
String temp = npc.name + ":" + npc.message1;// 加入npc对话
vectormessage.addElement(temp);
Image tempimage = npc.img;
vectorpicture.addElement(tempimage);
if (npc.name == "战士")
number = 2;
else if (npc.name == "战士2")
number = 3;
else if (npc.name == "战士3")
number = 4;
// if ((key & FIRE_PRESSED) != 0) {
// isbeat = true;
// }
} else if (npc.name == "商人") {
String temp = npc.name + ":" + npc.message1;// 加入npc对话
vectormessage.addElement(temp);
Image tempimage = npc.img;
vectorpicture.addElement(tempimage);
isSeller = true;
} else if(npc.name == "超级女生"){
String temp = npc.name + ":" + npc.message1;// 加入npc对话
vectormessage.addElement(temp);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?