?? gamecanvas.java
字號:
if( pieces[centerPoint -1] == null &&
pieces[centerPoint +ROW_LENGTH -1] == null)
{
//在左下與左方沒有任何東西時向左移動
pieces[centerPoint -1] = pieces[centerPoint +ROW_LENGTH];
pieces[centerPoint +ROW_LENGTH] = null;
//將方向變為向左
currentDirection = DIRECTION_LEFT;
}
}
break;
case DIRECTION_LEFT: //方向為向左時
//因為要作順時鐘旋轉,所以要檢查上方
if(centerPoint > ROW_LENGTH -1) {
//當不在最上面的時候
if( pieces[centerPoint -ROW_LENGTH] == null &&
pieces[centerPoint -ROW_LENGTH -1] == null)
{
//在左上與上方都沒有東西時向上移動
pieces[centerPoint -ROW_LENGTH] = pieces[centerPoint -1];
pieces[centerPoint -1] = null;
//將方向設為向上
currentDirection = DIRECTION_UP;
}
}
break;
}
}
/** 按下左按鍵后被調用出來的方法 */
private void doLeft() {
switch(currentDirection) {
case DIRECTION_UP: //方向向上時
if(centerPoint % ROW_LENGTH != 0) {
//不是在最左邊的時候
//檢查左方與左上
if( pieces[centerPoint -1] == null &&
pieces[centerPoint -ROW_LENGTH -1] == null)
{
//當左與左上都是空的時候
//向左移動一格
pieces[centerPoint -1] = pieces[centerPoint];
pieces[centerPoint -ROW_LENGTH -1] = pieces[centerPoint -ROW_LENGTH];
pieces[centerPoint] = null;
pieces[centerPoint -ROW_LENGTH] = null;
//移動中心位置
centerPoint--;
}
}
break;
case DIRECTION_RIGHT: //方向向右時
//檢查左方
if(centerPoint % ROW_LENGTH != 0) {
//不是在最左邊的時候
if( pieces[centerPoint -1] == null ) {
//當左與左上都是空的時候
//向左移動一格
pieces[centerPoint -1] = pieces[centerPoint];
pieces[centerPoint] = pieces[centerPoint +1];
pieces[centerPoint +1] = null;
//移動中心位置
centerPoint--;
}
}
break;
case DIRECTION_DOWN: //方向向下時
//檢查左方與左下
if(centerPoint % ROW_LENGTH != 0) {
//不是在最左邊的時候
if( pieces[centerPoint -1] == null &&
pieces[centerPoint +ROW_LENGTH -1] == null ) {
//當左與左下都是空的時候
//向左移動一格
pieces[centerPoint -1] = pieces[centerPoint];
pieces[centerPoint +ROW_LENGTH -1] = pieces[centerPoint +ROW_LENGTH];
pieces[centerPoint] = null;
pieces[centerPoint +ROW_LENGTH] = null;
//移動中心位置
centerPoint--;
}
}
break;
case DIRECTION_LEFT: //方向向下時
//檢查左方
if((centerPoint -1)% ROW_LENGTH != 0) {
//不是在最左邊的時候
if( pieces[centerPoint -1 -1] == null ) {
//當附在左方的方塊不在最左邊、且左方為空的時候
//向左移動一格
pieces[centerPoint -1 -1] = pieces[centerPoint -1];
pieces[centerPoint -1] = pieces[centerPoint];
pieces[centerPoint] = null;
//移動中心位置
centerPoint--;
}
}
break;
}
}
/** 按下右按鍵時被調用出來的方法 */
private void doRight() {
switch(currentDirection) {
case DIRECTION_UP: //方向向上時
if(centerPoint % ROW_LENGTH != ROW_LENGTH-1) {
//不是在最右邊的時候
//檢查右與右上
if( pieces[centerPoint +1] == null &&
pieces[centerPoint -ROW_LENGTH +1] == null ) {
//當右與右上為空白的時候
//向右移動一格
pieces[centerPoint +1] = pieces[centerPoint];
pieces[centerPoint -ROW_LENGTH +1] = pieces[centerPoint -ROW_LENGTH];
pieces[centerPoint] = null;
pieces[centerPoint -ROW_LENGTH] = null;
//移動中心位置
centerPoint++;
}
}
break;
case DIRECTION_RIGHT: //方向向右時
if((centerPoint +1) % ROW_LENGTH != ROW_LENGTH-1) {
//不是在最右邊的時候
//檢查右邊
if( pieces[centerPoint +1 +1] == null ) {
//當附在右方的方塊不在最右邊、且右方為空白的時候
//向右移動一格
pieces[centerPoint +1 +1] = pieces[centerPoint +1];
pieces[centerPoint +1] = pieces[centerPoint];
pieces[centerPoint] = null;
//移動中心位置
centerPoint++;
}
}
break;
case DIRECTION_DOWN: //方向向下時
if(centerPoint % ROW_LENGTH != ROW_LENGTH-1) {
//不是在最右邊的時候
//檢查右與右下
if( pieces[centerPoint +1] == null &&
pieces[centerPoint +ROW_LENGTH +1] == null ) {
//當右與右下為空白的時候
//向右移動一格
pieces[centerPoint +1] = pieces[centerPoint];
pieces[centerPoint +ROW_LENGTH +1] = pieces[centerPoint +ROW_LENGTH];
pieces[centerPoint] = null;
pieces[centerPoint +ROW_LENGTH] = null;
//移動中心位置
centerPoint++;
}
}
break;
case DIRECTION_LEFT: //方向向左時
if(centerPoint % ROW_LENGTH != ROW_LENGTH-1) {
//不是在最右邊的時候
//檢查右邊
if( pieces[centerPoint +1] == null ) {
//當右方為空白的時候
//向右移動一格
pieces[centerPoint +1] = pieces[centerPoint];
pieces[centerPoint] = pieces[centerPoint -1];
pieces[centerPoint -1] = null;
//移動中心位置
centerPoint++;
}
}
break;
}
}
/**
* 按下下按鍵或是向下落下時被調用的方法
* @return boolean 當可以向下落下時就會傳回true、不能落下時就會傳回false
*/
private boolean doFall() {
switch(currentDirection) {
case DIRECTION_UP: //方向為向上時
//檢查正下方
if(centerPoint < (COLUMN_LENGTH -1) * ROW_LENGTH) {
//當不是最下方的時候
if( pieces[centerPoint +ROW_LENGTH] == null ) {
//當正下方為空白時
//就往下一格
pieces[centerPoint +ROW_LENGTH] = pieces[centerPoint];
pieces[centerPoint] = pieces[centerPoint -ROW_LENGTH];
pieces[centerPoint -ROW_LENGTH] = null;
//移動中心位置
centerPoint = centerPoint + ROW_LENGTH;
return true;
}
}
break;
case DIRECTION_RIGHT: //方向為向右時
//檢查正下方與右下
if(centerPoint < (COLUMN_LENGTH -1) * ROW_LENGTH) {
//當不是最下方的時候
if( pieces[centerPoint +ROW_LENGTH] == null &&
pieces[centerPoint +1 +ROW_LENGTH] == null ) {
//當正下方與右下為空白時
//就往下一格
pieces[centerPoint +ROW_LENGTH] = pieces[centerPoint];
pieces[centerPoint +1 +ROW_LENGTH] = pieces[centerPoint +1];
pieces[centerPoint] = null;
pieces[centerPoint +1] = null;
//移動中心位置
centerPoint = centerPoint +ROW_LENGTH;
return true;
}
}
break;
case DIRECTION_DOWN: //方向為向下時
//檢查正下方
if(centerPoint +ROW_LENGTH < (COLUMN_LENGTH -1) * ROW_LENGTH) {
//當附屬的方塊不是最下方的時候
if( pieces[centerPoint +ROW_LENGTH +ROW_LENGTH] == null) {
//當附屬在下面的方塊不是在最下面,而正下方為空白時 //往下移動一格
pieces[centerPoint +ROW_LENGTH +ROW_LENGTH] = pieces[centerPoint +ROW_LENGTH];
pieces[centerPoint +ROW_LENGTH] = pieces[centerPoint];
pieces[centerPoint] = null;
//移動中心位置
centerPoint = centerPoint + ROW_LENGTH;
return true;
}
}
break;
case DIRECTION_LEFT: //當方向為向左時
//調查正下方與左下
if(centerPoint < (COLUMN_LENGTH -1) * ROW_LENGTH) {
if( pieces[centerPoint +ROW_LENGTH] == null &&
pieces[centerPoint -1 +ROW_LENGTH] == null ) {
//正下方與左下為空白時
//往下移動一格
pieces[centerPoint +ROW_LENGTH] = pieces[centerPoint];
pieces[centerPoint -1 +ROW_LENGTH] = pieces[centerPoint -1];
pieces[centerPoint] = null;
pieces[centerPoint -1] = null;
//移動中心位置
centerPoint = centerPoint +ROW_LENGTH;
return true;
}
}
break;
}
return false;
}
/** 啟動描繪線程的方法 */
private void doThreadStart() {
//啟動線程
new Thread(this).start();
}
/** 線程的運作部分 */
public void run() {
try {
switch(gameState) {
case GAME_START://游戲開始時
//先將標題的指令刪除
removeCommand(startCmd);
removeCommand(exitCmd);
//顯示游戲開始畫面
repaint();
Thread.sleep(1500);
//新增指令
addCommand(quitCmd);
if(! isPaused) {
addCommand(pauseCmd);
}
//將狀態設為游戲中,然后就此轉移到游戲中的處理
gameState = GAME_PLAYING;
case GAME_PLAYING://游戲中
repaint();
//游戲結束標志
boolean isGameOver = false;
//開始游戲循環
while(! isStopped) {//在Stop前持續循環
if(isNextFall) {
//落下下一個時
if( pieces[FALL_START_INDEX1] != null ||
pieces[FALL_START_INDEX2] != null)
{
//無法落下下一個時就算游戲結束
isGameOver = true;
//將此循環結束
doGameStop();
pieces[FALL_START_INDEX1] = nextPieces[0];
pieces[FALL_START_INDEX2] = nextPieces[1];
break;
}else {
//讓下一個落下時
pieces[FALL_START_INDEX1] = nextPieces[0];
pieces[FALL_START_INDEX2] = nextPieces[1];
currentDirection = DIRECTION_DEFAULT;
centerPoint = FALL_START_INDEX1;
isNextFall = false;
//準備下一個方塊組
nextPieces = getNextPieces();
//加速落下速度
fallCount++;
if(fallCount > 4) {
fallCount = 0;
if(tickCount4Speed > 2) {
tickCount4Speed--;
}
}
isKeyEnable = true;
}
}else {
//已經落下時
tickCount4Fall++;
if(tickCount4Fall == tickCount4Speed) {
//為落下時機時
tickCount4Fall = 0;
if(! doFall()) {
//不讓其落下后
repaint();
//使按鍵無效
isKeyEnable = false;
if(doDropDown()) {
//不能若下全體方塊組、而能夠落下方塊時
//暫停后進行下一個處理
repaint();
Thread.sleep(500);
}
//連鎖回數
int chainCount = 1;
while(true) {
//檢查是否湊成同色
//取得傳回放有湊成同色之Index之Vector的Vector
Vector matchVect = doMatchCheck();
int matchVectSize = matchVect.size();
if(matchVectSize > 0) {
//當有湊成同色的地方時
repaint();
Thread.sleep(200);
boolean isDropDown = false;
//檢查湊成同色的地方
for(int i=0; i < matchVectSize; i++) {
Vector inxVect = (Vector)matchVect.elementAt(i);
int matchInxLeng = inxVect.size();
if(matchInxLeng >= 4) {
//當湊成4個以上
//加上分數
int tmpScore = DEFAULT_SCORE_VALUE;
tmpScore = tmpScore + ((matchInxLeng-4) * DEFAULT_SCORE_VALUE);
tmpScore = tmpScore * chainCount;
score = score + tmpScore;
isDropDown = true;
for(int j=0; j < matchInxLeng; j++) {
//清除各方塊
Integer inxInteg = (Integer)inxVect.elementAt(j);
pieces[inxInteg.intValue()] = null;
}
}
}
repaint();
Thread.sleep(200);
if(isDropDown) {
//要刪除時就在暫停后進行下一個處理
doDropDown();
repaint();
Thread.sleep(500);
}else {
//都沒有結合的部分就撥除while
break;
}
}else {
//當湊成同色的部分不見時就撥除while
break;
}
chainCount++;
}
//落下下一個方塊組
isNextFall = true;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -