IEnumerator GameCoreRunner() { Init(); //Infinite Game Cycle while (isGaming) { //Spawn Player Shape playerShape = new PlayerShape(); playerShape.shapeCoordinates = TetrisShapes.GetShapeCoordinates(TetrisShapes.Shape.Random); playerShape.shapeCoordinates += new Coordinate(Mathf.RoundToInt(gameSize.x / 2), gameSize.y - 2); if (DetectIfCollideWithGameBoard(playerShape.shapeCoordinates)) { yield return(EditorCoroutineUtility.StartCoroutine(GameOverProcess(), ArcadeWindow.instance)); yield break; } double t = 0; while (playerShape != null) { t += deltaTime; if (t > FallFrequency) { t -= FallFrequency; t %= FallFrequency; FallDownPlayer(); } yield return(null); } BlockState[,] newBlockStates; bool isAnyMatchedLines = CheckMatch(out newBlockStates); gameBoard = newBlockStates; if (isAnyMatchedLines) { yield return(new EditorWaitForSeconds(1f)); ClearLine(); } } }
public TetrisShape(TetrisShapes shape) { List <Point> SomePoints = new List <Point>(); switch (shape) { case TetrisShapes.I: SomePoints = (PointsForI); color = Colors.Red; ShapeHeight = 4; break; case TetrisShapes.J: SomePoints = (PointsForJ); color = Colors.White; ShapeHeight = 3; break; case TetrisShapes.L: SomePoints = (PointsForL); color = Colors.Magenta; ShapeHeight = 3; break; case TetrisShapes.O: SomePoints = (PointsForO); color = Colors.Blue; ShapeHeight = 2; break; case TetrisShapes.S: SomePoints = (PointsForS); color = Colors.Green; ShapeHeight = 3; break; case TetrisShapes.T: SomePoints = (PointsForT); color = Colors.Brown; ShapeHeight = 3; break; case TetrisShapes.Z: SomePoints = (PointsForZ); color = Colors.Cyan; ShapeHeight = 3; break; default: SomePoints = (PointsForI); color = Colors.Red; break; } foreach (Point p in SomePoints) { System.Windows.Shapes.Rectangle e = new Rectangle(); e.Width = 16; e.Height = 16; e.Fill = new SolidColorBrush(color); e.SetValue(Canvas.LeftProperty, p.X); e.SetValue(Canvas.TopProperty, p.Y); _points.Add(new TetrisPoint(p, e)); } }
/// <summary> /// Generate a new random shape /// </summary> /// <param name="forPreview">Flag to check whether this shape is generated for preview</param> /// <returns>New instance of ITetrisShape class</returns> private ITetrisShape GenerateRandomShape(bool forPreview) { ITetrisShape shape = null; if (forPreview) { //Create a random side _isLeft = _random.Next() % 2 == 1; } if (forPreview) { //Create a random direction _currentDirection = (ShapeDirections)_random.Next(1, 5); } //Init location int colIdx = 17; int rowIdx = 2; if (!forPreview) { colIdx = _random.Next(2, Constants.MAX_WIDTH - 2); rowIdx = 6; } if (forPreview) { //Create a random shape _currentShapeModel = (TetrisShapes)_random.Next(1, 7); } //Check location of new shape before draw it. If it can't be drawn, game over if (!forPreview) { if (!CheckLocationOfNewShape(_currentShapeModel, _currentDirection, colIdx, rowIdx, _isLeft)) GameOver(); } switch (_currentShapeModel) { case TetrisShapes.TShape: shape = new TShape(sheet1, rowIdx, colIdx, _currentDirection); break; case TetrisShapes.SShape: shape = new SShape(sheet1, rowIdx, colIdx); break; case TetrisShapes.DotShape: shape = new DotShape(sheet1, rowIdx, colIdx); break; case TetrisShapes.LShape: shape = new LShape(sheet1, rowIdx, colIdx, _currentDirection, _isLeft); break; case TetrisShapes.IShape: shape = new IShape(sheet1, rowIdx, colIdx, _currentDirection); break; case TetrisShapes.ZShape: shape = new ZShape(sheet1, rowIdx, colIdx, _currentDirection, _isLeft); break; default: shape = new TShape(sheet1, rowIdx, colIdx, _currentDirection); break; } return shape; }
/// <summary> /// Check location of the new shape, if there's any obstacle, game over /// </summary> /// <param name="shape"></param> /// <param name="colIdx"></param> /// <param name="rowIdx"></param> private bool CheckLocationOfNewShape(TetrisShapes shape, ShapeDirections direction, int colIdx, int rowIdx, bool isLeft) { bool canDraw = true; Cell left = sheet1.Cells[rowIdx, colIdx - 1]; Cell center = sheet1.Cells[rowIdx, colIdx]; Cell right = sheet1.Cells[rowIdx, colIdx + 1]; Cell bottom = sheet1.Cells[rowIdx + 1, colIdx]; Cell bottomLeft = sheet1.Cells[rowIdx + 1, colIdx - 1]; Cell bottomRight = sheet1.Cells[rowIdx + 1, colIdx + 1]; if (center.CellType != null) { return false; } switch (shape) { case TetrisShapes.DotShape: //Already checked center break; case TetrisShapes.IShape: switch (direction) { case ShapeDirections.FaceDown: case ShapeDirections.FaceUp: //x //x //x if (left.CellType != null || right.CellType != null) canDraw = false; break; case ShapeDirections.FaceLeft: case ShapeDirections.FaceRight: //xxx if (bottom.CellType != null) canDraw = false; break; } break; case TetrisShapes.LShape: switch (direction) { case ShapeDirections.FaceDown: if (isLeft) { //xxx // x if (left.CellType != null || bottomRight.CellType != null) canDraw = false; } else { ///xxx ///x if (left.CellType != null || bottomLeft.CellType != null) canDraw = false; } break; case ShapeDirections.FaceUp: //x or x //xxx xxx if (left.CellType != null || right.CellType != null) canDraw = false; break; case ShapeDirections.FaceLeft: if (_isLeft) { // x // x //xx if (bottom.CellType != null || bottomLeft.CellType != null) canDraw = false; } else { //x //x //xx if (bottom.CellType != null || bottomRight.CellType != null) canDraw = false; } break; case ShapeDirections.FaceRight: if (_isLeft) { //xx //x //x if (bottom.CellType != null) canDraw = false; } else { //x //x //xx if (bottom.CellType != null || bottomRight.CellType != null) canDraw = false; } break; } break; case TetrisShapes.SShape: //xx //xx if (bottom.CellType != null || bottomRight.CellType != null) canDraw = false; break; case TetrisShapes.TShape: switch (direction) { case ShapeDirections.FaceDown: //xxx // x if (left.CellType != null || right.CellType != null || bottom.CellType != null) canDraw = false; break; case ShapeDirections.FaceUp: // x //xxx if (left.CellType != null || right.CellType != null) canDraw = false; break; case ShapeDirections.FaceLeft: // x //xx // x if (left.CellType != null || bottom.CellType != null) canDraw = false; break; case ShapeDirections.FaceRight: //x //xx //x if (right.CellType != null || bottom.CellType != null) canDraw = false; break; } break; case TetrisShapes.ZShape: switch (direction) { case ShapeDirections.FaceDown: if (_isLeft) { // //xx // xx if (left.CellType != null || bottom.CellType != null || bottomLeft.CellType != null) canDraw = false; } else { // // xx //xx if (bottomLeft.CellType != null || bottom.CellType != null || right.CellType != null) canDraw = false; } break; case ShapeDirections.FaceUp: if (_isLeft) { //xx // xx // if (right.CellType != null) canDraw = false; } else { // xx //xx // if (left.CellType != null) canDraw = false; } break; case ShapeDirections.FaceLeft: if (_isLeft) { // x //xx //x if (bottomLeft.CellType != null || left.CellType != null) canDraw = false; } else { //x //xx // x if (right.CellType != null || bottomRight.CellType != null) canDraw = false; } break; case ShapeDirections.FaceRight: if (_isLeft) { // x //xx //x if (bottom.CellType != null || right.CellType != null) canDraw = false; } else { //x //xx // x if (bottomRight.CellType != null || right.CellType != null) canDraw = false; } break; } break; } return canDraw; }