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

📄 shape.java

📁 自己写的一个Java版本的俄罗斯方块
💻 JAVA
字号:
package lx.entity;

import java.awt.Color;
import java.awt.Graphics;

import lx.listener.ShapeListener;
import lx.util.Global;

//用于图形显示的实体类
public class Shape {

	//图形的变动状态
	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 int[][] body;                    //图形状态数组
	private int     status;                  //图形当前显示状态
	
	//图形位置
	private int left;
	private int top;
	
	private ShapeListener listener;          //Shape监听器
	


	public Shape() {
		//启动线程
	       new Thread(new ShapeDriver()).start();
	}
	
	//向左移动
	public void moveLeft(){
		left--;
	}
	
	//向右移动
	public void moveRight(){
		left++;	
	}
	
	//向下移动
	public void moveDown(){
		top++;
	}
	
	//旋转
	public void rotate(){
		status = (status+1)%body.length;
	}
	
	//图像显示(绘制)
	public void drawMe(Graphics g){
		g.setColor(Color.blue);
		for(int x = 0; x < 4; x++){
			for(int y=0; y<4; y++){
				if(getFlagByPoint(x, y)){
					g.fill3DRect((left+x)*Global.CELL_SIZE, 
							     (top + y)*Global.CELL_SIZE, 
							     Global.CELL_SIZE, Global.CELL_SIZE, 
							     true);
				}
			}
		}
	}
	
	private boolean getFlagByPoint(int x,int y){
		return body[status][y * 4 + x] == 1;
	}

	public boolean isMember(int x,int  y,boolean rotate){
		int tempStatus = status;
		if(rotate){
			tempStatus = (status + 1) % body.length;
		}
		return body[tempStatus][y * 4 + x] == 1;
	}
	
	//图形驱动(线程驱动)
    private class ShapeDriver implements Runnable{
    	public void run() {
    		while(listener.isShapeMoveDownable(Shape.this)){
    			moveDown();
    			listener.shapeMoveDown(Shape.this);
    			try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					System.out.println("图形shape内部类线程错误!");
					e.printStackTrace();
				}
    		}
    	}
    }
	
    //注册监听器
    public void addShapeListener(ShapeListener sl){
    	if(sl != null){
    		this.listener = sl;
    	}
    }
    
	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 + -