下面是我寫的一個簡單的俄羅斯方塊游戲代碼,代碼中主要包含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;
}
}
/**
* 繪制游戲區域邊框
* @param g 畫筆
*/
private void paintBoard(Graphics g){
g.drawRect(0,0,
engine.MAXCOL * engine.CELLWIDTH,
engine.MAXROW * engine.CELLWIDTH);
}
/**
* 清屏
* @param g 畫筆
*/
private void clearScreen(Graphics g){
g.setColor(0xffffff);
g.fillRect(0,0,width,height);
g.setColor(0);
}
public void run() {
try{
while(true){
//延時
Thread.sleep(engine.speed / engine.times);
//邏輯處理
//如果不處於暫停狀態
if(!isPaused){
//方塊下落
engine.brickDown();
//游戲是否結束
if(engine.isGameOver()){
//顯示游戲結束
gameOver = true;
//重畫
repaint();
//結束線程
break;
}
}
//重繪
repaint();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
//游戲邏輯類
import java.util.*;
import javax.microedition.lcdui.*;
/**
* 游戲數據和邏輯類
* 未實現關卡變化的邏輯
*/
public class GameEngine {
/**方塊的信息, 0代表無內容,1代表有內容 */
int[][][][] brick = {
{
{
{ 0, 1, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 1, 1, 0 },
{ 0, 0, 0, 0 }
},
{
{ 0, 0, 0, 0 },
{ 0, 1, 1, 1 },
{ 0, 1, 0, 0 },
{ 0, 0, 0, 0 }
},
{
{ 0, 1, 1, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 0 }
},
{
{ 0, 0, 0, 0 },
{ 0, 0, 1, 0 },
{ 1, 1, 1, 0 },
{ 0, 0, 0, 0 }
}
},
{
{
{ 0, 0, 1, 0 },
{ 0, 0, 1, 0 },
{ 0, 1, 1, 0 },
{ 0, 0, 0, 0 }
},
{
{ 0, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 1, 1, 1 },
{ 0, 0, 0, 0 }
},
{
{ 0, 0, 0, 0 },
{ 0, 1, 1, 0 },
{ 0, 1, 0, 0 },
{ 0, 1, 0, 0 }
},
{
{ 0, 0, 0, 0 },
{ 1, 1, 1, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 0 }
}
},
{
{
{0,1,0,0},
{0,1,0,0},
{0,1,0,0},
{0,1,0,0}
},
{
{0,0,0,0},
{1,1,1,1},
{0,0,0,0},
{0,0,0,0}
}
},
{
{
{0,0,0,0},
{0,1,1,0},
{0,1,1,0},
{0,0,0,0}
}
},
{
{
{0,0,1,0},
{0,1,1,0},
{0,1,0,0},
{0,0,0,0}
},
{
{0,0,0,0},
{0,1,1,0},
{0,0,1,1},
{0,0,0,0}
}
},
{
{
{0,1,0,0},
{0,1,1,0},
{0,0,1,0},
{0,0,0,0}
},
{
{0,0,0,0},
{0,1,1,0},
{1,1,0,0},
{0,0,0,0}
}
},
{
{
{0,0,0,0},
{0,0,1,0},
{0,1,1,1},
{0,0,0,0}
},
{
{0,0,0,0},
{0,1,0,0},
{0,1,1,0},
{0,1,0,0}
},
{
{0,0,0,0},
{0,1,1,1},
{0,0,1,0},
{0,0,0,0}
},
{
{0,0,1,0},
{0,1,1,0},
{0,0,1,0},
{0,0,0,0}
}
}
};
/**行數*/
final int MAXROW = 19;
/**列數*/
final int MAXCOL = 10;
/**
* 游戲界面信息,0代表無內容,1代表有內容
*/
int[][] map = new int[MAXROW][MAXCOL];
/**每個單元格的寬度和高度*/
public static final int CELLWIDTH = 10;
/**下一個方塊的類型*/
int nextBrickType = 0;
//當前下落的方塊
/**當前格子的類型*/
int brickType;
/**當前變形信息*/
int index;
/**當前行*/
int cRow;
/**當前列*/
int cCol;
/**速度*/
int speed = 500;
/**加速度*/
int times = 1;
/**當前關卡*/
int level = 0;
/**積分,每消一行加10分*/
int score = 0;
Random r = new Random();
public GameEngine(){
//產生方塊
gernerateBrick();
}
/**
* 繪制一個格子
* @param g 畫筆對象
* @param x 格子左上角的x坐標
* @param y 格子左上角的y坐標
*/
private void paintCell(Graphics g,int x,int y){
//繪制黑色邊框
g.setColor(0);
g.drawRect(x,y,CELLWIDTH,CELLWIDTH);
//繪制邊框
g.setColor(0xffffff);
g.fillRect(x+1,y+1,CELLWIDTH-2,CELLWIDTH-2);
//黑塊
g.setColor(0);
g.fillRect(x + 2, y + 2,
CELLWIDTH - 4, CELLWIDTH - 4);
}
public void paintMap(Graphics g){
//循環繪制地圖
for(int row = 0;row < map.length;row++){
for(int col = 0;col < map[row].length;col++){
//是否繪制
if(map[row][col] == 1){
paintCell(g,col * CELLWIDTH,row * CELLWIDTH);
}
}
}
}
/**