J2ME游戏代码示例——俄罗斯方块(1)[Java编程]
本文“J2ME游戏代码示例——俄罗斯方块(1)[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
下面是我写的一个简单的俄罗斯方块游戏代码,代码中主要包含3个类:
lTerrisMIDlet——MIDlet类
lGameCanvas——游戏界面类
lGameEngine——游戏逻辑类
利用WTK2.5.1运行通过.
具体代码以下:
//MIDlet类
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
public class TerrisMIDlet extends MIDlet {
public TerrisMIDlet() {
Display.getDisplay(this).setCurrent(new GameCanvas());
}
protected void destroyApp(boolean arg0){}
protected void pauseApp() {}
protected void startApp() {}
}
//界面类
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
/**
* 俄罗斯方块游戏界面
*/
public class GameCanvas extends Canvasimplements Runnable{
GameEngine engine = new GameEngine();
int width;
int height;
/**游戏完毕变量*/
boolean gameOver = false;
/**游戏暂停变量*/
boolean isPaused = false;
String pauseStr = "暂停";
String continueStr = "持续";
public GameCanvas(){
//全屏幕
setFullScreenMode(true);
width = getWidth();
height = getHeight();
//启动线程
Thread th = new Thread(this);
th.start();
}
protected void paint(Graphics g) {
//清屏
clearScreen(g);
//绘制边框
paintBoard(g);
//绘制后台
engine.paintMap(g);
//绘制当前下落方块
engine.paintBrick(g);
//绘制下一个方块
engine.paintNextBrick(g);
//绘制关卡和游戏得分
engine.paintLevelAndScore(g);
//绘制游戏完毕
if(gameOver){
g.drawString("Game Over", 30, 85,
Graphics.TOP | Graphics.LEFT);
}
//绘制暂停按钮文字
if(isPaused){
g.drawString(pauseStr,width,height,
Graphics.RIGHT | Graphics.BOTTOM);
}else{
g.drawString(continueStr,width,height,
Graphics.RIGHT | Graphics.BOTTOM);
}
}
public void keyPressed(int keyCode){
if(keyCode == -7){ //右软键,暂停掌握
isPaused = !isPaused;
}
if(!isPaused){
int action = getGameAction(keyCode);
switch(action){
case UP:
case FIRE:
engine.changeShape();
break;
case LEFT:
engine.moveToLeft();
break;
case RIGHT:
engine.moveToRight();
break;
case DOWN:
engine.highSpeed();//加快
break;
}
}
repaint();
}
public void keyReleased(int keyCode){
int action = getGameAction(keyCode);
switch(action){
case DOWN:
engine.normalSpeed(); //恢复正常速度
break;
}
}
/**
以上是“J2ME游戏代码示例——俄罗斯方块(1)[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |