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

📄 shape.java

📁 这是一个贪食蛇的游戏源代码 代码的设计模式室调停者
💻 JAVA
字号:
package com.zhanggang.teris.entities;

import java.awt.Graphics;

import com.zhang.teris.listener.ShapeListener;
import com.zhang.teris.util.Global;

public class Shape {

	private int[][] body;
	private int status;
	private int left = Global.WIDTH / 2;
	private int top;
	
	public static final int ROTATE = 0;
	public static final int LEFT = 1;
	public static final int RIGHT = 2;
	public static final int DOWN = 3;
	
	
	
	private ShapeListener shapeListener;
	
	public void moveLeft() {
		System.out.println("Shape's moveLeft");
		left --;
	}

	public void moveRight() {
		System.out.println("Shape's moveRight");
		left ++;
	}

	public void moveDown() {
		System.out.println("Shape's moveDown");
		top ++;
	}

	public void rotate() {
		System.out.println("Shape's rotate");
		status = (status + 1) % body.length;
	}
	
	public int[] getPresentBody() {
		return body[status];
	}
	
	public int[] getRotateBody() {
		int tempStatus = (status + 1) % body.length;
		return body[tempStatus];
	}
	
	public void drawMe(Graphics g) {
		System.out.println("Shape's drawMe");
		for(int y=0; y<4; y++) {
			for(int x=0; x<4; x++) {
				if(getFlagByPoint(x, y)) {
					g.fill3DRect((left + x) * Global.CELL_SIZE , (top + y) * Global.CELL_SIZE,
							Global.CELL_SIZE, Global.CELL_SIZE, true); 
				}
			}
		}
	}
	
	/**
	 * 
	 * @param x 列
	 * @param y 行
	 * @return
	 */
	public boolean getFlagByPoint(int x, int y) {
		if(body[status][y * 4 + x] == 1)
			return true;
		else return false;
	}

	private class ShapeDirver implements Runnable {

		public void run() {
			while(shapeListener.isMoveDownable(Shape.this)) {
				moveDown();
				shapeListener.shpeMoveDown(Shape.this);
				try {
					Thread.sleep(100 * Global.GAME_SPEED);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public void addShapeListener(ShapeListener shapeListener) {
		if(shapeListener != null) {
			this.shapeListener = shapeListener;
		}
	}
	
	public void runShapeDirver() {
		new Thread(new ShapeDirver()).start();		
	}

	public int[][] getBody() {
		return body;
	}

	public void setBody(int[][] body) {
		this.body = body;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public int getLeft() {
		return left;
	}

	public void setLeft(int left) {
		this.left = left;
	}

	public int getTop() {
		return top;
	}

	public void setTop(int top) {
		this.top = top;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -