分享
 
 
 

俄罗斯方块源码(3)

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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;

}

*/

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有