黑马程序员 wp7,俄罗斯方块 探秘

来源:互联网 发布:java四种循环 编辑:程序博客网 时间:2024/06/05 00:20

  • ---------------------- Windows Phone 7手机开发 Net培训、期待与您交流! ----------------------
    俄罗斯方块是一款忒经典 的游戏,是一款风靡全球的游戏,因此是许多智能手机开发 者顶礼膜拜的app和学习的好素材。 下面我们就开始了这样一段俄罗斯方块探秘之旅 首先要创造相应的砖块 createPiece方法 /// /// 创建了piece (砖块)各种各样的款式 /// 这个的样式有各种各样的类型 如'田'字型,'N'类型,'T'类型等等 /// 而且我们使他起始位置是(0,3)坐标的位置 /// private void CreatePiece() { for (int x = 0; x < _columns; x++) { if (Container[0,x].Color!=null) { OnGameOver(null); break; } } Random random = new Random(); _currentPiece = _nextPiece == null ? _pieces[random.Next(0, 7)] : _nextPiece; _nextPiece = _pieces[random.Next(0, 7)]; _positionX = 3; _positionY = 0; SetNextContainerUI(); }为了使游戏更加的合理,我们需要一个判断砖块是否是出格的方法 /// /// 当x坐标的位置<0的位置 或者> 相应的宽度是边界的位置 /// 当y坐标的位置<0的位置 或者> 相应的高度是边界的位置 /// /// /// /// /// private bool IsBoundary(int[,] matrix, int offsetX, int offsetY) { RemovePiece(); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (matrix[i, j] == 1) { if (j + _positionX + offsetX > _columns - 1 || i + _positionY + offsetY > _rows - 1 || j + _positionX + offsetX < 0 || Container[i + _positionY + offsetY, j + _positionX + offsetX].Color != null) { AddPiece(0, 0); return true; } } } } AddPiece(0, 0); return false; }我们还需要一个能够 使他判断是否能够相消的方法 就是一行的砖块是否是排满的方法 /// /// 判断他同一行的砖块是不是颜色是不是一样 /// 如果一样 进行了相消 /// private void RemoveRow() { int removeRowCount = 0; for (int y = 0; y < _rows; y++) { bool isLine = true; for (int x = 0; x < _columns; x++) { if (Container[y, x].Color == null) { isLine = false; break; } } if (isLine) { removeRowCount++; for (int x = 0; x < _columns; x++) { Container[y, x].Color = null; } for (int i = y; i > 0; i--) { for (int x = 0; x < _columns; x++) { Container[i, x].Color = Container[i - 1, x].Color; } } } } if (removeRowCount > 0) { Score =Score+ 10 * (int)Math.Pow(2,removeRowCount); } RemoveRowCount = RemoveRowCount + removeRowCount; Level = (int)Math.Sqrt(RemoveRowCount/5); _timer.Interval = TimeSpan.FromMilliseconds(_initSpeed - _levelSpeed * Level > _levelSpeed ? _initSpeed - _levelSpeed * Level : _levelSpeed); }最终 我们写一个清空砖块的方法 /// /// 使每个每个的位置进行了清空 呵呵 /// public void Clear() { for (int x = 0; x < _columns; x++) { for (int y = 0; y < _rows; y++) { Container[y, x].Color = null; } } }

    ---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! ----------------------

    详细请查看:http://net.itheima.com/