⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 controller.java

📁 自己写的一个Java版本的俄罗斯方块
💻 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 + -