?? game.java
字號:
/*
* $Id: Game.java, 2007-10-11, 下午01:56:36, Eric. Exp$
*
* Copyright (c) 2006-2007 Wipro Technologies. All Rights Reserved.
* This software program and documentation are copyrighted by Wipro
* Technologies.
*/
package eric.block;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Display;
/**
* Class/Interface description
*
* @author Eric
* @see Another Class
* @since 0.1
*/
public class Game
{
protected Display display = null;
protected Main main = null;
protected GC gameGC;
private GC nextGC;
protected GameBoard gameBoard = null;
protected NextBoard nextBoard = null;
Block currentBlock = null;
Block nextBlock = null;
boolean playing = false;
private int delay;
private boolean paused;
private int score;
private boolean tryAgain;
private int level;
private int fullRowOneTime;
private int totalLines;
private int currentBlockType;
private int nextBlockType;
private GameThread gameThread;
/**
* @param x_columns
* @param y_rows
* @param display
* @param main
*/
public Game(int x_columns, int y_rows, Display display, Main main)
{
this.display = display;
this.main = main;
this.gameGC = new GC(main.getCurrentBoardCanvas());
this.nextGC = new GC(main.getNextBoardCanvas());
MyGC.setMyGC(gameGC);
gameBoard = new GameBoard(x_columns, y_rows, display);
nextBoard = new NextBoard(display);
}
/**
* @return boolean
*/
public boolean isPlaying()
{
return playing;
}
/**
* @return boolean
*/
public boolean isGameOver()
{
for (int x = 0; x < 10; x++)
{
if (gameBoard.getPositionValue(x, 0) != gameBoard.EMPTY)
{
return true;
}
}
return false;
}
/**
* Start game
*/
public void startGame()
{
delay = 1000;
score = 0;
level = 1;
main.display.syncExec(new Runnable()
{
public void run()
{
main.setScoreText(score);
main.setLevelText(level);
}
});
totalLines = 0;
playing = true;
tryAgain = false;
gameBoard.init();
paused = false;
currentBlock = Block.getRandomBlock(120, 0, display);
nextBlock = Block.getRandomBlock(120, 0, display);
currentBlockType = currentBlock.getBlockType();
nextBoard.setCurrentBlock(currentBlockType);
nextBlockType = nextBlock.getBlockType();
nextBoard.setNextBlock(nextBlockType);
MyGC.setMyGC(nextGC);
nextBoard.draw();
MyGC.setMyGC(gameGC);
gameThread = new GameThread();
gameThread.start();
}
/**
* Game over's method
*/
public void gameOver()
{
quit();
main.display.syncExec(new Runnable()
{
public void run()
{
main.setBtStartPauseText("Try Again");
}
});
gameBoard.drawMessage("Game Over");
playing = false;
tryAgain = true;
}
/**
* @param e
*/
public void controlGame(KeyEvent e)
{
int y;
switch (e.keyCode)
{
// Pause game
case 'p':
if (playing)
{
pauseGame();
main.display.syncExec(new Runnable()
{
public void run()
{
main.setBtStartPauseText("Resume");
}
});
}
break;
// Resume game
case 'r':
if (playing)
{
resumeGame();
main.display.syncExec(new Runnable()
{
public void run()
{
main.setBtStartPauseText("Pause");
main.getCurrentBoardCanvas().redraw();
}
});
}
break;
// Move left
case SWT.ARROW_LEFT:
if (playing && !paused)
{
if (gameBoard.canMoveLeft(currentBlock))
{
currentBlock.moveLeft();
}
}
break;
// Move right
case SWT.ARROW_RIGHT:
if (playing && !paused)
{
if (gameBoard.canMoveRight(currentBlock))
{
currentBlock.moveRight();
}
}
break;
// Move Down
case SWT.ARROW_DOWN:
if (playing && !paused)
{
if (gameBoard.canMoveDown(currentBlock))
{
currentBlock.moveDown();
}
}
break;
// Move down to bottom
case ' ':
if (playing && !paused)
{
y = currentBlock.getY();
currentBlock.clear();
while (gameBoard.canMoveDown(currentBlock))
{
y += 30;
currentBlock.setY(y);
}
currentBlock.setY(y);
currentBlock.draw();
}
break;
// shape shifter
case SWT.ARROW_UP:// ↑ 翻轉(zhuǎn)變形
if (playing && !paused)
{
if (gameBoard.canRotate(currentBlock))
{
currentBlock = currentBlock.deasilBlock();
currentBlock.draw();
} else
{
currentBlock.draw();
}
}
break;
}
}
/**
*
*/
public void quit()
{
gameThread = null;
}
/**
* Pause game
*/
public void pauseGame()
{
paused = true;
gameThread.setPaused(true);
gameBoard.drawMessage("Paused");
}
/**
* restart game
*/
public void resumeGame()
{
paused = false;
gameThread.setPaused(false);
}
class GameThread extends Thread
{
private boolean paused;
/**
* @return boolean
*/
public boolean isPaused()
{
return paused;
}
/**
* @param paused
*/
public void setPaused(boolean paused)
{
this.paused = paused;
}
public void run()
{
while (gameThread == this)
{
runningGame();
try
{
Thread.sleep(delay);
} catch (InterruptedException e)
{
e.printStackTrace();
}
while (gameThread == this && paused)
{
try
{
Thread.sleep(500);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
}
/**
* @return boolean
*/
public boolean isPaused()
{
return paused;
}
/**
* @return boolean
*/
public boolean isTryAgain()
{
return tryAgain;
}
/**
* dis one row add score
*/
public void scoreModify()
{
switch (fullRowOneTime)
{
case 1:
score += 10;
break;
case 2:
score += 30;
break;
case 3:
score += 50;
break;
case 4:
score += 80;
break;
default:
score += 0;
}
main.display.syncExec(new Runnable()
{
public void run()
{
main.setScoreText(score);
}
});
}
private void runningGame()
{
if (gameBoard.canMoveDown(currentBlock))
{
currentBlock.moveDown();
} else
{
gameBoard.addBlock(currentBlock);
gameBoard.draw();
score += 5;
main.display.syncExec(new Runnable()
{
public void run()
{
main.display.syncExec(new Runnable()
{
public void run()
{
main.setScoreText(score);
}
});
}
});
currentBlock = nextBlock;
if (gameBoard.hasFullRow())
{
gameBoard.removeFullRows();
main.display.syncExec(new Runnable()
{
public void run()
{
main.getCurrentBoardCanvas().redraw();
}
});
fullRowOneTime = gameBoard.getFullRowOneTime();
scoreModify();
totalLines = gameBoard.getRemovedLines();
levelModify();
}
if (isGameOver())
{
MyGC.setMyGC(nextGC);
nextBoard.clear();
MyGC.setMyGC(gameGC);
gameOver();
} else
{
currentBlockType = currentBlock.getBlockType();
nextBoard.setCurrentBlock(currentBlockType);
nextBlock = Block.getRandomBlock(120, 0, display);
nextBlockType = nextBlock.getBlockType();
nextBoard.setNextBlock(nextBlockType);
MyGC.setMyGC(nextGC);
nextBoard.draw();
MyGC.setMyGC(gameGC);
currentBlock.draw();
}
}
}
/**
* Level modified. if level modeified, the down speed will be changed
*/
public void levelModify()
{
if (totalLines >= 10 && totalLines < 20)
{
level = 2;
delay = 800;
}
if (totalLines >= 20 && totalLines < 35)
{
level = 3;
delay = 700;
}
if (totalLines >= 35 && totalLines < 55)
{
level = 4;
delay = 600;
}
if (totalLines >= 55 && totalLines < 80)
{
level = 5;
delay = 500;
}
if (totalLines >= 80 && totalLines < 110)
{
level = 6;
delay = 400;
}
if (totalLines >= 110 && totalLines < 145)
{
level = 7;
delay = 300;
}
if (totalLines >= 145 && totalLines < 185)
{
level = 8;
delay = 200;
}
if (totalLines >= 185 && totalLines < 230)
{
level = 9;
delay = 100;
}
if (totalLines >= 230)
{
level = 10;
delay = 50;
}
main.display.syncExec(new Runnable()
{
public void run()
{
main.setLevelText(level);
main.setScoreText(score);
}
});
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -