📄 gamedemo.java
字号:
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
public class GameDemo extends MIDlet implements CommandListener{
private LayerCanvas gameCanvas;
private final static Command exit = new Command("Exit", Command.EXIT, 1);
public void startApp() {
if (gameCanvas== null) {
gameCanvas= new LayerCanvas();
gameCanvas.addCommand(exit);
gameCanvas.setCommandListener(this);
}
gameCanvas.startGame();
Display.getDisplay(this).setCurrent(gameCanvas);
}
public void pauseApp() {
if (gameCanvas != null){
gameCanvas.stopGame();
}
}
public void destroyApp(boolean unconditional) {
if (gameCanvas != null){
gameCanvas.stopGame();
}
}
public void commandAction(Command c, Displayable s){
if (c == exit) {
destroyApp(false);
notifyDestroyed();
}
}
}
//该类继承了GameCanvas类,并封装了游戏的运行逻辑
class LayerCanvas extends GameCanvas implements Runnable{
//指示游戏是否结束
boolean gameOver;
//绘制图像缓冲区的Graphics对象
private Graphics g;
//游戏中用到的Sprite对象
private Sprite sprite;
//Sprite的位置
private int x,y;
//Sprite的移动速度
private int speed=5;
//GameCanvas的宽度和高度
private int width,height;
//游戏背景TiledLayer对象
private TiledLayer background;
//管理各个Layer的LayerManager对象
private LayerManager mng;
//Sprite中每帧图像的宽度和高度
private static final int SPRIT_FRAME_WIDTH = 45;
private static final int SPRIT_FRAME_HEIGHT = 18;
public LayerCanvas (){
super(true);
//获得绘制图像缓冲区的Graphics对象
g=getGraphics();
//创建管理各个Layer的LayerManager对象
mng=new LayerManager();
//用图像文件mosquito.png创建Image对象
Image img=null;
try{
img=Image.createImage("/mosquito.png");
}
catch(java.io.IOException e){
System.out.println("can not open image file:mosquito.png");
}
//用Image对象创建sprite对象
sprite=new Sprite(img,SPRIT_FRAME_WIDTH,SPRIT_FRAME_HEIGHT);
//把创建的sprite对象添加到LayerManager中
mng.append(sprite);
//创建背景图像
createBackground();
//设置Sprite的初始位置
x=0;
y=20;
//获得GameCanvas的宽度和高度
width=getWidth();
height=getHeight();
}
public void createBackground(){
try{
Image img=Image.createImage("/tile.png");
background=new TiledLayer(12,4,img,32,32);
background.createAnimatedTile(5);
int[] map = {
0, 0, 1, 3, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 4, 4, 3, 0, 0, 0, 0, 1, 2, 2,
1, 4, 4, 4, 4, 3, 0, 0, 1, 4, 4, 4,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
};
for(int i=0;i<map.length;i++){
int column=i%12;
int row=(i-column)/12;
background.setCell(column, row, map[i]);
}
mng.append(background);
}
catch(java.io.IOException e){
System.out.println("can not open image file:tile.png");
}
}
//开始游戏运行
public void startGame(){
gameOver=false;
Thread t=new Thread(this);
t.start();
}
//停止游戏运行
public void stopGame(){
gameOver=true;
}
//游戏主循环
public void run(){
//用于显示背景上的水流动画
int i=5;
//临时保存Sprite的位置,用于在发生碰撞冲突时恢复原来的位置
int tempx=x,tempy=y;
while(!gameOver){
//查询键盘状态
int keyState=this.getKeyStates();
//如果方向键按下,则移动Sprite,并保证Sprite在游戏场景区域内
if((keyState&RIGHT_PRESSED)!=0){
tempx=x-speed;
if(tempx<width-12*32)
tempx=width-12*32;
}
if((keyState&LEFT_PRESSED)!=0){
tempx=x+speed;
if(tempx>0)
tempx=0;
}
if((keyState&UP_PRESSED)!=0){
tempy=y-speed;
if(tempy<20)
tempy=20;
}
if((keyState&DOWN_PRESSED)!=0){
tempy=y+speed;
if(tempy>height)
tempy=height;
}
//设置背景和Sprite的新位置,以产生Sprite移动的效果
background.setPosition(tempx,height-128);
sprite.setPosition(40,tempy);
//如果发生碰撞冲突,则恢复原来的位置
if(sprite.collidesWith(background,false)){
background.setPosition(x,height-128);
sprite.setPosition(40,y);
}
//没有碰撞冲突,就把新位置保存下来
else{
x=tempx;
y=tempy;
}
//清除背景
g.setColor(128,255,255);
g.fillRect(0,0,width,height);
//设置水流动画
i=i+1;
if(i>7)
i=5;
background.setAnimatedTile(-1,i);
//Sprite后移一帧
sprite.nextFrame();
//设置显示区域
mng.setViewWindow(0,20,width,height-20);
//绘制游戏场景
mng.paint(g,0,20);
//绘制屏幕上方的游戏提示区域
g.setColor(0,0,0);
g.fillRect(0,0,width,20);
g.setColor(255,255,0);
g.fillRect(0,5,width,10);
g.setColor(255,0,0);
int pos=(40-x)*width/(12*32);
g.fillArc(pos-5,5,10,10,0,360);
//把绘制好的图像由缓冲区复制到实际屏幕
flushGraphics();
//延时
try {
Thread.sleep(50);
}
catch (InterruptedException ie) {
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -