/// <summary> /// Method <c>GameTimer_Tick()</c> /// Represents the that should happen every timer tick. /// Ex: the _shape should move down if it can, if not a new shape /// is generated at the top. /// Checks for game over condition and calls the method that clears full /// rows. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void GameTimer_Tick(object sender, EventArgs e) { _shape.MoveDown(); if (!_shape.ValidMoveDown() && !_gameOver) { _shape.MarkPosition(); _shape = _nextShape; _shape.CenterShape(); NextBlock.Clear(); _nextShape = _shapeFactory.BuildRandomShape(); NextBlock.AddRange(_nextShape.ShapeBlocks); Blocks.AddRange(_shape.ShapeBlocks); if (GameOverCondition()) { _gameOver = true; _timer.Stop(); GameOver gameOverWindow = new GameOver(); gameOverWindow.ShowDialog(); } CheckForPoints(); } }
/// <summary> /// Constructor <c> Tetris</c> instantiates tetris with all the default conditions /// </summary> public Tetris() { _shapeFactory = new ShapeFactory(GameBoard.Grid); Blocks = new ObservableCollection <BlockModel>(); NextBlock = new ObservableCollection <BlockModel>(); HeldBlock = new ObservableCollection <BlockModel>(); _timer = new DispatcherTimer(); _timer.Interval = new TimeSpan(0, 0, 0, 0, _timeInterval); _timer.Tick += GameTimer_Tick; _shape = _shapeFactory.BuildRandomShape(); _shape.CenterShape(); _nextShape = _shapeFactory.BuildRandomShape(); NextBlock.AddRange(_nextShape.ShapeBlocks); Blocks.AddRange(_shape.ShapeBlocks); Score = 0; Level = 1; _timer.Start(); }
/// <summary> /// Method <c>HoldBlock()</c> /// sets the heldBlock to the current block, and then sets the current block to /// the next block. /// </summary> public void HoldBlock() { if (HeldBlock.Count == 0) { _heldShape = _shapeFactory.BuildShape(_shape); for (int i = 0; i < 4; i++) { Blocks.Remove(x => x.GridX == _shape.ShapeBlocks[i].GridX && x.GridY == _shape.ShapeBlocks[i].GridY); } _shape = _nextShape; _shape.CenterShape(); Blocks.AddRange(_shape.ShapeBlocks); _nextShape = _shapeFactory.BuildRandomShape(); NextBlock.Clear(); NextBlock.AddRange(_nextShape.ShapeBlocks); HeldBlock.AddRange(_heldShape.ShapeBlocks); } else { HeldBlock.Clear(); var tempShape = _heldShape; _heldShape = _shapeFactory.BuildShape(_shape); for (int i = 0; i < 4; i++) { Blocks.Remove(x => x.GridX == _shape.ShapeBlocks[i].GridX && x.GridY == _shape.ShapeBlocks[i].GridY); } _shape = tempShape; _shape.CenterShape(); Blocks.AddRange(_shape.ShapeBlocks); HeldBlock.AddRange(_heldShape.ShapeBlocks); } }