?? tetrisblock.java
字號:
// next = Math.abs(rand.nextInt()) % 7 + 1;
/* 得到當(dāng)前下墜物 */
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; /* 游戲容器內(nèi)徑的一半 */
y = 0; /* y坐標(biāo) */
rot = 0;
//判斷map數(shù)據(jù),決定y的真正值,之所以這么處理,是因為當(dāng)game over的時候,最后一個下墜物,可能只能畫出一部分
//為了達到這個效果,必須讓y成為一個恰當(dāng)?shù)呢撝?
while (isCrashAtBegin()) {
y--;
if(y<-4){
break;
}
}
oldx = x;
oldy = y;
oldrot = rot;
}
/**
* 設(shè)置當(dāng)前下墜物變量的內(nèi)容
* @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];
}
}
}
}
/**
* 設(shè)置下一個下墜物變量的內(nèi)容。只需要包存4中旋轉(zhuǎn)變化中的第一種即可,所以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];
}
}
}
/* 旋轉(zhuǎn)下墜物 */
protected void rotBlock() {
rot++;
if (rot == 4) {
rot = 0;
}
}
/**
* 繪制下墜物,包括清除下墜物的舊圖像,調(diào)用繪制下墜物新圖像的函數(shù)
* 本地方法
* @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);
}
}
}
}
/*
* 遠端用戶使用,清除當(dāng)前下墜的方塊
*
*/
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);
//繪制重新接收的當(dāng)前方塊
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坐標(biāo)
* @param py y坐標(biāo)
* @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) {
//清除繪制區(qū)域
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; /* 終止循環(huán) */
}
else {
row--;
}
}
}
return check;
}
/* 下墜物下移1行 */
public void down() {
y = y + 1;
}
/* 判斷是否能旋轉(zhuǎn) */
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; /* 終止循環(huán) */
}
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; /* 終止循環(huán) */
}
else {
row--;
}
}
}
}
return check;
}
/* 左右移動 */
public void move(int direct) {
if (direct == 1) {
x = x - 1;
}
else {
x = x + 1;
}
}
public int getY() {
return y;
}
/* 根據(jù)下墜物的當(dāng)前位置設(shè)置地圖數(shù)據(jù) */
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;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -