主要是学习游戏速度的调整,小球移动速度的改变,连续按键与非连续按键的控制。
最终效果图:
下边帖出代码,MIDlet类就不贴了
GameCanvas类
import javax.microedition.lcdui.*;
/**
* 小球速度的改变
*
* @author kf156(亚日)
*
*/
public class GameCanvas extends Canvas implements Runnable {
public final static int KEY_UP = -1;// 上
public final static int KEY_DOWN = -2;// 下
public final static int KEY_LEFT = -3;// 左
public final static int KEY_RIGHT = -4;// 右
public final static int SPEED_UP = -6; // 功能键 左 1
public final static int SPEED_DOWN = -7; // 功能键 右 2
public boolean running;
private Thread thread;
private int[] key = new int[4];
private int width;
private int height;
private int Speed;// 游戏速度(利用线程休眠时间来控制)
public Font font;
private final int DIAMETER = 20;// 小球直径
public boolean flagX, flagY;// XY方向标记
int ballX, ballY;// 小球坐标
int x, y;// XY方向移动速度
public GameCanvas() {
setFullScreenMode(true);
font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
Font.SIZE_MEDIUM);
width = getWidth();
height = getHeight();
running = true;
x = 4;
y = 4;
ballX = 0;
ballY = 0;
flagX = true;
flagY = true;
Speed = 50;
startThread();
}
public void startThread() {
if (thread == null)
thread = new Thread(this);
thread.start();
}
public void showNotify() {
}
public void init() {
}
public void free() {
}
public void input() {
key[0] = key[2];
key[1] = key[3];
key[2] = 0;
}
public void update() {
switch (key[0]) {// 非连接按键
case KEY_UP:
if (y > 0)
y--;
break;
case KEY_DOWN:
if (y < 20)
y++;
break;
case KEY_LEFT:
if (x > 0)
x--;
break;
case KEY_RIGHT:
if (x < 20)
x++;
break;
}
switch (key[1]) {// 连续按键控制
case SPEED_UP:// 加速
Speed--;
break;
case SPEED_DOWN:// 减速
Speed++;
break;
}
// 限制速度范围
if (Speed < 1)
Speed = 1;
else if (Speed > 50)
Speed = 50;
ballX += flagX ? x : -x;// 判断水平移动方向
ballY += flagY ? y : -y;// 判断垂直移动方向
// 边界判断
if (ballX > width - DIAMETER) {// 右边界
ballX = width - DIAMETER;
flagX = false;
} else if (ballX < 0) {// 左边界
ballX = 0;
flagX = true;
}
if (ballY > height - DIAMETER) {// 下边界
ballY = height - DIAMETER;
flagY = false;
} else if (ballY < 0) {// 上边界
ballY = 0;
flagY = true;
}
}
protected void paint(Graphics g) {
// 背景填充
g.setColor(0);
g.fillRect(0, 0, width, height);
// 绘制小球
g.setColor(0xff0000);
g.fillArc(ballX, ballY, DIAMETER, DIAMETER, 0, 360);
// 文字
g.setColor(0x00ffff);
g.drawString("游戏速度为:" + (51 - Speed), width >> 1, (height >> 1)
- font.getHeight(), Graphics.HCENTER | Graphics.TOP);
g.drawString("X方向移动速度:" + x, width >> 1, height >> 1, Graphics.HCENTER
| Graphics.TOP);
g.drawString("Y方向移动速度:" + y, width >> 1, (height >> 1)
+ font.getHeight(), Graphics.HCENTER | Graphics.TOP);
g.drawString("加速", 0, height - font.getHeight(), 20);
g.drawString("减速", width, height - font.getHeight(), 24);
}
public void run() {
int debug = 0;
try {
while (running) {
// 真机上用次方法
long stare = System.currentTimeMillis();
input();
debug = 1;
update();
debug = 2;
repaint();
serviceRepaints();
long end = System.currentTimeMillis() - stare;
if (end < Speed)
Thread.sleep(Speed - end);
}
GameMidlet.quitApp();
} catch (Exception e) {
System.err.println("run error:" + e + " debug:" + debug);
}
}
/***************************************************************************
* 重载的FullCanvas的按键触发的方法
*
* @param keyCode
* int 系统传入的按键代码
**************************************************************************/
protected void keyPressed(int keyCode) {
key[2] = key[3] = keyCode;
}
/***************************************************************************
* 重载的FullCanvas的重复一个按键触发的方法
*
* @param keyCode
* 系统传入的按键代码
**************************************************************************/
protected void keyRepeated(int keyCode) {
}
/***************************************************************************
* 重载的FullCanvas的释放一个按键触发的方法
*
* @param keyCode
* 系统传入的按键代码
**************************************************************************/
public void keyReleased(int keyCode) {
key[3] = 0;
}
}
工程已经上传,觉得网页上看不方便的朋友也可直接下载:http://download.csdn.net/source/1573193