📄 controller.java
字号:
package lx.controller;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import lx.entity.Ground;
import lx.entity.Shape;
import lx.entity.ShapeFactory;
import lx.listener.ShapeListener;
import lx.view.GamePanle;
public class Controller extends KeyAdapter implements ShapeListener {
private Shape shape;
private ShapeFactory shapeFactory;
private Ground ground;
private GamePanle gamePanle;
public Controller(ShapeFactory shapeFactory,Ground ground,GamePanle gamePanle) {
this.shapeFactory = shapeFactory;
this.ground = ground;
this.gamePanle = gamePanle;
}
//键盘监听
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
if(ground.isMoveable(shape, Shape.ROTATE)){
shape.rotate();
}
break;
case KeyEvent.VK_DOWN:
if(isShapeMoveDownable(shape)){
shape.moveDown();
}
break;
case KeyEvent.VK_LEFT:
if(ground.isMoveable(shape, Shape.LEFT)){
shape.moveLeft();
}
break;
case KeyEvent.VK_RIGHT:
if(ground.isMoveable(shape, Shape.RIGHT)){
shape.moveRight();
}
break;
}
gamePanle.dispaly(ground, shape);
}
public void shapeMoveDown(Shape shape) {
gamePanle.dispaly(ground, shape);
}
//判断图形是否可以继续下落
public synchronized boolean isShapeMoveDownable(Shape shape) {
if(this.shape != shape){
return false;
}
if(ground.isMoveable(shape,Shape.DOWN)){
return true;
}
ground.accept(this.shape);
//没有满行则生成新的图形
if(!ground.isFull()){
this.shape = shapeFactory.getShape(this);
}
return false;
}
//开始新游戏
public void newGame(){
shape = shapeFactory.getShape(this);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -