controller.java
来自「这是一个贪食蛇的游戏源代码 代码的设计模式室调停者」· Java 代码 · 共 73 行
JAVA
73 行
package com.zhang.teris.controller;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import com.zhanggang.teris.entities.ShapeFactory;
import com.zhang.teris.listener.ShapeListener;
import com.zhanggang.teris.entities.Ground;
import com.zhanggang.teris.entities.Shape;
import com.zhanggang.teris.view.GamePanel;
public class Controller extends KeyAdapter implements ShapeListener {
private Ground ground;
private Shape shape;
private GamePanel gamePanel;
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
if(ground.isMovable(shape, Shape.LEFT))
shape.moveLeft();
break;
case KeyEvent.VK_RIGHT:
if(ground.isMovable(shape, Shape.RIGHT))
shape.moveRight();
break;
case KeyEvent.VK_DOWN:
if(ground.isMovable(shape, Shape.DOWN))
shape.moveDown();
break;
case KeyEvent.VK_UP:
if(ground.isMovable(shape, Shape.ROTATE))
shape.rotate();
break;
default:
break;
}
gamePanel.display(ground, shape);
}
public void shpeMoveDown(Shape shape) {
gamePanel.display(ground, shape);
}
public void newGame() {
shape = ShapeFactory.getShape(this);
}
public boolean isGameOver() {
return ground.isFull();
}
public Controller(Ground ground, Shape shape, GamePanel gamePanel) {
this.ground = ground;
this.shape = shape;
this.gamePanel = gamePanel;
}
public boolean isMoveDownable(Shape shape) {
boolean result = ground.isMovable(shape, Shape.DOWN);
if(result == false) {
ground.accept(shape);
if(!isGameOver()) {
this.shape = ShapeFactory.getShape(this);
}
}
return result;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?