/// <summary> /// sets current tetronimo to next and picks a new one for the next tetronimo /// also checks if the current one fits and if not ends the game /// </summary> private void SpawnNewTetronimo() { _currentTetronimo = _currentTetronimo == null?GetRandomTetronimo() : _nextTetronimo; _nextTetronimo = GetRandomTetronimo(); PlaceCurrentTetronimo(); if (!_currentTetronimo.Fits()) // if it doesn't fit anymore the player has lost { lost = true; } }
/// <summary> /// Draw a tetronimo in a rect /// </summary> /// <param name="spriteBatch">the thing used to draw</param> /// <param name="tetronimo">the tetronimo to draw</param> /// <param name="rect">the rect in which to draw</param> private void DrawTetronimoInRect(SpriteBatch spriteBatch, Tetronimo tetronimo, Rectangle rect) { //draws the tetronimo centered in the grid tetronimo?.Draw(spriteBatch, _offset + (new Vector2(rect.X, rect.Y) + (new Vector2(rect.Width, rect.Height) - tetronimo.Size) / 2) * Tetronimo.BlockSize); }
/// <summary> /// handles the logic for the grid /// </summary> /// <param name="gameTime"></param> public void Update(GameTime gameTime) { if (lost) { return; } float deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds; _inputHelper.Update(gameTime); if (_movementTimer <= 0) { if (_inputHelper.KeyDown(_right)) { if (_currentTetronimo.Move(new Point(1, 0))) { _movementTimer = MovementTimerReset; } } else if (_inputHelper.KeyDown(_left)) { if (_currentTetronimo.Move(new Point(-1, 0))) { _movementTimer = MovementTimerReset; } } } else { _movementTimer -= deltaTime; } if (_gravityTimer <= 0) { if (!_currentTetronimo.Move(new Point(0, 1))) { PlaceInGrid(); } _gravityTimer = GravityTimerReset - GravityTimerSubtractPerLevel * Math.Min(MaxLevel, _clearedLines / LinesPerLevel); } else { _gravityTimer -= deltaTime * (_inputHelper.KeyDown(_down) ? DownPressGravityMultiplier : 1); } if (_inputHelper.KeyPressed(_rotate)) { _currentTetronimo.Rotate(true); } if (_inputHelper.KeyPressed(_place)) { while (_currentTetronimo.Move(new Point(0, 1))) { ; } PlaceInGrid(); } if (_inputHelper.KeyPressed(_hold) && !_hasHeld) { var temp = _holdTetronimo; _holdTetronimo = _currentTetronimo; _holdTetronimo.position = Point.Zero; if (temp == null) { SpawnNewTetronimo(); } else { _currentTetronimo = temp; PlaceCurrentTetronimo(); } _hasHeld = true; } }