?? tetrisview.java
字號(hào):
package com.google.andriod.tetris;
import java.util.ArrayList;
import java.util.Map;
import java.util.Random;
import android.content.Context;
import android.content.Resources;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
public class TetrisView extends TileView {
private static final String TAG = "TetrisView";
private int mMode = READY;
public static final int PAUSE = 0;
public static final int READY = 1;
public static final int RUNNING = 2;
public static final int LOSE = 3;
private static final int STAR = 1;
private static final int AUTODOWN =0;
private static final int GOLEFT =1;
private static final int GORIGHT=2;
private static final int ROTATE =3;
private static final int FALLEN =4;
private int mControl =AUTODOWN;
private int mlevel = 1;
private long mScore = 0;
private long mMoveDelay = 300;
private long mLastMove;
private TextView mStatusText;
private TextView mInfoText;
private static final Random RNG = new Random();
private Shape mShape = null;
private RefreshHandler mRedrawHandler = new RefreshHandler();
class RefreshHandler extends Handler{
@Override
public void handleMessage(Message msg) {
TetrisView.this.update();
TetrisView.this.invalidate();
}
public void sleep(long delayMillis) {
this.removeMessages(0);
sendMessageDelayed(obtainMessage(0), delayMillis);
}
};
public TetrisView(Context context, AttributeSet attrs, Map inflateParams,
int defStyle){
super(context, attrs, inflateParams, defStyle);
initTetrisView();
}
public TetrisView(Context context, AttributeSet attrs, Map inflateParams){
super(context, attrs, inflateParams);
initTetrisView();
}
private void initTetrisView(){
setFocusable(true);
Resources r = this.getContext().getResources();
loadTile(r.getDrawable(R.drawable.star));
}
private void initNewGame(){
mShape = new Shape(mXTileCount/2-1,1,RNG.nextInt(6));
updateInfo();
}
public Bundle saveState(){
Bundle map = new Bundle();
map.putLong("mScore", Long.valueOf(mScore));
map.putInteger("mLevel", Integer.valueOf(mlevel));
return map;
}
public void restoreState(Bundle icicle){
setMode(PAUSE);
mScore = icicle.getLong("mScore");
mlevel = icicle.getInteger("mLevel");
}
public boolean onKeyDown(int keyCode, KeyEvent msg) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
if (mMode == READY | mMode == LOSE) {
initNewGame();
setMode(RUNNING);
clearTiles();
invalidate();
update();
return (true);
}
if (mMode == PAUSE) {
if(mShape == null){
initNewGame();
clearTiles();
invalidate();
}
setMode(RUNNING);
update();
return (true);
}
}
if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
if(mMode == RUNNING){
mControl = GOLEFT;
}
return (true);
}
if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
if(mMode == RUNNING){
mControl = GORIGHT;
}
return (true);
}
if(keyCode == KeyEvent.KEYCODE_DPAD_UP){
if(mMode == RUNNING){
mControl = ROTATE;
}
return (true);
}
if(keyCode == KeyEvent.KEYCODE_DPAD_DOWN){
if(mMode == RUNNING){
mControl = FALLEN;
}
return (true);
}
return super.onKeyDown(keyCode, msg);
}
public void setTextView(TextView newView){
mStatusText = newView;
}
public void setInfoTextView(TextView newView){
mInfoText = newView;
}
public void setMode(int newMode){
int oldMode = mMode;
mMode = newMode;
if (newMode == RUNNING & oldMode != RUNNING) {
mStatusText.setVisibility(View.INVISIBLE);
return;
}
Resources res = getContext().getResources();
CharSequence str = "";
if (newMode == PAUSE) {
str = res.getText(R.string.mode_pause);
}
if (newMode == READY) {
str = res.getText(R.string.mode_ready);
}
if (newMode == LOSE) {
str = res.getString(R.string.mode_lose_prefix) + mScore
+ res.getString(R.string.mode_lose_suffix);
}
mStatusText.setText(str);
mStatusText.setVisibility(View.VISIBLE);
}
public void update(){
if(mMode == RUNNING){
long now = System.currentTimeMillis();
if (now - mLastMove > mMoveDelay) {
updateShape();
updateInfo();
}
}
if(mScore > 5000){
mMoveDelay = 100;
mlevel = 2;
}
else if(mScore > 2000){
mMoveDelay = 200;
mlevel = 3;
}
mRedrawHandler.sleep(mMoveDelay);
}
private boolean tryRotate(Shape shape){
int temp;
Shape tempShape= new Shape(shape);
tempShape.Rotate();
//check for mask
for(int i=0;i<4;i++){
if(checkTile(tempShape.vertex[i].x, tempShape.vertex[i].y)>0)
return false;
}
//check for left boundary
temp = tempShape.vertex[0].x;
for(int i=1; i<4; i++){
if(tempShape.vertex[i].x<temp)
temp = tempShape.vertex[i].x;
}
if(temp<0)
return false;
//check for right boundary
temp = tempShape.vertex[0].x;
for(int i=1; i<4; i++){
if(tempShape.vertex[i].x>temp)
temp = tempShape.vertex[i].x;
}
if(temp>mXTileCount-1)
return false;
//check for bottom boundary
temp = tempShape.vertex[0].y;
for(int i=1; i<4; i++){
if(tempShape.vertex[i].y>temp)
temp = tempShape.vertex[i].y;
}
if(temp>mYTileCount-1)
return false;
return true;
}
private boolean tryDown(Shape shape){
int temp;
Shape tempShape= new Shape(shape);
tempShape.MoveDown();
//check for mask
for(int i=0;i<4;i++){
if(checkTile(tempShape.vertex[i].x, tempShape.vertex[i].y)>0)
return false;
}
//check for bottom boundary
temp = tempShape.vertex[0].y;
for(int i=1; i<4; i++){
if(tempShape.vertex[i].y>temp)
temp = tempShape.vertex[i].y;
}
if(temp>mYTileCount-1)
return false;
return true;
}
private boolean tryLeft(Shape shape){
int temp;
Shape tempShape= new Shape(shape);
tempShape.MoveLeft();
//check for mask
for(int i=0;i<4;i++){
if(checkTile(tempShape.vertex[i].x, tempShape.vertex[i].y)>0)
return false;
}
//check for left boundary
temp = tempShape.vertex[0].x;
for(int i=1; i<4; i++){
if(tempShape.vertex[i].x<temp)
temp = tempShape.vertex[i].x;
}
if(temp<0)
return false;
return true;
}
private boolean tryRight(Shape shape){
int temp;
Shape tempShape= new Shape(shape);
tempShape.MoveRight();
//check for mask
for(int i=0;i<4;i++){
if(checkTile(tempShape.vertex[i].x, tempShape.vertex[i].y)>0)
return false;
}
//check for left boundary
temp = tempShape.vertex[0].x;
for(int i=1; i<4; i++){
if(tempShape.vertex[i].x>temp)
temp = tempShape.vertex[i].x;
}
if(temp>mXTileCount-1)
return false;
return true;
}
private void clearShape(){
for(int i=0;i<4;i++){
setTile(0, mShape.vertex[i].x, mShape.vertex[i].y);
}
}
private void drawShape(){
for(int i=0;i<4;i++){
setTile(STAR, mShape.vertex[i].x, mShape.vertex[i].y);
}
}
private void updateInfo(){
String str="score:";
str = str + mScore;
str = str + " level:";
str = str + mlevel;
mInfoText.setText(str);
mInfoText.setVisibility(View.VISIBLE);
}
private void updateShape(){
clearShape();
switch(mControl){
case GOLEFT:
if(tryLeft(mShape)){
mShape.MoveLeft();
}
mControl = AUTODOWN;
break;
case GORIGHT:
if(tryRight(mShape)){
mShape.MoveRight();
}
mControl = AUTODOWN;
break;
case ROTATE:
if(tryRotate(mShape)){
mShape.Rotate();
}
mControl = AUTODOWN;
break;
case FALLEN:
while(tryDown(mShape)){
mShape.MoveDown();
}
drawShape();
mScore += caculScore(destroyRow());
mShape = new Shape(mXTileCount/2-1,1,RNG.nextInt(7));
if(checkLose()){
setMode(LOSE);
}
mControl = AUTODOWN;
break;
default:
if(tryDown(mShape)){
mShape.MoveDown();
}else{
drawShape();
mScore += caculScore(destroyRow());
mShape = new Shape(mXTileCount/2-1,1,RNG.nextInt(7));
if(checkLose()){
setMode(LOSE);
}
mControl = AUTODOWN;
}
break;
}
drawShape();
}
private boolean checkLose(){
for(int i=0; i<4; i++){
if(checkTile(mShape.vertex[i].x, mShape.vertex[i].y)>0)
return true;
}
return false;
}
private long caculScore(int lines){
long s = (long)Math.pow(2, lines-1);
return s * 100;
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -