package Tetris;
import javax.microedition.lcdui.Graphics;
import java.util.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class Tetris {
private static final int [][] SHAPE_DATA = {
{0x038, 0x092, 0x038, 0x092},
{0x093, 0x03c, 0x192, 0x078},
{0x096, 0x138, 0x0d2, 0x039},
{0x1b0, 0x1b0, 0x1b0, 0x1b0},
{0x198, 0x05a, 0x198, 0x05a},
{0x0f0, 0x099, 0x0f0, 0x099},
{0x03a, 0x0b2, 0x0b8, 0x09a}
};
public static final int COLOR_DATA [] = {
0x0000ff,// blue
0xff7f50,// coral
0x006400,// darkgreen
0x8b008b,// darkmagenta
0xdc143c,// crimson
0x008b8b,// darkcyan
0xffd700 // gold
};
public static final int COLOR_GRAY = 0xd3d3d3;// light gray
public static final int COLOR_BLACK = 0x000000;// black
public static final int COLOR_BLUE = 0x1E90FF;// blue
public static final int COLOR_WHITE = 0xffffff;
public static final int COLOR_CORAL = 0xff7f50;
public int bx;
public int by;
public int shape;
public int direction;
private int [] cellColor = new int [4];
private int [][] shapeData = new int [3][3];
public Tetris() {
}
public Tetris(Tetris tetrisTemp) {
this.shape = tetrisTemp.shape;
this.direction = tetrisTemp.direction;
this.bx = tetrisTemp.bx;
this.by = tetrisTemp.by;
for (int i = 0; i < 4; i++)
this.cellColor[i] = tetrisTemp.cellColor[i];
this.initShapeData();
}
/*
public boolean moveUp() {
if (by <= 0) {
for (int col = 0; col < 3; col ++)
if (shapeData[-by][col] == 1)
return false;
}
by -= 2;
return true;
}
public boolean moveDown() {
if (by >= (TetrisGameCanvas.ROW - 3)) {
for (int col = 0; col < 3; col ++)
if (shapeData[TetrisGameCanvas.ROW - by - 1][col] == 1)
return false;
}
++ by;
return true;
}
public boolean moveLeft() {
if (bx <= 0) {
for (int row = 0; row < 3; row ++)
if (shapeData[row][-bx] == 1)
return false;
}
-- bx;
return true;
}
public boolean moveRight() {
if (bx >= (TetrisGameCanvas.COL - 3)) {
for (int row = 0; row < 3; row ++)
if (shapeData[row][TetrisGameCanvas.COL - bx - 1] == 1)
return false;
}
++ bx;
return true;
}
*/
public void moveUp() {
-- by;
}
public void moveDown() {
++ by;
}
public void moveLeft() {
-- bx;
}
public void moveRight() {
++ bx;
}
public void rotate() {
direction = (direction + 1) % 4;
initShapeData();
}
/**
* 绘制方块
* @param graphics
*/
public void draw(Graphics graphics) {
int iCellColor = 0;
for (int row = 0; row < 3; row++)
for (int col = 0; col < 3; col++) {
if (shapeData[row][col] == 1)
drawCell(graphics,
cellColor[iCellColor++],
TetrisGameCanvas.BASE_X + (bx + col) * TetrisGameCanvas.CELL_WIDTH,
TetrisGameCanvas.BASE_Y + (by + row) * TetrisGameCanvas.CELL_WIDTH,
false,
true);
}
// 一字形状方块特殊处理
if (shape == 0) {
if (direction == 1 || direction == 3) {// 竖
drawCell(graphics,
cellColor[iCellColor++],
TetrisGameCanvas.BASE_X + (bx + 1) * TetrisGameCanvas.CELL_WIDTH,
TetrisGameCanvas.BASE_Y + (by + 3) * TetrisGameCanvas.CELL_WIDTH,
false,
true);
} else {// 横
drawCell(graphics,
cellColor[iCellColor++],
TetrisGameCanvas.BASE_X + (bx + 3) * TetrisGameCanvas.CELL_WIDTH,
TetrisGameCanvas.BASE_Y + (by + 1) * TetrisGameCanvas.CELL_WIDTH,
false,
true);
}
}
}
/**
* 绘制方块
* @param graphics
*/
public void draw(Graphics graphics, int baseX, int baseY) {
int iCellColor = 0;
for (int row = 0; row < 3; row++)
for (int col = 0; col < 3; col++) {
if (shapeData[row][col] == 1)
drawCell(graphics,
cellColor[iCellColor++],
baseX + col * TetrisGameCanvas.CELL_WIDTH,
baseY + row * TetrisGameCanvas.CELL_WIDTH,
false,
true);
}
// 一字形状方块特殊处理
if (shape == 0) {
if (direction == 1 || direction == 3) { // 竖
drawCell(graphics,
cellColor[iCellColor++],
baseX + TetrisGameCanvas.CELL_WIDTH,
baseY + 3 * TetrisGameCanvas.CELL_WIDTH,
false,
true);
}
else { // 横
drawCell(graphics,
cellColor[iCellColor++],
baseX + 3 * TetrisGameCanvas.CELL_WIDTH,
baseY + TetrisGameCanvas.CELL_WIDTH,
false,
true);
}
}
}
/**
* 绘制每个格子
* @param graphics
* @param color
* @param x
* @param y
*/
public static void drawCell(Graphics graphics, int[] color,
int x, int y, boolean erase, boolean fix) {
if (erase) {
graphics.setColor(COLOR_BLACK);
graphics.fillRect(x, y,
TetrisGameCanvas.CELL_WIDTH,
TetrisGameCanvas.CELL_WIDTH);
}
else {
graphics.setColor(color[0], color[1], color[2]);
graphics.fillRect(x + 1, y + 1,
TetrisGameCanvas.CELL_WIDTH - 1,
TetrisGameCanvas.CELL_WIDTH - 1);
graphics.setColor(COLOR_GRAY);
graphics.drawLine(x, y, x + TetrisGameCanvas.CELL_WIDTH, y);
graphics.drawLine(x, y, x, y + TetrisGameCanvas.CELL_WIDTH);
graphics.setColor(COLOR_BLACK);
// graphics.setColor(COLOR_GRAY);
graphics.drawLine(x, y + TetrisGameCanvas.CELL_WIDTH,
x + TetrisGameCanvas.CELL_WIDTH,
y + TetrisGameCanvas.CELL_WIDTH);
graphics.drawLine(x + TetrisGameCanvas.CELL_WIDTH, y,
x + TetrisGameCanvas.CELL_WIDTH,
y + TetrisGameCanvas.CELL_WIDTH);
}
if (fix == false) {
graphics.setColor(COLOR_WHITE);
graphics.drawLine(x - 1, y - 1, x + TetrisGameCanvas.CELL_WIDTH + 1,
y + 1);
graphics.drawLine(x - 1, y - 1, x + 1,
y + TetrisGameCanvas.CELL_WIDTH + 1);
graphics.drawLine(x - 1, y + TetrisGameCanvas.CELL_WIDTH - 1,
x + TetrisGameCanvas.CELL_WIDTH + 1,
y + TetrisGameCanvas.CELL_WIDTH + 1);
graphics.drawLine(x + TetrisGameCanvas.CELL_WIDTH - 1, y - 1,
x + TetrisGameCanvas.CELL_WIDTH + 1,
y + TetrisGameCanvas.CELL_WIDTH + 1);
}
}
public static void drawCell(Graphics graphics, int color, int x, int y,
boolean erase, boolean fix) {
if (erase) {
graphics.setColor(COLOR_BLACK);
graphics.fillRect(x, y,
TetrisGameCanvas.CELL_WIDTH,
TetrisGameCanvas.CELL_WIDTH);
} else {
graphics.setColor(color);
graphics.fillRect(x + 1, y + 1,
TetrisGameCanvas.CELL_WIDTH - 1,
TetrisGameCanvas.CELL_WIDTH - 1);
graphics.setColor(COLOR_GRAY);
graphics.drawLine(x, y, x + TetrisGameCanvas.CELL_WIDTH, y);
graphics.drawLine(x, y, x, y + TetrisGameCanvas.CELL_WIDTH);
// graphics.setColor(COLOR_BLACK);
// graphics.setColor(COLOR_GRAY);
graphics.drawLine(x, y + TetrisGameCanvas.CELL_WIDTH,
x + TetrisGameCanvas.CELL_WIDTH, y + TetrisGameCanvas.CELL_WIDTH);
graphics.drawLine(x + TetrisGameCanvas.CELL_WIDTH, y,
x + TetrisGameCanvas.CELL_WIDTH, y + TetrisGameCanvas.CELL_WIDTH);
if (fix == false) {
graphics.setColor(COLOR_WHITE);
graphics.drawLine(x - 1, y - 1, x + TetrisGameCanvas.CELL_WIDTH, y - 1);
graphics.drawLine(x - 1, y - 1, x - 1, y + TetrisGameCanvas.CELL_WIDTH);
graphics.drawLine(x - 1, y + TetrisGameCanvas.CELL_WIDTH - 1,
x + TetrisGameCanvas.CELL_WIDTH, y + TetrisGameCanvas.CELL_WIDTH - 1);
graphics.drawLine(x + TetrisGameCanvas.CELL_WIDTH - 1, y - 1,
x + TetrisGameCanvas.CELL_WIDTH - 1, y + TetrisGameCanvas.CELL_WIDTH);
}
}
}
private void initShapeData() {
int tempShapeData = SHAPE_DATA[shape][direction];
shapeData[0][0] = (tempShapeData & 0x100) >> 8;
shapeData[0][1] = (tempShapeData & 0x80) >> 7;
shapeData[0][2] = (tempShapeData & 0x40) >> 6;
shapeData[1][0] = (tempShapeData & 0x20) >> 5;
shapeData[1][1] = (tempShapeData & 0x10) >> 4;
shapeData[1][2] = (tempShapeData & 0x8) >> 3;
shapeData[2][0] = (tempShapeData & 0x4) >> 2;
shapeData[2][1] = (tempShapeData & 0x2) >> 1;
shapeData[2][2] = (tempShapeData & 0x1) >> 0;
}
public static Random random = new Random(new Date().getTime());
/**
* 生成随机的不小于0的整数
* @return 返回生成的随机数
*/
public static int getRandom() {
int iReturn = random.nextInt() >>> 1;// 随机生成不小于0的整数
return iReturn;
}
/**
* 生成新形状的相应数据
*/
public void newShape() {
// 随机形状
shape = getRandom() % 7;
// 形状随机方向
direction = getRandom() % 4;
// 形状中每个格子的颜色随机生成
for (int i = 0; i < 4; i++) {
cellColor[i] = COLOR_DATA[getRandom() % 6];
}
initShapeData();
setInitXY();
}
public void setShape(Tetris tetrisTemp) {
this.shape = tetrisTemp.shape;
this.direction = tetrisTemp.direction;
this.bx = tetrisTemp.bx;
this.by = tetrisTemp.by;
for (int i = 0; i < 4; i++)
this.cellColor[i] = tetrisTemp.cellColor[i];
this.initShapeData();
}
/*
public int setX(int tempX) {
bx = tempX;
return bx;
}
public int getX() {
return bx;
}
public int setY(int tempY) {
by = tempY;
return by;
}
public int getY() {
return by;
}
*/
public boolean getShapeData(int row, int col) {
if (shapeData[row][col] == 1)
return true;
else
return false;
}
public int getCellColor(int index) {
return cellColor[index];
}
private void setInitXY() {
bx = TetrisGameCanvas.COL/2 - 1;
by = 0;
// for (int row = 0; row < 3; row ++)
// for (int col = 0; col < 3; col ++) {
// if (shapeData[row][col] == 1) {
// by = -row;
// break;
// }
// }
}
/*
public int getShape() {
return shape;
}
public int getDirection() {
return direction;
}
*/
}
本文地址:http://com.8s8s.com/it/it15457.htm