?? terisview.java
字號:
package bianp.teris;
import java.util.Random;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Style;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
class BrickShape {
short[] groupx = new short[4];
short[] groupy = new short[4];
static BrickShape[] _shapes;
public static final int SHAPE_COUNT = 6;
static {
// 都以左下角為坐標點
short[] shapes = {
//
// ##
// ##
0, 0, 0, -1, 1, 0, 1, -1,
//
0, 0, 0, -1, 1, 0, 1, -1,
//
0, 0, 0, -1, 1, 0, 1, -1,
//
0, 0, 0, -1, 1, 0, 1, -1,
// _#__#_______#
// ###_##_###_##
// ____#___#___#
0, 0, -1, 0, 1, 0, 0, -1,
//
0, 0, 0, 1, 0, -1, 1, 0,
//
0, 0, -1, 0, 1, 0, 0, 1,
//
0, 0, 0, -1, 0, 1, -1, 0,
// ## # #
// _# ### # ###
// _# ## #
//
0, 0, 0, -1, 0, -2, 1, 0,
//
0, 0, 1, 0, 2, 0, 0, 1,
//
0, 0, 0, 1, 0, 2, -1, 0,
//
0, 0, -1, 0, -2, 0, 0, -1,
// # ##
// # # # ###
// ## ### # #
//
0, 0, 0, -1, 0, -2, -1, 0,
//
0, 0, 1, 0, 2, 0, 0, -1,
//
0, 0, 0, 1, 0, 2, 1, 0,
//
0, 0, -1, 0, -2, 0, 0, 1,
// ####
0, 0, -1, 0, 1, 0, 2, 0,
//
0, 0, 0, -1, 0, 1, 0, 2,
//
0, 0, -1, 0, -2, 0, 1, 0,
//
0, 0, 0, -1, 0, -2, 0, 1,
// ## #
// ## ##
// #
0, 0, 0, -1, -1, -1, 1, 0,
//
0, 0, 1, 0, 1, -1, 0, 1,
//
0, 0, 0, -1, -1, -1, 1, 0,
//
0, 0, 1, 0, 1, -1, 0, 1,
//
};
_shapes = new BrickShape[4 * SHAPE_COUNT];
for (int i = 0; i < SHAPE_COUNT; ++i) {
for (int j = 0; j < 4; ++j) {
BrickShape bs = new BrickShape();
_shapes[i * 4 + j] = bs;
for (int k = 0; k < 4; ++k) {
int l = (i * 4 + j) * 8 + 2 * k;
bs.groupx[k] = shapes[l];
bs.groupy[k] = shapes[l + 1];
}
}
}
}
static BrickShape getShape(int shapeID, int angleID) {
return _shapes[shapeID * 4 + angleID];
}
}
public class TerisView extends View {
private Paint _paint;
private TerisConfig _config;
private Rect _terisRect = new Rect();
private Rect _clientRect = new Rect();
private IntQueue _keyQueue = new IntQueue(2);
private boolean[] _bricks;
private Drawable _brickImg;
private BrickShape _curShape = BrickShape.getShape(0, 0);
private int _curX;
private int _curY;
private int _curAngleID;
private int _curShapeID;
public TerisView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
public TerisView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public TerisView(Context context) {
super(context);
init(context);
}
protected void init(Context context) {
_paint = new Paint();
_config = new TerisConfig();
_bricks = new boolean[_config.rows * _config.cols];
// Load Drawable
_brickImg = context.getResources().getDrawable(R.drawable.brick_blue);
Log.d("bianp", "initialized...");
}
private boolean getXY(int x, int y) {
if (x >= 0 && x < _config.cols && y >= 0 && y < _config.rows)
return _bricks[y * _config.cols + x];
else
return true;
}
private void setXY(int x, int y, boolean b) {
if (x >= 0 && x < _config.cols && y >= 0 && y < _config.rows) {
// Log.d("bianp", "set(" + x +","+y+")" + b);
_bricks[y * _config.cols + x] = b;
}
}
@Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
super.onLayout(changed, left, top, right, bottom);
this.setFocusable(true);
// 計算一些東東。。。
_clientRect.set(left, top, right, bottom);
Rect r = _terisRect;
TerisConfig c = _config;
r.right = c.cols * c.cellSize;
r.bottom = c.rows * c.cellSize;
r.offset((_clientRect.width() - r.width()) / 2,
(_clientRect.height() - r.height()) / 2);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent msg) {
_keyQueue.push(keyCode);
return super.onKeyDown(keyCode, msg);
}
@Override
protected void onDraw(Canvas canvas) {
final Paint paint = _paint;
paint.setColor(0xff993300);
paint.setStyle(Style.FILL);
canvas.drawRect(_clientRect, paint);
paint.setColor(0xff000000);
canvas.drawRect(_terisRect, paint);
drawBricks(canvas, paint);
// Log.d("bianp", "draw");
}
private void drawBricks(Canvas canvas, Paint paint) {
int w = _config.cols;
int h = _config.rows;
for (int x = 0; x < w; ++x) {
for (int y = 0; y < h; ++y) {
if (getXY(x, y))
drawBrick(canvas, x, y, paint);
}
}
}
private void drawBrick(Canvas canvas, int x, int y, Paint paint) {
int w = _config.cellSize;
int mx = _terisRect.left + x * w;
int my = _terisRect.top + y * w;
_brickImg.setBounds(mx, my, mx + w, my + w);
_brickImg.draw(canvas);
}
private Random rnd = new Random();
private boolean _missionFailed;
/**
* 處理鍵盤事件
*/
public void falldown() {
if (_missionFailed)
return;
// 先擦除
for (int i = 0; i < 4; ++i) {
int x = _curX + _curShape.groupx[i];
int y = _curY + _curShape.groupy[i];
setXY(x, y, false);
}
boolean stoped = false;
// 判斷是否停止
int nY = _curY + 1;
for (int i = 0; i < 4; ++i) {
int x = _curX + _curShape.groupx[i];
int y = nY + _curShape.groupy[i];
if ((y >= _config.rows)
|| (x >= 0 && x < _config.cols && y >= 0
&& y < _config.rows && getXY(x, y))) {
stoped = true;
break;
}
}
if (!stoped)
++_curY;
for (int i = 0; i < 4; ++i) {
int x = _curX + _curShape.groupx[i];
int y = _curY + _curShape.groupy[i];
setXY(x, y, true);
}
if (stoped) {
removeCompleteRow();
_curShapeID = rnd.nextInt(BrickShape.SHAPE_COUNT);
_curAngleID = 0;
_curShape = BrickShape.getShape(_curShapeID, _curAngleID);
_curX = _config.cols / 2;
_curY = 0;
for (int i = 0; i < 4; ++i) {
int x = _curX + _curShape.groupx[i];
int y = _curY + _curShape.groupy[i];
if (x >= 0 && x < _config.cols && y >= 0 && y < _config.rows
&& getXY(x, y)) {
Log.d("bianp", "Mission Failed!");
_missionFailed = true;
}
setXY(x, y, true);
}
}
}
private void removeCompleteRow() {
for (int y = _config.rows - 1; y >= 0;) {
int count = 0;
for (int x = 0; x < _config.cols; ++x) {
if (getXY(x, y))
++count;
}
if (count == 0)
break;
if (count == _config.cols) {
// 需要剔除
for (int i = y; i >= 1; --i) {
for (int j = 0; j < _config.cols; ++j) {
setXY(j, i, getXY(j, i - 1));
}
}
for (int j = 0; j < _config.cols; ++j) {
setXY(j, 0, false);
}
} else {
--y;
}
}
}
public void processkey() {
// 處理鍵盤事件
if (_keyQueue.empty())
return;
// 先擦除
for (int i = 0; i < 4; ++i) {
int x = _curX + _curShape.groupx[i];
int y = _curY + _curShape.groupy[i];
setXY(x, y, false);
}
int k = _keyQueue.shift();
int nx = _curX;
int ny = _curY;
BrickShape nshape = _curShape;
int newangle = _curAngleID;
switch (k) {
case KeyEvent.KEYCODE_DPAD_LEFT:
nx -= 1;
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
nx += 1;
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
ny += 3;
break;
case KeyEvent.KEYCODE_DPAD_UP:
case KeyEvent.KEYCODE_DPAD_CENTER:
// transform...
newangle = (newangle + 1) % 4;
nshape = BrickShape.getShape(_curShapeID, newangle);
break;
default:
return;
}
for (int i = 0; i < 4; ++i) {
int x = nx + nshape.groupx[i];
int y = ny + nshape.groupy[i];
if (getXY(x, y)) {
nshape = null;
break;
}
}
if (nshape != null) {
_curX = nx;
_curY = ny;
_curAngleID = newangle;
_curShape = nshape;
}
for (int i = 0; i < 4; ++i) {
int x = _curX + _curShape.groupx[i];
int y = _curY + _curShape.groupy[i];
setXY(x, y, true);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -