?? shape.java
字號:
package cn.itcast.tetris.entities;
import java.awt.Color;
import java.awt.Graphics;
import java.util.TooManyListenersException;
import cn.itcast.tetris.listener.ShapeListener;
import cn.itcast.tetris.util.Global;
/**
*
* 維護一個圖形, 一個圖形可以有一種或多種狀態<BR>
* <BR>
* 可以通過 setColor() 方法改變圖形的顏色<BR>
* <BR>
* 可以通過覆蓋 drawUnit(Graphics, int, int, int, int) 方法改變圖形的顯示<BR>
* <BR>
* 用內部類 ShapeDriver 驅動圖形定時向下移動<BR>
* <BR>
* 使用時一定要給實例注冊監聽器, 否則不能正常運行 <BR>
*
* @version 1.0, 01/01/08
*
* @author 湯陽光
*
*/
public class Shape {
/**
* 變形(旋轉)
*/
public static final int ROTATE = 5;
/**
* 上移
*/
public static final int UP = 1;
/**
* 下落
*/
public static final int DOWN = 2;
/**
* 左移
*/
public static final int LEFT = 3;
/**
* 右移
*/
public static final int RIGHT = 4;
/**
* 監聽器組
*/
protected ShapeListener listener;
/**
*
*/
protected int[][] body;
/**
* 當前顯示的狀態
*/
protected int status;
/**
* 圖形的真實高度
*/
protected int height;
/**
* 左上角的位置
*/
protected int left;
/**
* 左上角的位置
*/
protected int top;
/**
* 下落的速度
*/
protected int speed;
/**
* 生命
*/
protected boolean life;
/**
* 暫停狀態
*/
protected boolean pause;
protected boolean swift;
protected int swiftSpeed = Global.SWIFT_SPEED;
protected Thread shapeThread, swiftThread;
/**
* 顏色
*/
protected Color color = Color.BLUE;
/**
* 指定類型, 指定狀態的構造方法<BR>
* 將會調用 init() 方法
*
* @param body
* @param status
*/
public Shape(int[][] body, int status) {
super();
this.body = body;
this.status = status;
for (int y = 0; y < 4; y++)
for (int x = 0; x < 4; x++)
if (isMember(x, y, false))
height = y + 1;
init();
}
/**
* 初始化位置,速度等
*/
public void init() {
life = true;
pause = false;
swift = false;
left = Global.WIDTH / 2 - 2;
top = 0 - height;
speed = Global.CURRENT_SPEED;
}
/**
* 旋轉(或叫做變形, 顯示下一個狀態)
*/
public void rotate() {
status = (status + 1) % body.length;
}
public void moveUp() {
top--;
}
/**
* 向下移動
*/
public void moveDown() {
top++;
}
/**
* 向左移動
*/
public void moveLeft() {
left--;
}
/**
* 向右移動
*/
public void moveRight() {
left++;
}
/**
* 驅動圖形定時下落的內部類
*
* @version 1.0, 01/01/08
*
* @author 湯陽光
*/
protected class ShapeDriver implements Runnable {
/**
* 驅動圖形定時下落
*/
public void run() {
if (listener == null)
throw new RuntimeException("請先注冊 ShapeListener");
while (life && listener.isShapeMoveDownable(Shape.this)) {
if (!swift) {
if (!pause) {
moveDown();
/**
* 觸發下落事件
*/
listener.shapeMovedDown(Shape.this);
}
}
try {
Thread.sleep(speed);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
life = false;
}
}
/**
* 顯示, 將調用drawUnit(Graphics, int, int, int, int)方法
*
* @param g
*/
public void drawMe(Graphics g) {
/**
* 死了就不畫了,針對游戲結束時的最后一個圖形
*/
if (!life)
return;
g.setColor(color);
for (int x = 0; x < 4; x++)
for (int y = 0; y < 4; y++)
if (getFlagByPoint(status, x, y))
drawUnit(g, (left + x) * Global.CELL_WIDTH, (top + y)
* Global.CELL_HEIGHT, Global.CELL_WIDTH,
Global.CELL_HEIGHT);
}
/**
*
* 畫具體的每一個方塊的方法, 可以覆蓋這個方法改變圖形的顯示
*
* @param g
* @param x
* 像素坐標 x
* @param y
* 像素坐標 y
* @param width
* 寬度(單位:像素)
* @param height
* 高度(單位:像素)
*/
public void drawUnit(Graphics g, int x, int y, int width, int height) {
g.fill3DRect(x, y, width, height, true);
}
/**
* 相對坐標(x,y)是否是圖形中的點
*
* @param x
* 相對坐標x
* @param y
* 相對坐標y
* @return
*/
protected boolean getFlagByPoint(int status, int x, int y) {
return body[status][y * 4 + x] == 1;
}
/**
*
* 指定的位置是否是圖形的一部分
*
* @param x
* x(格子)(相對)坐標
* @param y
* y(格子)(相對)坐標
* @param isRotate
* 是否旋轉了
* @return
*/
public boolean isMember(int x, int y, boolean isRotate) {
return getFlagByPoint(isRotate ? (status + 1) % body.length : status,
x, y);
}
/**
* 加速
*/
public void speedUp() {
if (speed > Global.SPEED_STEP)
speed -= Global.SPEED_STEP;
Global.CURRENT_SPEED = speed;
}
/**
* 減速
*/
public void speedDown() {
speed += Global.SPEED_STEP;
Global.CURRENT_SPEED = speed;
}
/**
* 得到圖形的下落速度
*
* @return
*/
public int getSpeed() {
return speed;
}
/**
* 設置圖形的下落速度
*
* @param speed
*/
public void setSpeed(int speed) {
this.speed = speed;
}
/**
* 得到圖形的暫停狀態
*
* @return
*/
public boolean isPause() {
return pause;
}
/**
* 設置圖形的暫停狀態
*
* @param pause
*/
public void setPause(boolean pause) {
this.pause = pause;
}
/**
* 更改暫停狀態<BR>
* 若是暫停狀態, 則繼續下落<BR>
* 若正在下落, 則暫停
*/
public void changePause() {
this.pause = !this.pause;
}
/**
* 得到圖形的當前是第幾種狀態
*
* @return
*/
public int getStatus() {
return status;
}
/**
* 設置圖形的當前是第幾種狀態
*
* @param status
*/
public void setStatus(int status) {
this.status = status;
}
/**
* 得到圖形的位置
*
* @return
*/
public int getLeft() {
return left;
}
/**
* 設置圖形的位置
*
* @param left
*/
public void setLeft(int left) {
this.left = left;
}
/**
* 得到圖形的位置
*
* @return
*/
public int getTop() {
return top;
}
/**
* 設置圖形的位置
*
* @param top
*/
public void setTop(int top) {
this.top = top;
}
/**
* 得到圖形的顏色
*
* @return
*/
public Color getColor() {
return color;
}
/**
* 設置圖形的顏色
*
* @param color
*/
public void setColor(Color color) {
this.color = color;
}
/**
* 設置圖形的類型(多種狀態)
*
* @param body
*/
public void setBody(int[][] body) {
this.body = body;
}
/**
* 添加監聽器<BR>
* 將會啟動驅動圖形下落的線程
*
* @param l
*/
public void addShapeListener(ShapeListener l) {
if (l == null || this.listener == l)
return;
if (this.listener != null)
throw new RuntimeException(new TooManyListenersException());
this.listener = l;
start();
}
protected void start() {
shapeThread = new Thread(new ShapeDriver());
shapeThread.start();
}
public boolean isLife() {
return life;
}
/**
* 結束圖形定時下落的線程
*/
public synchronized void die() {
this.life = false;
}
public boolean isSwift() {
return swift;
}
/**
* 一落到底
*
* @param swift
*/
public void setSwift(boolean swift) {
if (this.swift == swift)
return;
this.swift = swift;
if (this.swift) {
swiftThread = new Thread(new ShapeSwiftDriver());
swiftThread.start();
}
}
protected class ShapeSwiftDriver implements Runnable {
public void run() {
// TODO Auto-generated method stub
while (swift && life) {
if (listener == null)
throw new RuntimeException("請先注冊 ShapeListener");
if (listener.isShapeMoveDownable(Shape.this)) {
if (!pause) {
moveDown();
/**
* 觸發下落事件
*/
listener.shapeMovedDown(Shape.this);
}
try {
Thread.sleep(swiftSpeed);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
life = false;
}
}
}
}
public int getSwiftSpeed() {
return swiftSpeed;
}
public void setSwiftSpeed(int swiftSpeed) {
this.swiftSpeed = swiftSpeed;
}
public int getHeight() {
return height;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -