📄 gameworld.java
字号:
package com.j2medev.chapter5.example.hold;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.LayerManager;
import com.j2medev.chapter5.utility.Clock;
import com.j2medev.chapter5.utility.ImageFont;
import com.j2medev.chapter5.utility.ImageUtilities;
import com.j2medev.chapter5.example.GameScreenHold;
import com.j2medev.chapter5.example.InputAction;
public class GameWorld {
public int width, height;
public int viewport_width;
public int viewport_height;
public int offset_x;
public int offset_y;
public int text_offset = 0;
public InputAction upAction = new InputAction();
public InputAction downAction = new InputAction();
public InputAction leftAction = new InputAction();
public InputAction rightAction = new InputAction();
public InputAction fireAction = new InputAction(
InputAction.MODE_INITAL_ONLY);
public Plane plane;
private Background bk;
public Bullets bullets;
LayerManager manager;
ImageFont fontbig, fontsmall;
Image bomb_ico;
public int state;
final static public int STATE_WAITFORSTART = 1;// 等待状态
final static public int STATE_RUNNING = 2;// 运行状态
final static public int STATE_RESULT = 3;// 显示成绩状态
static public Clock gameClock;
public GameWorld(int w, int h) {
state = STATE_WAITFORSTART;
gameClock = new Clock();
GameScreenHold.inputManager.mapkeyCodeToInputAction(
GameScreenHold.KEY_NUM2, upAction);
GameScreenHold.inputManager.mapkeyCodeToInputAction(-1, upAction);
GameScreenHold.inputManager.mapkeyCodeToInputAction(
GameScreenHold.KEY_NUM8, downAction);
GameScreenHold.inputManager.mapkeyCodeToInputAction(-2, downAction);
GameScreenHold.inputManager.mapkeyCodeToInputAction(
GameScreenHold.KEY_NUM4, leftAction);
GameScreenHold.inputManager.mapkeyCodeToInputAction(-3, leftAction);
GameScreenHold.inputManager.mapkeyCodeToInputAction(
GameScreenHold.KEY_NUM6, rightAction);
GameScreenHold.inputManager.mapkeyCodeToInputAction(-4, rightAction);
GameScreenHold.inputManager.mapkeyCodeToInputAction(-5, fireAction);
GameScreenHold.inputManager.mapkeyCodeToInputAction(
GameScreenHold.KEY_NUM5, fireAction);
width = w;
height = h;
viewport_width = w>176?176:w;
viewport_height = h>208?208:h;
offset_x = (width - viewport_width) / 2;
offset_y = (height - viewport_height) / 2;
plane = Plane.createPlane(this);
bk = Background.createBackground();
bullets = Bullets.createBullets(this);
fontbig = new ImageFont(ImageUtilities.createImage("/b_number.png"),
10, 19, new char[] { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9' });
fontsmall = new ImageFont(ImageUtilities.createImage("/s_number.png"),
5, 7, new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9' });
bomb_ico = ImageUtilities.createImage("/bomb_icon.png");
manager = new LayerManager();
manager.append(plane.rest);
manager.append(plane.explosion);
manager.append(plane);
manager.append(bk);
manager.setViewWindow(0, 0, viewport_width, viewport_height);
}
public void reset() {
gameClock.restart();
plane.reset();
bullets.initBullets();
resetText_Offset();
}
public void paint(Graphics g) {
g.setColor(255, 255, 255);
g.fillRect(0, 0, width, height);
manager.paint(g, offset_x, offset_y);
bullets.paint(g, offset_x, offset_y);
g.drawImage(bomb_ico, offset_x, offset_y + viewport_height,
Graphics.BOTTOM | Graphics.LEFT);
if (state != STATE_WAITFORSTART) {
fontbig.drawString(g, new Integer(
(int) (gameClock.getTime() / 1000)).toString(), offset_x+viewport_width/2-5,offset_y+60);
fontsmall.drawString(g, new Integer(plane.bombnum).toString(), offset_x
+ bomb_ico.getWidth(), offset_y + viewport_height - 10);
}
if (state == STATE_WAITFORSTART) {
String str = "Press FIRE to Start!";
printPrompt(g, str);
}
if (state == STATE_RESULT) {
String str = result(gameClock.getTime());
printPrompt(g, str);
}
}
public void update() {
switch (state) {
case STATE_WAITFORSTART:
if (fireAction.isPressed()) {
state = STATE_RUNNING;
reset();
}
break;
case STATE_RUNNING:
bk.update();
plane.update();
bullets.updateBullets(plane, plane.state == Plane.STATE_NORMAL);
if (plane.state == Plane.STATE_DEAD) {
state = STATE_RESULT;
gameClock.pause();
}
break;
case STATE_RESULT:
bk.update();
plane.update();
bullets.updateBullets(plane, false);
if (fireAction.isPressed()) {
state = STATE_RUNNING;
reset();
}
break;
}
}
public String result(long gametime) {
String returnStr;
if (gametime < 10000) {
returnStr = "Do you play with foot?";
} else if (gametime < 16000) {
returnStr = "Come boy, you can do it!";
} else if (gametime < 20000) {
returnStr = "What a pity! try again.";
} else if (gametime < 25000) {
returnStr = "I see you're a real man.";
} else if (gametime < 30000) {
returnStr = "Great! I know you have talent with the game.";
} else if (gametime < 40000) {
returnStr = "Can't belive, I met a game master.";
} else {
returnStr = "Oh my god! Are you a human?";
}
return returnStr + " Press FIRE to restart!";
}
public void printPrompt(Graphics g, String str) {
int oldx = g.getClipX();
int oldy = g.getClipY();
int oldw = g.getClipWidth();
int oldh = g.getClipHeight();
int newx = offset_x;
int newy = offset_y + viewport_height / 2 - 20;
int neww = viewport_width;
int newh = 30;
if ((text_offset -= 2) < -g.getFont().stringWidth(str))
resetText_Offset();
// System.out.println(text_offset);
g.setColor(54, 34, 45);
g.setClip(newx, newy, neww, newh);
g.fillRect(newx, newy, neww, newh);
g.setColor(255, 255, 255);
g.drawString(str, newx + text_offset, newy + 20, Graphics.BASELINE
| Graphics.LEFT);
g.setClip(oldx, oldy, oldw, oldh);
}
private void resetText_Offset() {
text_offset = viewport_width;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -