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

📄 controller.java

📁 这是一个贪食蛇的游戏源代码 代码的设计模式室调停者
💻 JAVA
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -