Android实现俄罗斯方块

本文实例为大家分享了Android实现俄罗斯方块的具体代码,供大家参考,具体内容如下

思路:

  • 首先要画出游戏背景墙;
  • 其次,要有方块,以及方块单元;
  • 方块的不同形状,颜色随机产生;
  • 游戏的控制面板。

可能会出现的问题或者难点:

边界问题:

①处于边界的时候,方块不可以再左右移动;
②下降的时候,到达边界即底部,则不可继续下落,此时应该产生一个新的方块;

与其它方块接触问题:

①下落的时候,如果碰到其它的方块则停止下落;
②左右移动的时候,移动的过程中,如果接触到其他方快,则不可再继续左右移动;

方块的消除:

①调用方块消除方法的时间:当方块下落到底部的时候,判断是否有需要消除的行;
②消除某一行之后,应该把这一行上面的全部方块下移一行;

方块的旋转:

在当前项目中,我采用的是顺时针旋转。
①当旋转的时候,如果出现方块部分超出了边界,应该对方块进行平移,使其回到边界以内。(曾在网上看到有人做过,判断旋转之后是否会超出边界,如果会超出,则不进行旋转,我觉得不好,方块只要没有下落到底部,我觉得都可以进行旋转,除了没有空间让其旋转外);
②如果空间不足以旋转,也不可以旋转。空间不足以旋转的意思是:比如横向方向只有两个的空间,而方块旋转后会占用三个空间,此时也不可进行旋转;
③当无法继续下落或者下落到了底部也不可再进行旋转

控制面板:

①游戏开始、暂停、继续、结束,这些状态应该怎么去控制,以及游戏与控制台的事件关联。

未发现的问题:

因为本人能力,只做到这么多,如果有人发现问题,可以留言交流,欢迎挑问题。

游戏的运行界面如下所示,基本的功能以及操作很简单。

下面直接看项目代码
项目文件结构

下面分别介绍每个类的功能

TetrisViewAW.java游戏的主界面,背景墙以及方块都在此TetrisViewAW.Java里面,就是一个自定义的View ,(默认大家对于自定义View是熟悉的),在改类里面,有一个游戏主线程,用于控制游戏的开始,暂停,继续,停止,以及方块下落的速率。代码我加了很多注释,看不懂的可以留言。还有一点需要注意,当停止游戏时,要释放线程,养成好习惯

/**
 * 俄罗斯方块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<BlockUnit> blockUnits = new ArrayList<BlockUnit>();
 /** 下一个要显示的方块 */
 private List<BlockUnit> blockUnitBufs = new ArrayList<BlockUnit>();
 /** 下一个要显示的方块 */
 private List<BlockUnit> routeBlockUnitBufs = new ArrayList<BlockUnit>();
 /** 全部的方块allBlockUnits */
 private List<BlockUnit> allBlockUnits = new ArrayList<BlockUnit>();
 /** 调用此对象的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<BlockUnit> 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();
  }

 }
 }

 }

 }

}

BlockUnit.java方块的单元块,大家都玩过俄罗斯方块,每一个方块由四个单元块组成。单元快应该有以下属性:①大小:单元块的大小决定了主界面的容量(容纳单元块的数量);②颜色:每个单元块都有一个颜色,美化游戏界面(可无);③坐标:包括X轴坐标、Y轴坐标,在绘制方块的时候,以单元块的坐标为起点绘制,即:单元块的坐标值应该为单元块在界面上的左上角的坐标。

此类的主要功能有:方块的下落,左右移动,判断是否可以旋转等功能都在此类中,算是核心类。

/**
 * 俄罗斯方块的单元快
 *
 * @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<BlockUnit> blockUnits, int max_x, List<BlockUnit> 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<BlockUnit> blockUnits, int max_x, List<BlockUnit> 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<BlockUnit> blockUnits, int max_y, List<BlockUnit> 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<BlockUnit> blockUnits, List<BlockUnit> 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<BlockUnit> blockUnits, int max_x, List<BlockUnit> 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<BlockUnit> blockUnits, int max_x, List<BlockUnit> 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<BlockUnit> blockUnits, int max_Y, List<BlockUnit> 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<BlockUnit> 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<BlockUnit> 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);
 }
 }
}

TetrisBlock.java用于产生不同形状的方块,共有其中类型。

/**
 * 方块
 *
 * @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<BlockUnit> getUnits(int x, int y) {
 this.x = x;
 this.y = y;
 return returnUnit();
 }

 /**
 * 随机产生一种方块
 * @return
 */
 public List<BlockUnit> returnUnit() {
 List<BlockUnit> units = new ArrayList<BlockUnit>(); // 方块组成部分
 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;
 }

NextBlockView.java其实就是游戏主界面的一个缩减版,用于显示下一个要出现的方块的,玩家可以明确的知道下一个方块的形状和颜色。

/**
 * 下一个要展示的方块
 *
 * @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<BlockUnit> blockUnits = new ArrayList<BlockUnit>();
 /** 背景墙画笔 */
 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<BlockUnit> 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);
 }
 }

}

GameConfig.java用于配置方块的下落速度

public class GameConfig {
 /**方块下落的速度*/
 public static final int SPEED = 300;
}

TetrisActivityAW.java主界面,包括游戏主界面和控制台,很简单,直接贴代码。

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<BlockUnit> 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();
 }
}

TetrisActivityAW activity的xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="${relativePackage}.${activityClass}" >

 <com.awang.media.minetetris.TetrisViewAW
 android:id="@+id/tetrisViewAW1"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_marginBottom="200dp"
 android:layout_marginRight="120dp" />

 <com.awang.media.minetetris.NextBlockView
 android:id="@+id/nextBlockView1"
 android:layout_width="120dp"
 android:layout_height="120dp"
 android:layout_alignParentRight="true" />

 <LinearLayout
 android:layout_width="110dp"
 android:layout_height="wrap_content"
 android:layout_alignParentRight="true"
 android:layout_below="@+id/nextBlockView1"
 android:layout_marginTop="20dp"
 android:orientation="vertical" >

 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal" >

 <TextView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="分数" />

 <TextView
 android:id="@+id/score"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginLeft="10dp"
 android:text="1" />
 </LinearLayout>

 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_marginTop="20dp"
 android:orientation="horizontal" >

 <TextView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="等级" />

 <TextView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginLeft="10dp"
 android:text="1" />
 </LinearLayout>

 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_marginTop="20dp"
 android:orientation="horizontal" >

 <TextView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="速度" />

 <TextView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginLeft="10dp"
 android:text="0" />
 </LinearLayout>

 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_marginTop="20dp"
 android:orientation="horizontal" >

 <TextView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="最高分" />

 <TextView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginLeft="10dp"
 android:text="0" />
 </LinearLayout>

 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginTop="10dp"
 android:onClick="startGame"
 android:text="开始" />

 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:onClick="pauseGame"
 android:text="暂停" />

 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:onClick="continueGame"
 android:text="继续" />

 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:onClick="stopGame"
 android:text="结束" />
 </LinearLayout>

 <RelativeLayout
 android:layout_width="match_parent"
 android:layout_height="200dp"
 android:layout_alignParentBottom="true" >

 <Button
 android:layout_width="100dp"
 android:layout_height="100dp"
 android:onClick="toLeft"
 android:text="左" />

 <TextView
 android:id="@+id/game_staus_tip"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="5dp"
 android:text="点击开始运行游戏"
 android:textSize="20sp" />
 <Button
 android:layout_width="100dp"
 android:layout_height="100dp"
 android:layout_centerInParent="true"
 android:onClick="toRoute"
 android:text="旋转" />
 <Button
 android:layout_width="100dp"
 android:layout_height="100dp"
 android:layout_alignParentRight="true"
 android:onClick="toRight"
 android:text="右" />
 </RelativeLayout>

</RelativeLayout>

整个项目就是这些,代码已经全部贴出来了。

整个项目写的时候,以为很简单,但是却遇到了很多问题,不过都已解决。欢迎来找bug,大家共同进步。

源码下载地址:Android 俄罗斯方块与贪吃蛇源码下载

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Java 小游戏开发之俄罗斯方块

    Java项目 俄罗斯方块 一.心得 二.游戏实例 游戏截图 目录结构 三.代码 1.主界面 Tetris.java package com.fry.tetris; import java.util.Arrays; import java.util.Random; /** * 4格方块 */ public class Tetromino { protected Cell[] cells = new Cell[4]; /** 保存旋转的相对于轴位置状态 */ protected State[] st

  • java实现俄罗斯方块

    假期闲着无事,就用一周多的时间看了百度java吧的一位大神(alwing)发布的视频,学着用java写了一个俄罗斯方块,在此就以发布源代码以及必要讲解的形式来感谢他的帮助.当然我这里也是做了一些改动,做出来的程序界面以及功能没有和他的完全一样. 整个程序运行起来的界面如下所示: 程序包含的功能有: 俄罗斯方块本身基本的游戏元素 显示下一轮出现的方块.当前等级.分数.消行数等数值 以值槽形式显示当前等级到下一等级的距离 左上角头像以及背景图片会随着等级的升高而改变 循环播放背景音乐 一. 整个程序

  • 一款适用于Android平台的俄罗斯方块

    俄罗斯方块Tetris是一款很经典的益智游戏,之前就做了一款桌面版的java俄罗斯方块,这次就尝试着写了一款适用于Android平台的俄罗斯方块. 整个程序设计十分简洁,只是运用了两个类而已,最终做出的效果图如下所示: 首先,要考虑的自然是游戏应该如何布局的问题了.我的想法是将手机屏幕分为上下两部分,上边用来显示游戏者的名称.所得分数以及下一个方块,称为"文字区域",下边自然就是游戏区域了. 如图所示: 布局文件如下: <LinearLayout xmlns:android=&q

  • Java游戏俄罗斯方块的实现实例

    Java游戏俄罗斯方块的实现实例 java小游戏主要理解应用java Swing,awt等基础组件的知识,通过本例应当掌握面向对象的知识. 实现代码: package cn.hncu.games; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import

  • Java俄罗斯方块小游戏

    去年就已经学了这个技术了,一直没去写,现在抽个时间写了个俄罗斯方块游戏. 只有简单的新游戏,暂停,继续,积分功能.简单的实现了俄罗斯的经典功能. 不介绍了,有兴趣的自己运行一下,后面贴出了图片. 代码: package cn.hncu; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.Act

  • Android实现俄罗斯方块

    本文实例为大家分享了Android实现俄罗斯方块的具体代码,供大家参考,具体内容如下 思路: 首先要画出游戏背景墙: 其次,要有方块,以及方块单元: 方块的不同形状,颜色随机产生: 游戏的控制面板. 可能会出现的问题或者难点: 边界问题: ①处于边界的时候,方块不可以再左右移动: ②下降的时候,到达边界即底部,则不可继续下落,此时应该产生一个新的方块: 与其它方块接触问题: ①下落的时候,如果碰到其它的方块则停止下落: ②左右移动的时候,移动的过程中,如果接触到其他方快,则不可再继续左右移动:

  • Android开发之多线程中实现利用自定义控件绘制小球并完成小球自动下落功能实例

    本文实例讲述了Android开发之多线程中实现利用自定义控件绘制小球并完成小球自动下落功能的方法.分享给大家供大家参考,具体如下: 1.布局界面 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pare

  • Android实现扫雷小游戏

    本文实例为大家分享了Android实现扫雷小游戏的具体代码,供大家参考,具体内容如下 先看效果图: 初始游戏界面: 翻开块和标记块界面: 游戏结束界面: 菜单界面: 更换难度界面: 查看游戏记录界面: 代码分析 Block.java 这部分代码实现的是游戏界面的板块 设置四个变量来记录当前块是否被翻开,当前块是否是地雷,是否把当前快标记为地雷(也就是插旗子),当前块周围的地雷数量. 关键部分代码: //设置翻开状态 public void setNumberOfSurroundingMines(

  • 利用Jetpack Compose实现经典俄罗斯方块游戏

    目录 可组合函数 游戏机身 - TetrisBody 游戏按钮 - TetrisButton 游戏屏幕 - TetrisScreen 调度器 - TetrisViewModel 项目地址 你的童年是否有俄罗斯方块呢,本文就来介绍如何通过 Jetpack Compose 实现一个俄罗斯方块 ~~ 先看下效果图,功能还是挺完善的 就我自己的体验来说,使用 Compose 开发的应用我感受不到和 Android 原生开发之间有什么性能差异,但 Compose 在开发难度上会低很多 Google 官网上

  • Android辅助功能实现自动抢红包(附源码)

    一.描述 最近看到同事有用抢红包的软件,就想看看抢红包的具体实现是如何的,所以了解了一下,有用辅助功能实现的,所以在下面的示例中会展示一个抢红包的小Demo,附带源码抢红包源码. 二.效果图 在桌面收到红包进行抢 在聊天页面收到口令红包 三.AccessibilityService使用 创建辅助服务类,继承AccessibilityService,实现两个接口,接收系统的事件 public class MyService extends AccessibilityService { @Overr

  • 浅析Android Studio 3.0 升级各种坑(推荐)

    点击 Check for Updates 增量更新: 下载完成,会提示更新 您将 Gradle 更新至 4.1: 这里建议您对老项目先暂时点击 Don't remind me on this project,以防有坑.当然我不入地狱谁入地狱,我点 Update,于是问题来了,一直处于下载中,不过,莫担心,我下载好了,公众号聊天界面回复「 gradle-4.1-all 」,下载 gradle-4.1-all.zip 文件,放到: 重启 Android Studio. gradle 目录: Mac系

  • 使用AngularJS 应用访问 Android 手机的图片库

    Download angularjs.zip- 4.5 KB 介绍 这篇文章来说明如何使用AngularJs调用android Apps暴露的REST APIS来访问图像库. 背景 Android和IOS 有很多远程访问的app,但是开发者缺少远程访问手机特征的API.因此,myMoKit的开发是用来填补软件解决方案的缺陷的. 使用代码 使用代码是很简单的,你只要通过web URL 引用myMoKit 服务,你就可以看见所有暴露的REST API了 这些在手机里面的API列表和流媒体.通过Ang

  • Android开发实现模仿360二维码扫描功能实例详解

    本文实例讲述了Android开发实现模仿360二维码扫描功能的方法.分享给大家供大家参考,具体如下: 一.效果图: 二.框架搭建 1.首先,下载最新zxing开源项目. 下载地址:http://code.google.com/p/zxing/ 或 点击此处本站下载. 2.分析项目结构,明确扫描框架需求.在zxing中,有很多其他的功能,项目结构比较复杂:针对二维码QRCode扫描,我们需要几个包: (1)com.google.zxing.client.android.Camera 基于Camer

  • Android中控制和禁止ScrollView自动滑动到底部的方法

    一.Android 控制ScrollView滚动到底部 在开发中,我们经常需要更新列表,并将列表拉倒最底部,比如发表微博,聊天界面等等, 这里有两种办法,第一种,使用scrollTo(): public static void scrollToBottom(final View scroll, final View inner) { Handler mHandler = new Handler(); mHandler.post(new Runnable() { public void run()

随机推荐