首页>代码>java swing开发小游戏俄罗斯方块>/tetris1.4/src/sdu/edu/entities/Shap.java
package sdu.edu.entities;

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

import sdu.edu.listener.ShapListener;
import sdu.edu.util.Global;

public class Shap {
	public static final int RARTE = 0;
	public static final int LEFT = 1;
	public static final int RIGHT = 2;
	public static final int DOWN = 3;
	
	private ShapListener shapListener;
	
	private int status;
	private int[][] body;
	
	private int top;
	private int left;
	
	public Shap() {
		new Thread(new ShapDriver()).start();
	}
	
	public void moveDown() {
		System.out.println("shap's moveDown");
		top ++;
	}
	
	public void moveLeft() {
		System.out.println("shap's moveLeft");
		left --;
	}
	
	public void moveRight() {
		System.out.println("shap's moveRight");
		left ++;
	}
	
	public void rarte() {
		System.out.println("shap's rarte");
		status = (status + 1) % body.length;
	}
	
	public void drawMe(Graphics g) {
		Color c = g.getColor();
		g.setColor(Color.BLUE);
		for(int i = 0; i < 4; i++) {
			for(int j = 0; j < 4; j++) {
				if(getFlagByPoint(status, i, j)){
					g.fill3DRect((left + i) * Global.CELL_WIDTH, (top + j)
							* Global.CELL_HEIGHT, Global.CELL_WIDTH,
							Global.CELL_HEIGHT, true);
				}
			}
		}
		g.setColor(c);
	}
	
	private boolean getFlagByPoint(int status, int x, int y) {
		return body[status][y * 4 + x] == 1;
	}
	
	public boolean isMember(int x, int y, boolean rarte){
		int tempStatus = status; 
		if(rarte){
			tempStatus = (status + 1) % body.length;
		}
	   	return body[tempStatus][y * 4 + x] == 1;
	}
	
	public void addShapListener(ShapListener sl) {
		if(sl != null){
			this.shapListener = sl;
		}
	}
	
	public void setStuta(int stuta) {
		this.status = stuta;
	}

	public int getTop() {
		return top;
	}

	public int getLeft() {
		return left;
	}

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

	private class ShapDriver implements Runnable{

		@Override
		public void run() {
			// TODO Auto-generated method stub
			while (shapListener.isShapMoveDownable(Shap.this) ) {
				moveDown();
				shapListener.shapMoveDown(Shap.this);
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
最近下载更多
1112WHQ  LV7 2023年11月3日
郭佩泽  LV12 2019年8月20日
wasdfg  LV2 2019年6月5日
renshuao0822  LV2 2018年10月31日
120130140  LV7 2018年2月24日
大侠woaini  LV2 2017年12月26日
1357zhanghuan9  LV2 2017年6月28日
13956441640  LV2 2017年6月6日
solargen  LV7 2017年6月6日
sahdhasdi  LV2 2017年6月4日
最近浏览更多
ClydeSon  LV5 2023年12月27日
1112WHQ  LV7 2023年11月3日
微信网友_6699076084797440  LV7 2023年10月30日
wangjialiang1  LV17 2023年8月20日
sdyawd 2023年6月14日
暂无贡献等级
小安同学  LV7 2023年5月11日
tx1121  LV14 2023年4月9日
insane1  LV1 2022年12月6日
tree1112666  LV1 2022年8月19日
喃喵xxxx  LV6 2022年4月20日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友