📄 tetrisblock.java
字号:
// next = Math.abs(rand.nextInt()) % 7 + 1;
/* 得到当前下坠物 */
switch (pattern) {
case 1:
readPattern(blockpattern1);
break;
case 2:
readPattern(blockpattern2);
break;
case 3:
readPattern(blockpattern3);
break;
case 4:
readPattern(blockpattern4);
break;
case 5:
readPattern(blockpattern5);
break;
case 6:
readPattern(blockpattern6);
break;
case 7:
readPattern(blockpattern7);
break;
}
/* 得到下一个下坠物 */
switch (next) {
case 1:
readNextPattern(blockpattern1);
break;
case 2:
readNextPattern(blockpattern2);
break;
case 3:
readNextPattern(blockpattern3);
break;
case 4:
readNextPattern(blockpattern4);
break;
case 5:
readNextPattern(blockpattern5);
break;
case 6:
readNextPattern(blockpattern6);
break;
case 7:
readNextPattern(blockpattern7);
break;
}
x = 5; /* 游戏容器内径的一半 */
y = 0; /* y坐标 */
rot = 0;
//判断map数据,决定y的真正值,之所以这么处理,是因为当game over的时候,最后一个下坠物,可能只能画出一部分
//为了达到这个效果,必须让y成为一个恰当的负值
while (isCrashAtBegin()) {
y--;
if(y<-4){
break;
}
}
oldx = x;
oldy = y;
oldrot = rot;
}
/**
* 设置当前下坠物变量的内容
* @param nowblock int[][][] 7种下坠物常量之一
*/
private void readPattern(int[][][] nowblock) {
blockpattern = new int[4][4][4];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 4; k++) {
blockpattern[i][j][k] = nowblock[i][j][k];
}
}
}
}
/**
* 设置下一个下坠物变量的内容。只需要包存4中旋转变化中的第一种即可,所以roto为值=0
* @param nowblock int[][][] 7种下坠物常量之一
* 很容易理解
*/
private void readNextPattern(int[][][] nowblock) {
blockNextpattern = new int[4][4];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
blockNextpattern[i][j] = nowblock[0][i][j];
}
}
}
/* 旋转下坠物 */
protected void rotBlock() {
rot++;
if (rot == 4) {
rot = 0;
}
}
/**
* 绘制下坠物,包括清除下坠物的旧图像,调用绘制下坠物新图像的函数
* 本地方法
* @param g Graphics
*/
public void paint(Graphics g) {
//如果3维都没有变化,则无需重画
if ( (oldrot != rot) || (oldx != x) || (oldy != y)) {
//清除旧图形
g.setColor(TetrisCanvas.BACKGROUND);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (blockpattern[oldrot][i][j] == 1) {
g.fillRect(gamearea_x +
(oldx + j) * brick_Width,
gamearea_y +
(oldy + i) * brick_Width,
brick_Width, brick_Width);
}
}
}
drawBlock(g);
oldrot = rot;
oldx = x;
oldy = y;
}
}
/**
* 绘制下坠物
* @param g Graphics
* 本地,远端均可使用
*/
public void drawBlock(Graphics g) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (blockpattern[rot][i][j] == 1) {
drawBrick(gamearea_x +
(x + j) * brick_Width,
gamearea_y +
(y + i) * brick_Width, g, pattern - 1);
}
}
}
}
/*
* 远端用户使用,清除当前下坠的方块
*
*/
public void eraseBlock(Graphics g)
{
//清除旧图形
g.setColor(TetrisCanvas.BACKGROUND);
int px = gamearea_x + 5 * brick_Width;
int py = gamearea_y ;
int width = brick_Width * 4;
g.fillRect(px, py, width, width);
//绘制重新接收的当前方块
drawBlock(g);
}
/**
* 判断下坠物是不是和map中已有的砖块重叠,为了处理gameover的时候,只需画出部分下坠物的情况
@return true:有重叠,false:无重叠
*/
public boolean isCrashAtBegin() {
//行
for (int i = 3; i >= 0; i--) {
//列
for (int j = 0; j < 4; j++) {
int mx = x + j;
int my = y + i;
if (my < 0) {
my = 0;
}
if (blockpattern[rot][i][j] == 1 && map.get(mx, my) != 8 &&
map.get(mx, my) != 0) {
return true;
}
}
}
return false;
}
/**
* 画小砖块
* @param px x坐标
* @param py y坐标
* @param g Graphics
* @param colorIndex 颜色索引值
*/
public void drawBrick(int px, int py, Graphics g, int colorIndex) {
//画白边
g.setColor(255, 255, 255);
g.fillRect(px, py, 1, brick_Width);
g.fillRect(px, py, brick_Width, 1);
//画中心
int color = BRICK_COLORS[colorIndex];
g.setColor(color);
g.fillRect(px + 1, py + 1, brick_Width - 1,
brick_Width - 1);
//画灰边
g.setColor(0x00c0c0c0);
g.fillRect(px + brick_Width - 1, py + 1, 1,
brick_Width - 1);
g.fillRect(px + 1, py + brick_Width - 1,
brick_Width - 2, 1);
}
/**
* 在游戏容器的右边绘出下一个下坠物
* @param g Graphics
*/
public void drawNextBlock(Graphics g) {
//清除绘制区域
g.setColor(TetrisCanvas.BACKGROUND);
int px = gamearea_x + 12 * brick_Width;
int py = gamearea_y + 2 * brick_Width;
int width = brick_Width * 4;
g.fillRect(px, py, width, width);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (blockNextpattern[i][j] == 1) {
drawBrick(px + j * brick_Width,
py + i * brick_Width, g, next - 1);
}
}
}
}
/**
* 判断下坠物是否能下移
* @param kyouseiflag boolean true:自动下移 false:人工按键下移
* @return boolean
*/
public boolean checkDown() {
boolean check = true;
/* 分别扫描下坠物的4行,从最下面的那行开始 */
for (int i = 0; i < 4; i++) {
int row = 3;
while (row >= 0) {
if (blockpattern[rot][row][i] == 1) {
if (map.get(x + i, y + row + 1) != 0) {
check = false;
}
row = -1; /* 终止循环 */
}
else {
row--;
}
}
}
return check;
}
/* 下坠物下移1行 */
public void down() {
y = y + 1;
}
/* 判断是否能旋转 */
public boolean checkRot() {
boolean check = true;
int tmpRot = rot + 1;
if (tmpRot == 4) {
tmpRot = 0;
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (blockpattern[tmpRot][i][j] == 1) {
if (map.get(x + j, y + i) != 0) {
check = false;
}
}
}
}
return check;
}
/* 判断下坠物是否可以移动 */
public boolean checkMove(int direct) {
boolean check = true;
/* 分别扫描下坠物的4行 */
for (int i = 0; i < 4; i++) {
if (direct == 1) { /* 左移 */
int row = 0;
while (row <= 3) {
if (blockpattern[rot][i][row] == 1) {
if (map.get(x + row - 1, y + i) != 0) {
check = false;
}
row = 4; /* 终止循环 */
}
else {
row++;
}
}
}
else { /* 右移 */
int row = 3;
while (row >= 0) {
if (blockpattern[rot][i][row] == 1) {
if (map.get(x + row + 1, y + i) != 0) {
check = false;
}
row = -1; /* 终止循环 */
}
else {
row--;
}
}
}
}
return check;
}
/* 左右移动 */
public void move(int direct) {
if (direct == 1) {
x = x - 1;
}
else {
x = x + 1;
}
}
public int getY() {
return y;
}
/* 根据下坠物的当前位置设置地图数据 */
public void fixBlock() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (blockpattern[rot][i][j] == 1) {
map.set(x + j, y + i, pattern);
}
}
}
}
public int getPattern()
{
return pattern;
}
public int getNext()
{
return next;
}
public void generatePN()
{
pattern = next;
next = Math.abs(rand.nextInt()) % 7 + 1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -