📄 gamescreeninputenable.java
字号:
package com.j2medev.chapter5.example;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.*;
import java.util.Random;
final class GameScreenInputEnable extends GameCanvas implements Runnable {
private Graphics graphics; //保存图形环境实例
private boolean runningFlag = false; //线程运行标志
private static final long TIME_PER_FRAME = 100;// 每一帧的周期
private int width;//屏幕宽度
private int height;//屏幕高度
InputManager inputManager = new InputManager();
///////////////////////自定义部分开始/////////////////////////////////
InputAction upAction = new InputAction(InputAction.MODE_INITAL_ONLY);
InputAction downAction = new InputAction();
private Random random = new Random(); //设置一个随机数;
public static final int BLUE = 0x000000ff;
private int x, y, vx, vy;
String str = "J2ME Game World!";
///////////////////////自定义部分结束/////////////////////////////////
GameScreenInputEnable(boolean flag) { //构造方法;
super(flag);
graphics = getGraphics();//取得图形环境实例
width = getWidth();
height = getHeight();
init();
}
void init() { //初始化游戏数据;
///////////////////////自定义部分开始/////////////////////////////////
x = width / 2;
y = height / 2;
vx = random.nextInt(2) + 1;
vy = random.nextInt(2) + 1;
inputManager.mapkeyCodeToInputAction(KEY_NUM2, upAction);
inputManager.mapkeyCodeToInputAction(KEY_NUM8, downAction);
inputManager.mapkeyCodeToInputAction(-1,upAction);
inputManager.mapkeyCodeToInputAction(-2,downAction);
///////////////////////自定义部分结束/////////////////////////////////
}
// private long leftLastPressdTime = 0;
// private long limit = 100;//定100ms的阈值
//
// private boolean leftFlag=false;//标记
private void input() { //处理输入
///////////////////////自定义部分开始/////////////////////////////////
if(upAction.isPressed()){
y-=5;
}
if(downAction.isPressed()){
y+=3;
}
///////////////////////自定义部分结束/////////////////////////////////
}
private void logic() { //更新逻辑
///////////////////////自定义部分开始/////////////////////////////////
x += vx;
y += vy;
Font font = graphics.getFont();
if (x <= 0 || x + font.stringWidth(str) >= width) {
vx = -vx;
}
if (y <= 0 || y + 20 >= height) {
vy = -vy;
}
///////////////////////自定义部分结束/////////////////////////////////
}
private void render(Graphics g) { //渲染画面
///////////////////////自定义部分开始/////////////////////////////////
g.setColor(BLUE);
g.fillRect(0, 0, width, height);
g.setColor(0x00ffffff);
g.drawString(str, x, y, Graphics.LEFT | Graphics.TOP);
//System.out.println(width+","+height);
///////////////////////自定义部分结束/////////////////////////////////
}
synchronized public void start() {
if (!runningFlag) {
runningFlag = true;
Thread th = new Thread(this);//启动线程
th.start();
}
}
synchronized public void stop() {
runningFlag = false;
}
public void run() {
System.out.println("Game main loop start");
while (runningFlag) {
long startTime = System.currentTimeMillis();
input();
logic();
render(graphics);
flushGraphics();//更新显示屏幕
long elapsedTime = System.currentTimeMillis() - startTime;
if (elapsedTime < TIME_PER_FRAME) {
try {
Thread.sleep(TIME_PER_FRAME - elapsedTime);
} catch (InterruptedException ex) {
}
}
}
System.out.println("Game main loop stop");
}
protected void keyPressed(int keyCode) {
inputManager.keyPressed(keyCode);
}
protected void keyReleased(int keyCode) {
inputManager.keyReleased(keyCode);
}
protected void keyRepeated(int keyCode) {
inputManager.keyRepeated(keyCode);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -