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;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
J2ME 学习范例源代码
共1046个文件
class:389个
java:215个
xml:112个
需积分: 1 10 下载量 47 浏览量
2009-09-26
15:10:42
上传
评论
收藏 2.36MB RAR 举报
温馨提示
J2ME 学习范例源代码,包括登录、发送短信、HTTP之类的代码。供初学者参考。
资源推荐
资源详情
资源评论
收起资源包目录
J2ME 学习范例源代码 (1046个子文件)
GameWorld.class 7KB
GameWorld.class 7KB
GameWorld.class 7KB
SMSMIDlet.class 6KB
SMSMIDlet.class 6KB
SMSMIDlet.class 6KB
SMSMIDlet.class 6KB
SMSMIDlet.class 6KB
SMSMIDlet.class 6KB
Bullets.class 5KB
SecurityMIDlet.class 5KB
SecurityMIDlet.class 5KB
Bullets.class 5KB
Bullets.class 5KB
Client.class 4KB
AlarmMIDlet.class 4KB
Client.class 4KB
Client.class 4KB
AlarmMIDlet.class 4KB
AlarmMIDlet.class 4KB
ForMIDlet.class 4KB
SecurityMIDlet.class 4KB
SecurityMIDlet.class 4KB
SecurityMIDlet.class 4KB
SecurityMIDlet.class 4KB
ImageMIDlet.class 4KB
HttpGET.class 4KB
ForMIDlet.class 4KB
ForMIDlet.class 4KB
GameScreenInputEnable.class 4KB
ImageMIDlet.class 4KB
ImageMIDlet.class 4KB
HttpGET.class 4KB
HttpGET.class 4KB
GameScreenInputEnable.class 4KB
GameScreenInputEnable.class 4KB
HttpHandler.class 4KB
Plane.class 4KB
ImageMidlet.class 4KB
SplashCanvas.class 4KB
ImageMidlet.class 4KB
MainCanvas.class 4KB
PhoneBookMIDlet.class 4KB
PhoneBookMIDlet.class 4KB
SplashCanvas.class 4KB
SplashCanvas.class 4KB
TurboMidlet.class 4KB
Plane.class 4KB
Plane.class 4KB
LoginMIDlet.class 4KB
MainCanvas.class 4KB
MainCanvas.class 4KB
MainServlet.class 4KB
PhoneBookMIDlet.class 4KB
PhoneBookMIDlet.class 4KB
ImageMidlet.class 4KB
ImageMidlet.class 4KB
ImageMidlet.class 4KB
ImageMidlet.class 4KB
PhoneBookMIDlet.class 4KB
PhoneBookMIDlet.class 4KB
SendMessageUI.class 3KB
SendMessageUI.class 3KB
LoginMIDlet.class 3KB
LoginMIDlet.class 3KB
TurboMidlet.class 3KB
TurboMidlet.class 3KB
HttpHandler.class 3KB
HttpHandler.class 3KB
SendMessageUI.class 3KB
SendMessageUI.class 3KB
SendMessageUI.class 3KB
SendMessageUI.class 3KB
Server.class 3KB
ChatMIDlet.class 3KB
AlarmModel.class 3KB
ChatMIDlet.class 3KB
Server.class 3KB
ChatMIDlet.class 3KB
Server.class 3KB
ListExclusiveIconMidlet.class 3KB
ListExclusiveIconMidlet.class 3KB
ListExclusiveIconMidlet.class 3KB
StringItemMidlet.class 3KB
HttpHandler.class 3KB
AlarmModel.class 3KB
AlarmModel.class 3KB
GameScreen.class 3KB
Database.class 3KB
StringItemMidlet.class 3KB
StringItemMidlet.class 3KB
HttpHandler.class 3KB
HttpHandler.class 3KB
GameScreen.class 3KB
GameScreen.class 3KB
GameScreenHold.class 3KB
GameScreen.class 3KB
ListExclusiveMidlet.class 3KB
HttpThread.class 3KB
SMSMIDlet$MessageReader.class 3KB
共 1046 条
- 1
- 2
- 3
- 4
- 5
- 6
- 11
资源评论
Neetgo
- 粉丝: 24
- 资源: 20
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功