/// <summary> /// Main game function, performs game logic /// </summary> public void Update() { if (_currentscore >= MaxScore) { _timeBetweenFrames = _level >= 4 ? _timeBetweenFrames.Add(TimeSpan.FromMilliseconds(-35)) : _timeBetweenFrames.Add(TimeSpan.FromMilliseconds(-100)); _currentscore = 0; _level += 1; GameProgressChanged?.Invoke(this, new GameProgressChangedEventArgs(_level, LevelProgress)); Thread.Sleep(TimeSpan.FromMilliseconds((int)(TimeBeforeGameStart * 0.52))); ResetScreen(); Thread.Sleep(TimeSpan.FromMilliseconds((int)(TimeBeforeGameStart * 0.37))); return; } if (_newShapeNeeded) { _shape.Location = new Vector2(3, 0); _shape.ShapeOnMatrix = _futureShapeOnMatrix; _shape.Color = _futureColor; _shape.UpdateActiveShape(); if (_shape.ActiveShape.Any(vec => GetBrickAtPosition(vec.X, vec.Y).Color != EmptyCellColor)) { _isGameRunning = false; _level = 1; _currentscore = 0; GameProgressChanged?.Invoke(this, new GameProgressChangedEventArgs(_level, LevelProgress)); _timeBetweenFrames = TimeSpan.FromMilliseconds(DefaultTime); return; } ColorShape(); SetupFutureShape(); _futureShapeOnMatrix.ResetShape(); var preview = _futureShapeOnMatrix.GetListForPreview(); for (int i = 0; i < preview.Count; i++) { _nextShape[i].Color = preview[i] ? _futureColor : EmptyCellColor; } _newShapeNeeded = false; return; } MoveShape(Direction.Bottom); }
/// <summary> /// Performs row clear if it is needed /// </summary> /// <param name="y">Row to check</param> private void CheckClearGridRow(int y) { if (_currentscore >= 3) { return; } int start = y == _height - 1 ? 2 : 0; int end = y == _height - 1 ? _width - 2 : _width; for (int i = start; i < end; i++) { if (GetBrickAtPosition(i, y).Color == EmptyCellColor) { return; } } int half = _width / 2; int numOfColorsInGradient = 10; Color[][] colorTab = new Color[_width][]; for (int i = start; i < end; i++) { colorTab[i] = GenerateGradient(GetBrickAtPosition(i, y).Color, SparkColor, numOfColorsInGradient - 2); } for (int j = 0; j < numOfColorsInGradient; j++) { for (int i = start; i < end; i++) { GetBrickAtPosition(i, y).Color = colorTab[i][j]; } Thread.Sleep(50); } for (int i = start; i < end; i++) { colorTab[i] = GenerateGradient(SparkColor, EmptyCellColor, numOfColorsInGradient - 2); } for (int j = 0; j < numOfColorsInGradient; j++) { for (int i = start; i < end; i++) { GetBrickAtPosition(i, y).Color = colorTab[i][j]; } Thread.Sleep(35); } for (int j = y; j > 0; j--) { for (int i = start; i < end; i++) { GetBrickAtPosition(i, j).Color = GetBrickAtPosition(i, j - 1).Color; } } _currentscore += 1; GameProgressChanged?.Invoke(this, new GameProgressChangedEventArgs(_level, LevelProgress)); if (y == _height - 1) { CheckClearGridRow(_height - 1); } }