📄 game.java
字号:
package xn.tetris;
import java.io.IOException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Display;
/**
* 操作游戏的类
* */
public class Game {
TetrisBoard tetrisBoard;
NextBlockBoard nextBoard;
Block currentBlock;
Block nextBlock;
String msg;
private int delay;
private int score;
private int highestScore;
private int highestLevel;
private int fullRowOneTime;
private int level;
private int totalLines;
private int currentBlockType;
private int nextBlockType;
private boolean playing;
private boolean paused;
private boolean tryAgain;
private GC tetrisGC;
private GC nextGC;
private Display display;
private Main main;
private GameThread gameThread;
private SaveFileIO io;
public Game(int x_columns, int y_rows, Display display, Main main){
this.display = display;
this.main = main;
this.tetrisGC = new GC(main.getTetrisBoardCanvas());
this.nextGC = new GC(main.getNextBoardCanvas());
MyGC.setMyGC(tetrisGC);
io = new SaveFileIO();
tetrisBoard =
new TetrisBoard(x_columns, y_rows, display);
nextBoard = new NextBlockBoard(display);
}
public Game(){
}
//得到当前游戏是否暂停
public boolean isPaused(){
return paused;
}
//得到最高分
public int getHighestScore(){
return highestScore;
}
//得到最高分的最高等级
public int getHighestLevel(){
return highestLevel;
}
//开始游戏
public void startGame(){
delay = 500;
score = 0;
level = 1;
main.display.syncExec(new Runnable(){
public void run() {
// TODO Auto-generated method stub
main.setScoreText(score);
main.setLevelText(level);
}
});
totalLines = 0;
playing = true;
tryAgain = false;
tetrisBoard.init();
tetrisBoard.setRemovedLines(0);//将消行总数设为0
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(tetrisGC);
gameThread = new GameThread();
gameThread.start();
}
//暂停游戏
public void pauseGame(){
paused = true;
gameThread.setPaused(true);
tetrisBoard.drawMessage("Paused");
}
//继续游戏
public void resumeGame(){
paused = false;
gameThread.setPaused(false);
}
//通过键盘控制游戏
public void controlGame(KeyEvent e){
switch(e.keyCode){
case 'p'://暂停游戏
if(playing && !paused){
pauseGame();
main.display.syncExec(new Runnable(){
public void run() {
// TODO Auto-generated method stub
main.setBtStartPauseText("Resume");
}
});
}
break;
case 'r'://继续游戏
if(playing && paused){
resumeGame();
main.display.syncExec(new Runnable(){
public void run() {
// TODO Auto-generated method stub
main.setBtStartPauseText("Pause");
main.getTetrisBoardCanvas().redraw();
}
});
}
break;
case SWT.ARROW_LEFT://<-- 向左移动
if(playing && !paused){
if(tetrisBoard.canMoveLeft(currentBlock)){
currentBlock.move(Block.MoveSuit.MOVELEFT, tetrisBoard);
}
}
break;
case SWT.ARROW_RIGHT://--> 向右移动右
if(playing && !paused){
if(tetrisBoard.canMoveRight(currentBlock)){
currentBlock.move(Block.MoveSuit.MOVERIGHT, tetrisBoard);
}
}
break;
case SWT.ARROW_DOWN://↓ 向下移动
if(playing && !paused){
if(tetrisBoard.canMoveDown(currentBlock)){
currentBlock.move(Block.MoveSuit.MOVEDOWN, tetrisBoard);
}
}
break;
case ' '://空格 直降到底
if(playing && !paused){
currentBlock.move(Block.MoveSuit.FALL, tetrisBoard);
}
break;
case SWT.ARROW_UP://↑ 翻转变形
if(playing && !paused){
if(tetrisBoard.canRotate(currentBlock)){
currentBlock.move(Block.MoveSuit.ROTATE, tetrisBoard);
}
}
break;
}
}
//判断游戏是否gameover过,若从没gameover则返回false
public boolean isTryAgain(){
return tryAgain;
}
//判断游戏是否gameover
public boolean isGameOver(){
for(int x = 0; x < 10; x++ ){
if(tetrisBoard.getPositionValue(x, 0) != tetrisBoard.EMPTY){
return true;
}
}
return false;
}
//游戏gameover
public void gameOver(){
quit();
tetrisBoard.drawMessage("Game Over");
main.display.syncExec(new Runnable(){
public void run() {
// TODO Auto-generated method stub
main.setBtStartPauseText("Try Again");
}
});
playing = false;
tryAgain = true;
}
//退出游戏线程
public void quit(){
gameThread = null;
}
//运行游戏时,需要做的事
private void runningGame(){
if(tetrisBoard.canMoveDown(currentBlock)){
currentBlock.move(Block.MoveSuit.MOVEDOWN, tetrisBoard);
}
else{
tetrisBoard.addBlock(currentBlock);
tetrisBoard.draw();
score += 10;
main.display.syncExec(new Runnable(){
public void run() {
// TODO Auto-generated method stub
main.setScoreText(score);
}
});
currentBlock = nextBlock;
if(tetrisBoard.hasFullRow()){
tetrisBoard.removeFullRows();
main.display.syncExec(new Runnable(){
public void run() {
// TODO Auto-generated method stub
main.getTetrisBoardCanvas().redraw();
}
});
fullRowOneTime = tetrisBoard.getFullRowOneTime();
scoreModify();
totalLines = tetrisBoard.getRemovedLines();
levelModify();
}
if(isGameOver()){
MyGC.setMyGC(nextGC);
nextBoard.clear();
MyGC.setMyGC(tetrisGC);
gameOver();
int highestScoreBefore = 0;
try {
highestScoreBefore = Integer.valueOf(io.readFromFile()[1]);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(score > highestScoreBefore){
final Game myGame = this;
main.display.syncExec(new Runnable(){
public void run() {
// TODO Auto-generated method stub
highestScore = score;
highestLevel = level;
new InputHighestNameUI(main, myGame);
}
});
}
}
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(tetrisGC);
currentBlock.draw();
}
}
}
//等级下落间隔的改变
public void levelModify(){
if(totalLines >= 10 && totalLines < 25){
level = 2;
delay = 450;
}
if(totalLines >= 25 && totalLines < 45){
level = 3;
delay = 400;
}
if(totalLines >= 45 && totalLines < 70){
level = 4;
delay = 350;
}
if(totalLines >= 70 && totalLines < 100){
level = 5;
delay = 300;
}
if(totalLines >= 100 && totalLines < 135){
level = 6;
delay = 250;
}
if(totalLines >= 135 && totalLines < 175){
level = 7;
delay = 200;
}
if(totalLines >= 175 && totalLines < 220){
level = 8;
delay = 150;
}
if(totalLines >= 220 && totalLines < 270){
level = 9;
delay = 100;
}
if(totalLines >= 270){
level = 10;
delay = 50;
}
main.display.syncExec(new Runnable(){
public void run() {
// TODO Auto-generated method stub
main.setLevelText(level);
main.setScoreText(score);
}
});
}
//分数的改变
public void scoreModify(){
switch(fullRowOneTime){
case 1:
score += 30;
break;
case 2:
score += 50;
break;
case 3:
score += 80;
break;
case 4:
score += 120;
break;
}
main.display.syncExec(new Runnable(){
public void run() {
// TODO Auto-generated method stub
main.setScoreText(score);
}
});
}
//得到当前正在下落的方块
public Block getCurrentBlock(){
return currentBlock;
}
//设置当前正在下落的方块
public void setCurrentBlock(Block currentBlock){
this.currentBlock = currentBlock;
}
//得到下一个下落的方块
public Block getNextBlock(){
return nextBlock;
}
//设置下一个下落的方块
public void setNextBlock(Block nextBlock){
this.nextBlock = nextBlock;
}
//判断游戏是否在运行中
public boolean isPlaying(){
return playing;
}
//设置游戏是否在运行中
public void setPlaying(boolean playing){
this.playing = playing;
}
//游戏线程类
class GameThread extends Thread{
private boolean paused;
//返回游戏是否暂停
public boolean isPaused(){
return paused;
}
//设置游戏是否暂停
public void setPaused(boolean paused){
this.paused = paused;
}
public void run(){
while(gameThread == this){
runningGame();
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(gameThread == this && paused){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -