안 드 로 이 드 러시아 블록 (소스 코드)
27905 단어 안 드 로 이 드 편
생각:
발생 할 수 있 는 문제 나 난점:
게임 의 운행 인 터 페 이 스 는 다음 과 같다. 기본 적 인 기능 과 조작 은 매우 간단 하 다.
[외부 체인 이미지 저장 에 실 패 했 습 니 다. 원본 사이트 에 도 난 방지 체인 체제 가 있 을 수 있 습 니 다. 그림 을 저장 하여 직접 업로드 하 는 것 을 권장 합 니 다 (img - 3 ShqlKPe - 1595603247955) (https://img-blog.csdn.net/20170117150509362?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2FhXzA2MTg=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)]
다음은 프로젝트 코드 항목 파일 구조 직접 보기
다음은 각 유형의 기능 을 소개 한다.
/**
* Game
*
* @sign Created by wang.ao on 2017 1 12
*/
@SuppressLint("DrawAllocation")
public class TetrisViewAW extends View {
/** , */
public static final int beginPoint = 10;
/** */
private static int max_x, max_y;
/** */
private static int num_x = 0, num_y = 0;
/** */
private static Paint paintWall = null;
/** */
private static Paint paintBlock = null;
private static final int BOUND_WIDTH_OF_WALL = 2;
/** */
private List blockUnits = new ArrayList();
/** */
private List blockUnitBufs = new ArrayList();
/** */
private List routeBlockUnitBufs = new ArrayList();
/** allBlockUnits */
private List allBlockUnits = new ArrayList();
/** Activity */
private TetrisActivityAW father = null;
private int[] map = new int[100]; //
/** */
private Thread mainThread = null;
//
/** */
private boolean gameStatus = false;
/** */
private boolean runningStatus = false;
/** */
private static final int color[] = { Color.parseColor("#FF6600"), Color.BLUE, Color.RED, Color.GREEN, Color.GRAY };
/** , */
private int xx, yy;
/** , */
private TetrisBlock tetrisBlock;
/** */
private int score = 0;
/** */
private int blockType = 0;
public TetrisViewAW(Context context) {
this(context, null);
}
public TetrisViewAW(Context context, AttributeSet attrs) {
super(context, attrs);
if (paintWall == null) {//
paintWall = new Paint();
paintWall.setColor(Color.LTGRAY);
paintWall.setStyle(Paint.Style.STROKE);
paintWall.setStrokeWidth(BOUND_WIDTH_OF_WALL + 1);
}
if (paintBlock == null) {//
paintBlock = new Paint();
paintBlock.setColor(Color.parseColor("#FF6600"));
}
tetrisBlock = new TetrisBlock();
routeBlockUnitBufs = tetrisBlock.getUnits(beginPoint, beginPoint);
Arrays.fill(map, 0); // 0
//
}
/**
* activity
*
* @param tetrisActivityAW
*/
public void setFather(TetrisActivityAW tetrisActivityAW) {
father = tetrisActivityAW;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
max_x = getWidth();
max_y = getHeight();
RectF rel;
//
num_x = 0;
num_y = 0;
for (int i = beginPoint; i < max_x - BlockUnit.UNIT_SIZE; i += BlockUnit.UNIT_SIZE) {
for (int j = beginPoint; j < max_y - BlockUnit.UNIT_SIZE; j += BlockUnit.UNIT_SIZE) {
rel = new RectF(i, j, i + BlockUnit.UNIT_SIZE, j + BlockUnit.UNIT_SIZE);
canvas.drawRoundRect(rel, 8, 8, paintWall);
num_y++;
}
num_x++;
}
//
int len = blockUnits.size();
//
// Toast.makeText(context, "" + len, Toast.LENGTH_SHORT).show();
for (int i = 0; i < len; i++) {
int x = blockUnits.get(i).x;
int y = blockUnits.get(i).y;
//
paintBlock.setColor(color[blockUnits.get(i).color]);
rel = new RectF(x + BOUND_WIDTH_OF_WALL, y + BOUND_WIDTH_OF_WALL,
x + BlockUnit.UNIT_SIZE - BOUND_WIDTH_OF_WALL, y + BlockUnit.UNIT_SIZE - BOUND_WIDTH_OF_WALL);
canvas.drawRoundRect(rel, 8, 8, paintBlock);
}
//
len = allBlockUnits.size();
//
// Toast.makeText(context, "" + len, Toast.LENGTH_SHORT).show();
for (int i = 0; i < len; i++) {
int x = allBlockUnits.get(i).x;
int y = allBlockUnits.get(i).y;
paintBlock.setColor(color[allBlockUnits.get(i).color]);
rel = new RectF(x + BOUND_WIDTH_OF_WALL, y + BOUND_WIDTH_OF_WALL,
x + BlockUnit.UNIT_SIZE - BOUND_WIDTH_OF_WALL, y + BlockUnit.UNIT_SIZE - BOUND_WIDTH_OF_WALL);
canvas.drawRoundRect(rel, 8, 8, paintBlock);
}
}
/**
*
*/
public void startGame() {
gameStatus = true;
runningStatus = true;
if (mainThread == null || !mainThread.isAlive()) {
getNewBlock();
mainThread = new Thread(new MainThread());
mainThread.start();
}
}
/**
*
*/
public void pauseGame() {
runningStatus = false;
}
/**
*
*/
public void continueGame() {
runningStatus = true;
}
/**
*
*/
public void stopGame() {
// ,
runningStatus = false;
gameStatus = false;
mainThread.interrupt();
blockUnits.clear();
allBlockUnits.clear();
score = 0;
invalidate();
}
/**
*
*/
public void toLeft() {
if (BlockUnit.toLeft(blockUnits, max_x, allBlockUnits)) {
xx = xx - BlockUnit.UNIT_SIZE;
}
invalidate();
}
/**
*
*/
public void toRight() {
if (BlockUnit.toRight(blockUnits, max_x, allBlockUnits)) {
xx = xx + BlockUnit.UNIT_SIZE;
}
invalidate();
}
/**
*
*/
public void route() {
if (blockType == 3) {// ,
return;
}
if (routeBlockUnitBufs.size() != blockUnits.size()) {
routeBlockUnitBufs = tetrisBlock.getUnits(xx, yy);
}
for (int i = 0; i < blockUnits.size(); i++) {
routeBlockUnitBufs.get(i).x = blockUnits.get(i).x;
routeBlockUnitBufs.get(i).y = blockUnits.get(i).y;
}
for (BlockUnit blockUnit : routeBlockUnitBufs) {
int tx = blockUnit.x;
int ty = blockUnit.y;
blockUnit.x = -(ty - yy) + xx;
blockUnit.y = tx - xx + yy;
}
routeTran(routeBlockUnitBufs);
if (!BlockUnit.canRoute(routeBlockUnitBufs, allBlockUnits)) {
// Toast.makeText(father, " ", Toast.LENGTH_SHORT).show();
return;
}
for (BlockUnit blockUnit : blockUnits) {
int tx = blockUnit.x;
int ty = blockUnit.y;
blockUnit.x = -(ty - yy) + xx;
blockUnit.y = tx - xx + yy;
}
routeTran(blockUnits);
invalidate();
}
/**
* , , , , ,
* , ,
*/
public void routeTran(List blockUnitsBuf) {
boolean needLeftTran = false;
boolean needRightTran = false;
for (BlockUnit u : blockUnitsBuf) {
if (u.x < beginPoint) {
needLeftTran = true;
}
if (u.x > max_x - BlockUnit.UNIT_SIZE) {
needRightTran = true;
}
}
if (needLeftTran || needRightTran) {
for (BlockUnit u : blockUnitsBuf) {
if (needLeftTran) {
u.x = u.x + BlockUnit.UNIT_SIZE;
} else if (needRightTran) {
u.x = u.x - BlockUnit.UNIT_SIZE;
}
}
routeTran(blockUnitsBuf);
} else {
return;
}
}
/**
*
*/
private void getNewBlock() {
// ,x x ,y
this.xx = beginPoint + (num_x / 2) * BlockUnit.UNIT_SIZE;
this.yy = beginPoint;
if (blockUnitBufs.size() == 0) {
// ,
blockUnitBufs = tetrisBlock.getUnits(xx, yy);
}
blockUnits = blockUnitBufs;
blockType = tetrisBlock.blockType;
blockUnitBufs = tetrisBlock.getUnits(xx, yy);
if (father != null) {//
father.setNextBlockView(blockUnitBufs, (num_x / 2) * BlockUnit.UNIT_SIZE);
}
}
/**
*
*
* @sign Created by wang.ao on 2017 1 16
*/
private class MainThread implements Runnable {
@Override
public void run() {
while (gameStatus) {
while (runningStatus) {
if (BlockUnit.canMoveToDown(blockUnits, max_y, allBlockUnits)) {
// , ,
BlockUnit.toDown(blockUnits, max_y, allBlockUnits);
yy = yy + BlockUnit.UNIT_SIZE;
} else {
/**
* , allBlockUnits ,
* ,
*/
for (BlockUnit blockUnit : blockUnits) {
blockUnit.y = blockUnit.y + BlockUnit.UNIT_SIZE;
allBlockUnits.add(blockUnit);
}
for (BlockUnit u : blockUnits) {
// map,
int index = (int) ((u.y - beginPoint) / 50); //
map[index]++;
}
//
int end = (int) ((max_y - 50 - beginPoint) / BlockUnit.UNIT_SIZE);
int full = (int) ((max_x - 50 - beginPoint) / BlockUnit.UNIT_SIZE) + 1;
try {
Thread.sleep(GameConfig.SPEED);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i <= end; i++) {
/***
* ( , , )
* , ,
*/
if (map[i] >= full) {
BlockUnit.remove(allBlockUnits, i);
score += 100;
map[i] = 0;
for (int j = i; j > 0; j--)
map[j] = map[j - 1];
map[0] = 0;
for (BlockUnit blockUnit : allBlockUnits) {
if (blockUnit.y < (i * BlockUnit.UNIT_SIZE + beginPoint)) {
blockUnit.y = blockUnit.y + BlockUnit.UNIT_SIZE;
}
}
}
}
father.runOnUiThread(new Runnable() {
@Override
public void run() {
/**
*
*/
father.score.setText("" + score);
invalidate();
}
});
try {
Thread.sleep(GameConfig.SPEED * 3);
} catch (InterruptedException e) {
e.printStackTrace();
}
father.runOnUiThread(new Runnable() {
@Override
public void run() {
getNewBlock();
score += 10;
father.score.setText("" + score);
}
});
}
father.runOnUiThread(new Runnable() {
@Override
public void run() {
invalidate();
}
});
try {
Thread.sleep(GameConfig.SPEED);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
/**
*
*
* @sign Created by wang.ao on 2017 1 13
*/
public class BlockUnit {
public static final int UNIT_SIZE = 50;
public static final int BEGIN = 10;
public int color;
//
public int x, y;
public BlockUnit() {
}
public BlockUnit(int x, int y, int color) {
/*
* @param
*/
this.x = x;
this.y = y;
this.color = color;
}
/**
* ,1 ,2
* @param blockUnits
* @param max_x X ,
* @param allBlockUnits
* @return true; false
*/
public static boolean canMoveToLeft(List blockUnits, int max_x, List allBlockUnits) {
for (BlockUnit blockUnit : blockUnits) {
int x = blockUnit.x;
if (x - UNIT_SIZE < BEGIN) {
return false;
}
int y = blockUnit.y;
if (isSameUnit(x - UNIT_SIZE, y, allBlockUnits)) {
return false;
}
}
return true;
}
/**
* ,1 ,2
* @param blockUnits
* @param max_x X ,
* @param allBlockUnits
* @return true; false
*/
public static boolean canMoveToRight(List blockUnits, int max_x, List allBlockUnits) {
for (BlockUnit blockUnit : blockUnits) {
int x = blockUnit.x;
if (x + UNIT_SIZE > max_x - UNIT_SIZE) {
return false;
}
int y = blockUnit.y;
if (isSameUnit(x + UNIT_SIZE, y, allBlockUnits)) {
return false;
}
}
return true;
}
/**
* ,1 ,2
* @param blockUnits
* @param max_x X ,
* @param allBlockUnits
* @return true; false
*/
public static boolean canMoveToDown(List blockUnits, int max_y, List allBlockUnits) {
for (BlockUnit blockUnit : blockUnits) {
int x = blockUnit.x;
int y = blockUnit.y + UNIT_SIZE * 2;
if (y > max_y - UNIT_SIZE) {
return false;
}
if (isSameUnit(x, y, allBlockUnits)) {
return false;
}
}
return true;
}
public static boolean canRoute(List blockUnits, List allBlockUnits){
for (BlockUnit blockUnit: blockUnits) {
if(isSameUnit(blockUnit.x, blockUnit.y, allBlockUnits)){
return false;
}
}
return true;
}
/**
*
* @param blockUnits
* @param max_x
* @param allBlockUnits
* @return , :true, :false
*/
public static boolean toLeft(List blockUnits, int max_x, List allBlockUnits) {
if (canMoveToLeft(blockUnits, max_x, allBlockUnits)) {
for (BlockUnit blockUnit : blockUnits) {
blockUnit.x = blockUnit.x - UNIT_SIZE;
}
return true;
}
return false;
}
/**
*
* @param blockUnits
* @param max_x
* @param allBlockUnits
* @return , :true, :false
*/
public static boolean toRight(List blockUnits, int max_x, List allBlockUnits) {
if (canMoveToRight(blockUnits, max_x, allBlockUnits)) {
for (BlockUnit blockUnit : blockUnits) {
blockUnit.x = blockUnit.x + UNIT_SIZE;
}
return true;
}
return false;
}
/**
*
* @param blockUnits
* @param allBlockUnits
* @return , :true, :false
*/
public static void toDown(List blockUnits, int max_Y, List allBlockUnits) {
for (BlockUnit blockUnit : blockUnits) {
blockUnit.y = blockUnit.y + BlockUnit.UNIT_SIZE;
}
}
/**
*
* @param x
* @param y
* @param allBlockUnits
* @return
*/
public static boolean isSameUnit(int x, int y, List allBlockUnits) {
for (BlockUnit blockUnit : allBlockUnits) {
if (Math.abs(x - blockUnit.x) < UNIT_SIZE && Math.abs(y - blockUnit.y) < UNIT_SIZE) {
return true;
}
}
return false;
}
/**
* j
* @param allBlockUnits
* @param j
*/
public static void remove(List allBlockUnits, int j) {
for (int i = allBlockUnits.size() - 1; i >= 0; i--) {
/*
* ① ② y , j units
*/
if ((int) ((allBlockUnits.get(i).y - BEGIN) / 50) == j)
allBlockUnits.remove(i);
}
}
}
/**
*
*
* @sign Created by wang.ao on 2017 1 13
*/
public class TetrisBlock {
private static final int TYPE_SUM = 7;
public int blockType, blockDirection; // ,
private int color; //
private int x, y; //
public TetrisBlock() {
}
public TetrisBlock(int x, int y) {
this.x = x;
this.y = y;
}
public List getUnits(int x, int y) {
this.x = x;
this.y = y;
return returnUnit();
}
/**
*
* @return
*/
public List returnUnit() {
List units = new ArrayList(); //
blockType = (int) (Math.random() * TYPE_SUM) + 1; //
blockDirection = 1; //
color = (int) (Math.random() * 4) + 1; //
units.clear();
switch (blockType) {
case 1://
for (int i = 0; i < 4; i++) {
units.add(new BlockUnit(x + (-2 + i) * BlockUnit.UNIT_SIZE, y, color));
}
break;
case 2:
units.add(new BlockUnit(x + (-1 + 1) * BlockUnit.UNIT_SIZE, y - BlockUnit.UNIT_SIZE, color));
for (int i = 0; i < 3; i++) {
units.add(new BlockUnit(x + (-1 + i) * BlockUnit.UNIT_SIZE, y, color));
}
break;
case 3:
for (int i = 0; i < 2; i++) {
units.add(new BlockUnit(x + (i - 1) * BlockUnit.UNIT_SIZE, y - BlockUnit.UNIT_SIZE, color));
units.add(new BlockUnit(x + (i - 1) * BlockUnit.UNIT_SIZE, y, color));
}
break;
case 4:
units.add(new BlockUnit(x + (-1 + 0) * BlockUnit.UNIT_SIZE, y - BlockUnit.UNIT_SIZE, color));
for (int i = 0; i < 3; i++) {
units.add(new BlockUnit(x + (-1 + i) * BlockUnit.UNIT_SIZE, y, color));
}
break;
case 5:
units.add(new BlockUnit(x + (-1 + 2) * BlockUnit.UNIT_SIZE, y - BlockUnit.UNIT_SIZE, color));
for (int i = 0; i < 3; i++) {
units.add(new BlockUnit(x + (-1 + i) * BlockUnit.UNIT_SIZE, y, color));
}
break;
case 6:
for (int i = 0; i < 2; i++) {
units.add(new BlockUnit(x + (-1 + i) * BlockUnit.UNIT_SIZE, y - BlockUnit.UNIT_SIZE, color));
units.add(new BlockUnit(x + i * BlockUnit.UNIT_SIZE, y, color));
}
break;
case 7:
for (int i = 0; i < 2; i++) {
units.add(new BlockUnit(x + i * BlockUnit.UNIT_SIZE, y - BlockUnit.UNIT_SIZE, color));
units.add(new BlockUnit(x + (-1 + i) * BlockUnit.UNIT_SIZE, y, color));
}
break;
}
return units;
}
/**
*
*
* @sign Created by wang.ao on 2017 1 13
*/
@SuppressLint("DrawAllocation")
public class NextBlockView extends View {
/** , */
public static final int beginPoint = 10;
/** */
private static int max_x, max_y;
private List blockUnits = new ArrayList();
/** */
private static Paint paintWall = null;
private static final int BOUND_WIDTH_OF_WALL = 2;
private static Paint paintBlock = null;
private int div_x = 0;
//
private static final int color[] ={ Color.parseColor("#FF6600"), Color.BLUE, Color.RED, Color.GREEN, Color.GRAY };
public NextBlockView(Context context) {
this(context, null);
}
public NextBlockView(Context context, AttributeSet attrs) {
super(context, attrs);
if (paintWall == null) {//
paintWall = new Paint();
paintWall.setColor(Color.LTGRAY);
paintWall.setStyle(Paint.Style.STROKE);
paintWall.setStrokeWidth(BOUND_WIDTH_OF_WALL + 1);
}
if (paintBlock == null) {//
paintBlock = new Paint();
paintBlock.setColor(Color.parseColor("#FF6600"));
}
}
public void setBlockUnits(List blockUnits, int div_x) {
this.blockUnits = blockUnits;
this.div_x = div_x;
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
max_x = getWidth();
max_y = getHeight();
RectF rel;
//
int len = blockUnits.size();
//
// Toast.makeText(context, "" + len, Toast.LENGTH_SHORT).show();
for (int i = 0; i < len; i++) {
paintBlock.setColor(color[blockUnits.get(i).color]);
int x = blockUnits.get(i).x - div_x + BlockUnit.UNIT_SIZE * 2;
int y = blockUnits.get(i).y + BlockUnit.UNIT_SIZE * 2;
rel = new RectF(x + BOUND_WIDTH_OF_WALL, y + BOUND_WIDTH_OF_WALL,
x + BlockUnit.UNIT_SIZE - BOUND_WIDTH_OF_WALL, y + BlockUnit.UNIT_SIZE - BOUND_WIDTH_OF_WALL);
canvas.drawRoundRect(rel, 8, 8, paintBlock);
rel = new RectF(x, y, x + BlockUnit.UNIT_SIZE, y + BlockUnit.UNIT_SIZE);
canvas.drawRoundRect(rel, 8, 8, paintWall);
}
}
}
public class GameConfig {
/** */
public static final int SPEED = 300;
}
public class TetrisActivityAW extends Activity {
private NextBlockView nextBlockView;
private TetrisViewAW tetrisViewAW;
private TextView gameStatusTip;
public TextView score;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tetris_activity_aw);
nextBlockView = (NextBlockView) findViewById(R.id.nextBlockView1);
tetrisViewAW = (TetrisViewAW) findViewById(R.id.tetrisViewAW1);
tetrisViewAW.setFather(this);
gameStatusTip = (TextView) findViewById(R.id.game_staus_tip);
score = (TextView) findViewById(R.id.score);
}
public void setNextBlockView(List blockUnits, int div_x) {
nextBlockView.setBlockUnits(blockUnits, div_x);
}
/**
*
*
* @param view
*/
public void startGame(View view) {
tetrisViewAW.startGame();
gameStatusTip.setText(" ");
}
/**
*
*/
public void pauseGame(View view) {
tetrisViewAW.pauseGame();
gameStatusTip.setText(" ");
}
/**
*
*/
public void continueGame(View view) {
tetrisViewAW.continueGame();
gameStatusTip.setText(" ");
}
/**
*
*/
public void stopGame(View view) {
tetrisViewAW.stopGame();
score.setText(""+0);
gameStatusTip.setText(" ");
}
/**
*
*/
public void toLeft(View view) {
tetrisViewAW.toLeft();
}
/**
*
*/
public void toRight(View view) {
tetrisViewAW.toRight();
}
/**
*
*/
public void toRoute(View view) {
tetrisViewAW.route();
}
}
전체 항목 이 바로 이것들 이 고 코드 는 이미 전부 붙 여 졌 다.전체 항목 을 쓸 때 는 매우 간단 하 다 고 생각 했 지만 많은 문제 에 부 딪 혔 지만 이미 해결 되 었 다.bug 를 찾 아 오신 것 을 환영 합 니 다. 모두 함께 발전 합 니 다.
원본 다운로드 주소
http://download.csdn.net/download/waa_0618/9741351