📄 tetrisgamecanvas.java
字号:
package TetirisGame;
import javax.microedition.lcdui.*;
class TetrisGameCanvas extends Canvas implements Runnable {
Thread t;
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() {
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;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -