部分代码如下:
1.中央控制器:Controller.java代码;
package cn.itcast.controller;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
import cn.itcast.Ground;
import cn.itcast.Shape;
import cn.itcast.ShapeFactory;
import cn.itcast.listener.ShapeListener;
import cn.itcast.view.GamePanel;
public class Controller extends KeyAdapter implements ShapeListener
{
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);
}
//输出游戏结束
if(ground.isFull())
{
JOptionPane.showMessageDialog(null,"GAME OVER");
System.exit(0);
}
return false;
}
public void shapeMoveDown(Shape shape)
{
gamePanel.display(ground, shape);
}
private Shape shape;
private ShapeFactory shapeFactory;
private Ground ground;
private GamePanel gamePanel;
public Controller( ShapeFactory shapeFactory, Ground ground,
GamePanel gamePanel) {
super();
this.shapeFactory = shapeFactory;
this.ground = ground;
this.gamePanel = gamePanel;
}
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: