📄 mygamecanvas.java
字号:
if (ox < 0) {
ox = 0;
}
if (oy < 0) {
oy = 0;
}
if (ox + width > mapWidth) {
ox = mapWidth - width;
}
if (oy + height > mapHeight) {
oy = mapHeight - height;
}
lm.setViewWindow(ox, oy, width, height);
//取下一条消息
if (System.currentTimeMillis() - lastPopMsgTime > 1000) {
this.curMessage = this.popMessage();
lastPopMsgTime = System.currentTimeMillis();
}
} else if (gameStatus == STATUS_FIGHT_OVER) {
synchronized (engine) {
if (System.currentTimeMillis() - lastFightOverTime > 2000) {
//升级了
if (myPlayer.exp >= myPlayer.nextExp) {
myPlayer.nextExp = myPlayer.exp * 3 / 2;
myPlayer.rank++;
}
//学会技能了
if (ranSkill != null) {
myPlayer.skillVec.addElement(ranSkill);
}
ranSkill = null;
this.ranEnemy = null;
myPlayer.attackStatus = -1;
if (curLevel.isCompleted()) {
myPlayer = new Player(ResourceLoader.StringResource[0]);
ranSkill = null;
this.ranEnemy = null;
this.setGameStatus(STATUS_MISSION_OVER);
} else {
this.setGameStatus(STATUS_WALKING);
}
}
}
} else if (gameStatus == STATUS_GAME_OVER
&& System.currentTimeMillis() - lastGameOverTime > 2000) {
this.setGameStatus(STATUS_MAIN_MENU);
}
}
private void handleCrashNpc(NPC npc) {
switch (npc.type) {
//对话类NPC
case 1:
//改变可对话NPC的朝向
changeNpcDirect(npc, myPlayer);
//主角undo
myPlayer.undoMove();
//在对话队列里面加入谈话内容
for (int i = 0; i < npc.talkContents.size(); i++) {
String t = (String) npc.talkContents.elementAt(i);
int index = t.indexOf('_');
String nt = t.substring(0, index).trim();
String pt = t.substring(index + 1).trim();
//第一次碰上
if (!npc.isAgainCrash) {
if (!nt.equals("null")) {
String temp = nt;
if (nt.startsWith("$")) {
temp = nt.substring(1);
}
pushMessage(npc.name + ":" + temp);
}
if (!pt.equals("null")) {
pushMessage(myPlayer.name + ":" + pt);
}
} else {
//非第一次碰上
if (nt.startsWith("$")) {
pushMessage(npc.name + ":" + nt.substring(1));
if (!pt.equals("null")) {
pushMessage(myPlayer.name + ":" + pt);
}
}
}
}
npc.isAgainCrash = true;
break;
//武器护具贩卖型NPC
case 2:
changeNpcDirect(npc, myPlayer);
myPlayer.undoMove();
this.saleType = 1;
this.weaponVec = npc.weaponVec;
this.jacketVec = npc.jacketVec;
setGameStatus(STATUS_POPMENU_CONFIRM);
break;
case 3:
//药品贩卖类型NPC
changeNpcDirect(npc, myPlayer);
myPlayer.undoMove();
this.saleType = 2;
this.medicVec = npc.medicVec;
setGameStatus(STATUS_POPMENU_CONFIRM);
break;
case 4:
break;
case 5:
break;
}
}
private void changeNpcDirect(NPC npc, Player player) {
if (npc.type == 1) {
int dir = Tools.comparePos(player.getSprite(Player.STATUS_WALK),
npc.sprite);
//英雄在NPC的左边
if (dir == Canvas.LEFT) {
if (npc.startFrameIndex + 2 <= npc.endFrameIndex) {
npc.sprite.setFrame(npc.startFrameIndex + 2);
}
//英雄在NPC的上边
} else if (dir == Canvas.UP) {
if (npc.startFrameIndex + 1 <= npc.endFrameIndex) {
npc.sprite.setFrame(npc.startFrameIndex + 1);
}
//英雄在NPC右边
} else if (dir == Canvas.RIGHT) {
if (npc.startFrameIndex + 3 <= npc.endFrameIndex) {
npc.sprite.setFrame(npc.startFrameIndex + 3);
}
} else {
npc.sprite.setFrame(npc.startFrameIndex);
}
}
}
private void handleCrashProperty(Property pro) {
}
/**
* 往消息队列中添加消息
*
* @param message
* 要添加到消息队列中的消息
*/
public void pushMessage(String message) {
if (messageQueue == null) {
messageQueue = new Vector();
}
if (message != null && message.trim().length() > 0) {
messageQueue.addElement(message);
}
}
/**
* 从消息队列中取消息
*
* @return 从消息队列中取第一条消息
*/
public String popMessage() {
String message = null;
if (messageQueue != null && messageQueue.size() > 0) {
message = (String) messageQueue.elementAt(0);
messageQueue.removeElementAt(0);
}
return message;
}
/**
* 游戏主循环
*/
public void run() {
while (isRunning && this.gameThread == Thread.currentThread()) {
long start = System.currentTimeMillis();
//只有MyGameCanvas是当前Displayable对象才进行
if (this.isShown()) {
//处理输入
input();
//调整
tick();
//绘制
render();
//输出
flushGraphics();
}
long end = System.currentTimeMillis();
int duration = (int) (end - start);
if (duration < GAME_LOOP_INTERVAL) {
try {
Thread.sleep(GAME_LOOP_INTERVAL - duration);
} catch (InterruptedException ie) {
}
}
}
}
public MyGameCanvas(MIDlet _midlet) {
super(false);
this.midlet = _midlet;
width = getWidth();
height = getHeight();
this.myRPGGameMIDlet = (MyRPGGameMIDlet) _midlet;
this.setFullScreenMode(true);
gs = new GameStore(this);
new InnerThread().start();
}
public void constructMap() {
Level level = this.curLevel;
Map map = level.curMap;
int hc = map.heroAppearCol;
int hr = map.heroAppearRow;
int tw = level.tileWidth;
int th = level.tileHeight;
if (myPlayer.row == -1 || myPlayer.col == -1) {
myPlayer.getSprite(Player.STATUS_WALK)
.setPosition(hc * tw, hr * th);
} else {
myPlayer.getSprite(Player.STATUS_WALK).setPosition(
myPlayer.col * tw, myPlayer.row * th);
}
lm = new LayerManager();
//添加英雄
lm.append(myPlayer.getSprite(Player.STATUS_WALK));
//添加NPC和道具
Vector v = map.rpgObjects;
for (int i = 0; i < v.size(); i++) {
RPGObject obj = (RPGObject) v.elementAt(i);
obj.sprite.setPosition(obj.colNo * tw, obj.rowNo * th);
obj.sprite.setFrame(obj.startFrameIndex);
lm.append(obj.sprite);
}
lm.append(map.collisionArea);
for (int i = map.walkableArea.size() - 1; i >= 0; i--) {
TiledLayer tl = (TiledLayer) map.walkableArea.elementAt(i);
lm.append(tl);
}
}
public void saveGame() {
gs.saveGame();
}
public void handLevelLoaded() {
curLevel.curMap = (Map) curLevel.maps
.get(new Integer(curLevel.curMapNo));
constructMap();
setGameStatus(MyGameCanvas.STATUS_WALKING);
engine = new GameInnerThread(MyRPGGameMIDlet.mc);
engine.start();
}
/*
* (non-Javadoc)
*
* @see org.gamecollege.j2me.rpg.RPGGameCanvas#loadGame()
*/
public void loadGame() {
myPlayer = new Player(ResourceLoader.StringResource[0]);
if (gs.canLoad()) {
gs.loadGame();
}
}
/*
* (non-Javadoc)
*
* @see org.gamecollege.j2me.rpg.RPGGameCanvas#pauseGame()
*/
public void pauseGame() {
this.isRunning = false;
}
/*
* (non-Javadoc)
*
* @see org.gamecollege.j2me.rpg.RPGGameCanvas#stopGame()
*/
public void sizeChanged(int w, int h) {
width = w;
height = h;
// startGame();
// new InnerThread().start();
}
public void drawSubImg(Graphics g, Image img, int x, int y, int swidth,
int sheight) {
g.setClip(x, y, swidth, sheight);
g.drawImage(img, x - swidth, y - sheight, 0);
g.setClip(0, 0, width, height);
}
private void drawConfirmMenu(Graphics g) {
g.drawImage(ResourceLoader.menu[3], width / 2, height / 2,
Graphics.HCENTER | Graphics.VCENTER);
g.setColor(255, 255, 255);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
Font.SIZE_SMALL));
switch (this.saleType) {
case 1:
//确认是否买卖武器
paintMenu(g, ResourceLoader.StringResource[1], width / 2, 80,
curMenuIndex == 0);
paintMenu(g, ResourceLoader.StringResource[2], width / 2, 100,
curMenuIndex == 1);
paintMenu(g, ResourceLoader.StringResource[3], width / 2, 120,
curMenuIndex == 2);
break;
case 2:
//确认是否买卖药品
paintMenu(g, ResourceLoader.StringResource[4], width / 2, 80,
curMenuIndex == 0);
paintMenu(g, ResourceLoader.StringResource[5], width / 2, 100,
curMenuIndex == 1);
paintMenu(g, ResourceLoader.StringResource[3], width / 2, 120,
curMenuIndex == 2);
break;
}
}
private void drawBuying(Graphics g) {
g.drawImage(ResourceLoader.menu[0], width / 2, 0, Graphics.TOP
| Graphics.HCENTER);
g.setColor(255, 255, 255);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
Font.SIZE_SMALL));
switch (this.saleType) {
case 1:
int y1 = 10;
g.drawImage(ResourceLoader.menu[8], width / 2, y1, Graphics.TOP
| Graphics.HCENTER);
String type = ResourceLoader.StringResource[27];
if (!isArm) {
type = ResourceLoader.StringResource[28];
}
g.drawString(type, width / 2, y1 + 1, Graphics.TOP
| Graphics.HCENTER);
y1 += 18;
g.drawString(ResourceLoader.StringResource[29] + ":"
+ myPlayer.money, width / 2, y1, Graphics.TOP
| Graphics.HCENTER);
Vector ps = weaponVec;
if (!isArm) {
ps = jacketVec;
}
y1 += 18;
//武器护具列表
for (int i = 0; i < ps.size(); i++) {
Property property = (Property) ps.elementAt(i);
paintMenu(g, property.name, width / 2, y1, i == curMenuIndex);
y1 += 18;
}
//退出按钮
paintMenu(g, ResourceLoader.StringResource[3], width / 2, y1,
curMenuIndex == jacketVec.size());
Property curP = null;
if (isArm == false) {
if (curMenuIndex < jacketVec.size()) {
curP = (Property) jacketVec.elementAt(curMenuIndex);
}
} else {
if (curMenuIndex < weaponVec.size()) {
curP = (Property) weaponVec.elementAt(curMenuIndex);
}
}
if (curP != null) {
String allDesc = ResourceLoader.StringResource[6] + ":"
+ curP.description + " "
+ ResourceLoader.StringResource[7] + ":" + curP.price;
g.drawString(allDesc, width / 2, 208, Graphics.HCENTER
| Graphics.BOTTOM);
}
break;
case 2:
//药品
int y = 10;
g.drawString(ResourceLoader.StringResource[29] + ":"
+ myPlayer.money, width / 2, y, Graphics.TOP
| Graphics.HCENTER);
y += 18;
for (int i = 0; i < this.medicVec.size(); i++) {
Property property = (Property) medicVec.elementAt(i);
paintMenu(g, property.name, width / 2, y, i == curMenuIndex);
y += 18;
}
paintMenu(g, ResourceLoader.StringResource[3], width / 2, y,
curMenuIndex == medicVec.size());
curP = null;
if (medicVec.size() > curMenuIndex) {
curP = (Property) medicVec.elementAt(curMenuIndex);
}
if (curP != null) {
String allDesc = ResourceLoader.StringResource[6] + ":"
+ curP.description + " "
+ ResourceLoader.StringResource[7] + ":" + curP.price;
g.drawString(allDesc, width / 2, 208, Graphics.HCENTER
| Graphics.BOTTOM);
}
break;
}
}
/**
*
*/
protected void keyPressed(int keycode) {
switch (gameStatus) {
case STATUS_WALKING:
switch (keycode) {
case Canvas.KEY_NUM7:
this.setGameStatus(STATUS_PLAYER_INFO);
break;
case Canvas.KEY_NUM9:
this.setGameStatus(STATUS_MISSION_INFO);
break;
case Canvas.KEY_NUM0:
this.stopGame();
setGameStatus(STATUS_MAIN_MENU);
break;
}
break;
case STATUS_MISSION_INFO:
this.setGameStatus(STATUS_WALKING);
break;
case STATUS_MISSION_OVER:
this.stopGame();
this.setGameStatus(STATUS_MAIN_MENU);
break;
}
}
/**
* 绘制一个菜单项
* @param g Graphics 对象
* @param menu 菜单内容
* @param x x坐标位置
* @param y y坐标位置
* @param isCurrent 菜单项是否为当前菜单项
*/
private void paintMenu(Graphics g, String menu, int x, int y,
boolean isCurrent) {
g.drawImage(ResourceLoader.menu[1], x, y, Graphics.HCENTER
| Graphics.TOP);
g.setColor(255, 255, 255);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
Font.SIZE_SMALL));
g.drawString(menu, x, y + 1, Graphics.HCENTER | Graphics.TOP);
if (isCurrent) {
g.drawImage(ResourceLoader.menu[2], x, y + 1, Graphics.HCENTER
| Graphics.TOP);
}
}
/**
* @param gameStatus
* The gameStatus to set.
*/
void setGameStatus(int gameStatus) {
if (gameStatus == STATUS_BUYING || gameStatus == STATUS_POPMENU_CONFIRM
|| gameStatus == STATUS_SALING
|| gameStatus == STATUS_PLAYER_INFO) {
curMenuIndex = 0;
} else if (gameStatus == STATUS_MAIN_MENU) {
hasStored = gs.canLoad();
}
this.gameStatus = gameStatus;
}
/**
* @return Returns the gameStatus.
*/
int getGameStatus() {
return gameStatus;
}
class InnerThread extends Thread {
public void run() {
//绘制Flash画面
drawFlash();
//资源装载器
ResourceLoader rl = new ResourceLoader();
//开始装载资源
rl.start();
//循环直到所有资源装载完毕
while (true) {
if (rl.loadOver) {
break;
}
try {
Thread.sleep(100);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
//资源装载完毕,出现主菜单画面
setGameStatus(MyGameCanvas.STATUS_MAIN_MENU);
//开启游戏主循环线程
MyGameCanvas.this.startGame();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -