?? controller.java
字號:
package cn.itcast.tetris.controller;
import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JLabel;
import cn.itcast.tetris.entities.Ground;
import cn.itcast.tetris.entities.Shape;
import cn.itcast.tetris.entities.ShapeFactory;
import cn.itcast.tetris.listener.GameListener;
import cn.itcast.tetris.listener.GroundListener;
import cn.itcast.tetris.listener.ShapeListener;
import cn.itcast.tetris.util.Global;
import cn.itcast.tetris.view.GamePanel;
/**
* 控制器<BR>
* 控制Ground, Snake, Food<BR>
* 負責游戲的邏輯<BR>
* 處理按鍵事件<BR>
* <BR>
*
* @version 1.0, 01/01/08
*
* @author 湯陽光
*
*/
public class Controller extends KeyAdapter implements ShapeListener,
GroundListener {
protected Set<GameListener> listeners = new HashSet<GameListener>();
/**
* 圖形工廠
*
*/
protected ShapeFactory shapeFactory;
protected Shape shape;
protected Ground ground;
protected GamePanel gamePanel;
protected JLabel gameInfoLabel;
/**
* 當前的游戲狀態
*/
protected boolean playing;
/**
*
* @param shapeFactory
* @param ground
* @param gamePanel
*/
public Controller(ShapeFactory shapeFactory, Ground ground,
GamePanel gamePanel) {
super();
this.shapeFactory = shapeFactory;
this.ground = ground;
this.gamePanel = gamePanel;
}
/**
* 多接受一個 JTextComponent, 可以給在這個組件上顯示提示信息
*
* @param shapeFactory
* @param ground
* @param gamePanel
* @param gameInfoLabel
*/
public Controller(ShapeFactory shapeFactory, Ground ground,
GamePanel gamePanel, JLabel gameInfoLabel) {
this(shapeFactory, ground, gamePanel);
this.setGameInfoLabel(gameInfoLabel);
}
/**
* 處理鍵盤按鍵 <BR>
* LEFT: 向左移動<BR>
* RIGHT:向右移動<BR>
* DOWN: 向下移動<BR>
* UP: 變形<BR>
* PAGE UP: 加快速度<BR>
* PAGE DOWN: 減慢速度<BR>
* Y: 重新開始游戲<BR>
* ENTER: 暫停/繼續
*/
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() != KeyEvent.VK_Y && !playing)
return;
switch (e.getKeyCode()) {
/**
* 方向左
*/
case KeyEvent.VK_LEFT:
if (isPausingGame()) {
this.continueGame();
}
shape.setSwift(false);
if (isPlaying() && ground.isMoveable(shape, Shape.LEFT))
shape.moveLeft();
break;
/**
* 方向右
*/
case KeyEvent.VK_RIGHT:
if (isPausingGame()) {
this.continueGame();
}
shape.setSwift(false);
if (isPlaying() && ground.isMoveable(shape, Shape.RIGHT))
shape.moveRight();
break;
/**
* 方向上
*/
case KeyEvent.VK_UP:
if (isPlaying()) {
if (!shape.isPause()) {
if (ground.isMoveable(shape, Shape.ROTATE)) {
shape.setSwift(false);
shape.rotate();
}
} else {
if (ground.isMoveable(shape, Shape.UP))
shape.moveUp();
else {
shape.die();
shape = shapeFactory.getShape(this);
}
}
}
break;
/**
* 方向下
*/
case KeyEvent.VK_DOWN:
if (isPausingGame()) {
this.continueGame();
}
if (isPlaying() && isShapeMoveDownable(shape))
shape.moveDown();
break;
/**
* PAGE UP
*/
case KeyEvent.VK_PAGE_UP:
shape.speedUp();
break;
/**
* PAGE DOWN
*/
case KeyEvent.VK_PAGE_DOWN:
shape.speedDown();
break;
/**
* 反引號,換一個圖形
*/
case KeyEvent.VK_BACK_QUOTE:
if (isPlaying()) {
shape.die();
shape = shapeFactory.getShape(this);
}
break;
case KeyEvent.VK_ENTER:
if (isPausingGame())
this.continueGame();
else
this.pauseGame();
break;
case KeyEvent.VK_Y:
if (!isPlaying())
newGame();
break;
case KeyEvent.VK_SPACE:
if (isPlaying() && !isPausingGame())
shape.setSwift(true);
break;
}
/**
* 重新顯示
*/
gamePanel.redisplay(ground, shape);
if (gameInfoLabel != null)
gameInfoLabel.setText(this.getNewInfo());
}
/**
* 詢問一下圖形是否可以下落,如果不能下落了,就會讓圖形變成障礙物<BR>
* 這個方法是同步的
*/
public synchronized boolean isShapeMoveDownable(Shape s) {
if (shape == null)
return true;
if (!playing || shape != s)
return false;
if (ground.isMoveable(shape, Shape.DOWN))
return true;
shape.die();
ground.accept(shape);
if (playing && !ground.isFull()) {
shape = shapeFactory.getShape(this);
}
gamePanel.redisplay(ground, shape);
if (gameInfoLabel != null)
gameInfoLabel.setText(this.getNewInfo());
return false;
}
/**
* 處理圖形觸發的 shapeMovedDown (圖形下落) 事件<BR>
* 將會重新顯示
*/
public void shapeMovedDown(Shape s) {
// TODO Auto-generated method stub
if (playing && ground != null && shape != null)
gamePanel.redisplay(ground, shape);
}
/**
* 開始一個新游戲
*/
public void newGame() {
playing = true;
ground.init();
ground.addGroundListener(this);
Global.CURRENT_SPEED = Global.DEFAULT_SPEED;
shape = shapeFactory.getShape(this);
if (playing)
gamePanel.redisplay(ground, shape);
if (gameInfoLabel != null)
gameInfoLabel.setText(this.getNewInfo());
for (GameListener l : listeners)
l.gameStart();
}
/**
* 停止當前游戲
*/
public void stopGame() {
if (shape == null)
return;
playing = false;
for (GameListener l : listeners)
l.gameOver();
}
/**
* 暫停游戲
*/
public void pauseGame() {
if (shape == null)
return;
shape.setPause(true);
for (GameListener l : listeners)
l.gamePause();
}
/**
* 繼續游戲
*/
public void continueGame() {
shape.setPause(false);
for (GameListener l : listeners)
l.gameContinue();
}
/**
* 游戲是否是在暫停狀態
*/
public boolean isPausingGame() {
return shape.isPause();
}
/**
* 獲得游戲的最新提示信息
*
* @return
*/
public String getNewInfo() {
if (!playing || ground.isFull())
return " ";// "提示: 按 Y 開始新游戲";
else
return new StringBuffer().append("提示: ").append(" 速度 ").append(
shape.getSpeed()).append("毫秒/格").toString();
}
public ShapeFactory getShapeFactory() {
return shapeFactory;
}
public void setShapeFactory(ShapeFactory shapeFactory) {
this.shapeFactory = shapeFactory;
}
public Ground getGround() {
return ground;
}
public void setGround(Ground ground) {
this.ground = ground;
}
public GamePanel getGamePanel() {
return gamePanel;
}
public void setGamePanel(GamePanel gamePanel) {
this.gamePanel = gamePanel;
}
/**
* 處理Ground 觸發的 beforeDeleteFullLine 事件將會改變滿行的顏色并暫停一段時間
*/
public void beforeDeleteFullLine(Ground ground, int lineNum) {
// TODO Auto-generated method stub
ground.changeFullLineColor(lineNum);
gamePanel.redisplay(ground, shape);
try {
Thread.sleep(Global.STAY_TIME);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 處理Ground 觸發的 fullLineDeleted 事件, 這個方法什么也沒做, 只是打印了一句話
*/
public void fullLineDeleted(Ground ground, int deletedLineCount) {
// TODO Auto-generated method stub
System.out.println("消了 " + deletedLineCount + " 行");
}
/**
* 是否正在游戲中
*
* @return
*/
public boolean isPlaying() {
if (playing && !ground.isFull())
return true;
return false;
}
/**
* 得到顯示提示信息的組件
*
* @return
*/
public JLabel getGameInfoLabel() {
return gameInfoLabel;
}
/**
* 設置
*
* @param gameInfoLabel
*/
public void setGameInfoLabel(JLabel gameInfoLabel) {
this.gameInfoLabel = gameInfoLabel;
this.gameInfoLabel.setSize(Global.WIDTH * Global.CELL_WIDTH, 20);
this.gameInfoLabel.setFont(new Font("宋體", Font.PLAIN, 12));
gameInfoLabel.setText(this.getNewInfo());
}
/**
* 處理Ground 的 groundIsFull() 事件, 將觸發游戲結束事件
*/
public void groundIsFull(Ground ground) {
// TODO Auto-generated method stub
if (playing) {
playing = false;
for (GameListener l : listeners)
l.gameOver();
}
}
/**
* 添加監聽器, 可添加多個
*
* @param l
*/
public void addGameListener(GameListener l) {
if (l != null)
this.listeners.add(l);
}
/**
* 移除監聽器
*
* @param l
*/
public void removeGameListener(GameListener l) {
if (l != null)
this.listeners.remove(l);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -