?? tetrisgamecanvas.java
字號:
//totalscore 得分
import javax.microedition.lcdui.*;
class TetrisGameCanvas extends Canvas implements Runnable,CommandListener {
Thread t;
private Command goHome;
private PetMIDlet midlet;
private Command startCommand = new Command("Start",Command.SCREEN,1);
private Command pauseCommand = new Command("Pause", Command.SCREEN, 1);
private static final int CELLNUM = 7;//方塊類型總數
private static final int WIDTH_NUMBER = 10; //寬10方格
private static final int HEIGHT_NUMBER = 16;//高16方格
private static final int WIDTH = 100; //寬10方格
private static final int HEIGHT = 160;//高16方格
private static final int GRIDWIDTH = 10;
private static final int bx = 10;
private static final int by = 10;
private int grid[][];
private int row = 0, column = 0;//坐標從0開始(row,column) =( 行,列)
private boolean ifhaddown = false;//判斷方塊是否已經不在下落,false表示不在下落
private int[][] getboardcell = new int[4][4];//存儲獲得當前得方塊
private boolean pause = false;//是否暫停游戲
private boolean begin = false;//是否開始游戲
private int cellstyle;//方塊類型
private int totalscore; //得分
private String text = "得分:";
Board board = new Board(WIDTH, HEIGHT, GRIDWIDTH);
BoardCells bcell = new BoardCells();
private Image offScreenImage;
private Graphics offScreenGraphics;
TetrisGameCanvas(PetMIDlet midlet) {
this.midlet = midlet;
goHome=new Command("我要回家",Command.BACK,1);
addCommand(goHome);
addCommand(startCommand);
addCommand(pauseCommand);
setCommandListener(this);
t = new Thread(this);
t.start();
// 初始化離屏緩沖區
if (offScreenImage == null) {
offScreenImage = Image.createImage(this.getWidth(), this
.getHeight());
offScreenGraphics = offScreenImage.getGraphics();
}
//定義方格
grid = new int[HEIGHT_NUMBER][WIDTH_NUMBER];
clearBoard();
}
protected void start() {
pause = false;
}
protected void pause() {
pause = true;
}
private void clearBoard() {
int i, j;
//初始化方塊是否顯示
for (i = 0; i <= HEIGHT_NUMBER - 1; i++)
for (j = 0; j <= WIDTH_NUMBER - 1; j++) {
grid[i][j] = 0;
}
}
//判斷方塊占用得行數和列數 ,即存在1的行數和列數
private int[] GetRowColumn(int x, int y, int[][] bcell) {
int[] rtn = new int[2];
int i, j, getx = 4, gety = 4;
boolean isAllZero = false;
//判斷方塊占用得行數和列數
for (i = 4; i >= 1; i--) {
for (j = 1; j <= 4; j++) {
if (bcell[j - 1][i - 1] == 0) {
isAllZero = true;
} else {
isAllZero = false;
break;
}
}
if (isAllZero == true)
gety = gety - 1;
isAllZero = false;
}
isAllZero = false;
for (i = 4; i >= 1; i--) {
for (j = 1; j <= 4; j++) {
if (bcell[i - 1][j - 1] == 0) {
isAllZero = true;
} else {
isAllZero = false;
break;
}
}
if (isAllZero == true)
getx = getx - 1;
isAllZero = false;
}
rtn[0] = getx;
rtn[1] = gety;
return rtn;
}
//是否可以移動,翻轉等;
private boolean Movable(int x, int y, int[][] bcell) {
int i, j, getx, gety;
int[] rtn = new int[2];
//獲得方塊占用的行數和列數
rtn = GetRowColumn(x, y, bcell);
getx = rtn[0];
gety = rtn[1];
//如果越界,返回不能移動
if ((x + getx > HEIGHT_NUMBER) || (y + gety > 10) || (x < 0) || (y < 0)) {
return false;
}
//是否與以前下落得方塊碰撞
for (i = 1; i <= getx; i++)
for (j = 1; j <= gety; j++) {
if (grid[x + i - 1][y + j - 1] * bcell[i - 1][j - 1] == 1)
return false;
}
return true;
}
//在不會沖突得時候,拷貝單元
private void copycell(int x, int y, int[][] bcell) {
int i, j, getx, gety;
int[] rtn = new int[2];
//獲得某一形狀的方塊的行數和列數
rtn = GetRowColumn(x, y, bcell);
getx = rtn[0];
gety = rtn[1];
for (i = 1; i <= getx; i++)
for (j = 1; j <= gety; j++) {
if (bcell[i - 1][j - 1] == 1)
grid[x + i - 1][y + j - 1] = 1;
}
}
//清除單元,也就是恢復拷貝單元 前的操作
private void clearcell(int x, int y, int[][] bcell) {
int i, j, getx, gety;
int[] rtn = new int[2];
//獲得某一形狀的方塊的行數和列數
rtn = GetRowColumn(x, y, bcell);
getx = rtn[0];
gety = rtn[1];
for (i = 1; i <= getx; i++)
for (j = 1; j <= gety; j++) {
if (bcell[i - 1][j - 1] == 1)
grid[x + i - 1][y + j - 1] = 0;
}
}
//清除沒有空白的一行方塊 ,并把上方的方塊下移
//可以消行,返回true
private boolean ClearLine() {
boolean isLine = false, hasline = false;
int i, j, m, n;
for (i = HEIGHT_NUMBER - 1; i >= 4; i--) {
for (j = 0; j <= WIDTH_NUMBER - 1; j++) {
if (grid[i][j] == 0) {
isLine = false;
break;
} else {
isLine = true;
}
}
//如果是整行,移動方塊
if (isLine == true) {
for (m = i; m >= 4; m--)
for (n = 0; n <= WIDTH_NUMBER - 1; n++) {
grid[m][n] = grid[m - 1][n];
}
i = i + 1;
totalscore = totalscore + 1;
//顯示當前得分
text = "得分:" + String.valueOf(totalscore);
hasline = true;
}
}
return hasline;
}
private boolean GameOver() {
boolean gameover = false;
int i, j;
for (i = HEIGHT_NUMBER - 1; i >= 0; i--) {
for (j = 0; j <= WIDTH_NUMBER - 1; j++) {
if (grid[i][j] == 1) {
gameover = false;
break;
} else {
gameover = true;
}
}
//如果是整行,移動方塊
if (gameover == true) {
if (i <= 1)
return true;
else
return false;
}
}
return false;
}
//畫3維方格
public static void drawBrick(int px, int py, int width, Graphics g) {
//畫白邊
g.setColor(255, 255, 255);
g.fillRect(px, py, 1, width);
g.fillRect(px, py, width, 1);
//畫中心
int color = 0x00FFFF00;
g.setColor(color);
g.fillRect(px + 1, py + 1, width - 1, width - 1);
//畫灰邊
g.setColor(0x00c0c0c0);
g.fillRect(px + width - 1, py + 1, 1, width - 1);
g.fillRect(px + 1, py + width - 1, width - 2, 1);
}
protected void paint(Graphics g) {
offScreenGraphics.setColor(0x000000);
offScreenGraphics.fillRect(0, 0, this.getWidth(), this.getHeight());
board.DrawBoard(offScreenGraphics, bx, by, false);
offScreenGraphics.setColor(0x00FF00FF);
offScreenGraphics.setColor(0x00ffff00);
//繪制提示信息:
offScreenGraphics.drawString("按#新游戲", bx + WIDTH + 10, by, Graphics.TOP
| Graphics.LEFT);
//繪制得分信息:
offScreenGraphics.drawString(text, bx + WIDTH + 10, by + 20,
Graphics.TOP | Graphics.LEFT);
int i, j;
for (i = 0; i < HEIGHT_NUMBER; i++)
for (j = 0; j < WIDTH_NUMBER; j++) {
if (grid[i][j] == 1) {
drawBrick(bx + j * GRIDWIDTH, by + i * GRIDWIDTH,
GRIDWIDTH, offScreenGraphics);
}
}
g.drawImage(offScreenImage, 0, 0, Graphics.TOP | Graphics.LEFT);
}
public void run() {
while (true) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println("sleep error : " + e.getMessage());
}
if (begin = true && pause == false) {
playGame();
}
}
}
protected synchronized void keyRepeated(int keyCode) {
if (pause == true)
return;
int action = getGameAction(keyCode);
if (action == Canvas.DOWN) {
clearcell(row, column, getboardcell);
if (Movable(row + 1, column, getboardcell)) {
row = row + 1;
copycell(row, column, getboardcell);
repaint();
} else {
copycell(row, column, getboardcell);
}
}
}
protected synchronized void keyPressed(int keyCode) {
//如果游戲暫停,則按鍵無效
if (pause == true)
return;
int action = getGameAction(keyCode);
switch (action) {
case Canvas.LEFT:
/* 左移 */
clearcell(row, column, getboardcell);
if ((Movable(row, column - 1, getboardcell)) && (ifhaddown == true)) {
column = column - 1;
copycell(row, column, getboardcell);
repaint();
} else {
copycell(row, column, getboardcell);
}
break;
case Canvas.RIGHT: /* 右移 */
clearcell(row, column, getboardcell);
if ((Movable(row, column + 1, getboardcell)) && (ifhaddown == true)) {
column = column + 1;
copycell(row, column, getboardcell);
repaint();
} else {
copycell(row, column, getboardcell);
}
break;
case Canvas.UP: /* 下墜塊變化 */
clearcell(row, column, getboardcell);
if ((Movable(row, column, bcell
.GetWorkcell((cellstyle + 1) % 4 + 1)))
&& (ifhaddown == true)) {
cellstyle = (cellstyle + 1) % 4;
getboardcell = bcell.GetWorkcell(cellstyle + 1);
copycell(row, column, getboardcell);
repaint();
} else {
copycell(row, column, getboardcell);
}
break;
case Canvas.DOWN:/* 下移 */
clearcell(row, column, getboardcell);
if (Movable(row + 1, column, getboardcell)) {
row = row + 1;
copycell(row, column, getboardcell);
repaint();
} else {
copycell(row, column, getboardcell);
}
break;
case Canvas.FIRE: /* 下墜塊變化 */
break;
}
if (keyCode == Canvas.KEY_POUND) {//開始新游戲
clearBoard();
repaint();
ifhaddown = false;
totalscore = 0;
begin = true;
}
}
protected void playGame() {
int i = 0;
int m, n;
//判斷是否從新出現新的方塊
if (ifhaddown == false) {
row = 0;
column = 4;
ifhaddown = true;
getboardcell = bcell.SetNewCells();
cellstyle = bcell.GetNewCellCount();
copycell(row, column, getboardcell);
repaint();
return;
}
clearcell(row, column, getboardcell);
boolean ifmovable = Movable(row + 1, column, getboardcell);
if (ifmovable == true) //如果能繼續下落
{
row++;
copycell(row, column, getboardcell);
repaint();
} else {
copycell(row, column, getboardcell);
ifhaddown = false;
//如果可以消行
if (ClearLine()) {
repaint();
}
}
if ((GameOver() == true) && (ifhaddown == false)) {
begin = false;
//note.setText("游戲 結束! 按S重新開始游戲");
//timer.stop();
return;
}
}
public void commandAction(Command c, Displayable d) {
if(c==goHome){
midlet.intoHomeCanvas();
}
if (c ==startCommand) {
addCommand(pauseCommand);
removeCommand(startCommand);
start();
}
if (c == pauseCommand) {
removeCommand(pauseCommand);
addCommand(startCommand);
pause();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -