Exemple #1
0
 /// <summary>
 /// Creates/Recreates the game and then starts it.
 /// </summary>
 private void NewGame()
 {
     this.Controls.Clear();
     this.gridSize       = new Size(this.ClientSize.Width / BaseBlock.StandardBlockSize.Width, this.ClientSize.Height / BaseBlock.StandardBlockSize.Height);
     this.snake          = new Snake(SnakeGame.DefaultSnakeLength);
     this.emptyPositions = SnakeUtility.GetEmptyBlockPositions(this.snake, this.gridSize);
     this.food           = new FoodBlock(this.emptyPositions);
     this.direction      = new Point(0, 1);
     this.state          = GameState.Playing;
     this.Invalidate();
     this.gameTimer.Enabled = true;
 }
Exemple #2
0
        /// <summary>
        /// Performs the main loop of game which is ran on every tick of the game timer.
        /// </summary>
        private void GameLoop()
        {
            if (this.direction.X == 0 && this.direction.Y == 0)
            {
                return;
            }

            if (SnakeUtility.WillEatFood(this.snake, this.food, this.direction))
            {
                this.snake.Grow();
                this.emptyPositions = SnakeUtility.GetEmptyBlockPositions(this.snake, this.gridSize);
                this.food           = new FoodBlock(this.emptyPositions);
            }

            this.snake.Move(this.direction);
            this.previousDirection = this.direction;

            if (SnakeUtility.HasCollidedWithSelf(this.snake) || SnakeUtility.HasHitBounds(this.snake, this.gridSize.Width, this.gridSize.Height) || this.emptyPositions.Count == 0)
            {
                this.GameOver();
            }
        }