Example #1
0
        /// <summary>
        ///     Start a new game of Snake
        ///     If this new game is the first, then the Snake is initially not moving
        /// </summary>
        /// <param name="startingGame">Is this the first game of snake that has been played</param>
        private void NewGame(bool startingGame)
        {
            // If the new game is the first that has been started, initially the snake is not moving
            if (startingGame)
            {
                snake.MovingDirection = Direction.NotMoving;
            }
            else
            {
                // Otherwise the Snake is moving right and the game has started
                snake.MovingDirection = Direction.Right;
                if (!gameStarted)
                    gameStarted = true;
            }

            // Reset the Snake to prepare for a new game
            snake.Reset();

            // Find a location for the food
            food = GetRandomSector();

            // Reset the values of all fields to start a new game
            turnQueue.Clear();
            score = 0;
            gamePaused = false;
            gameEnded = false;
        }
Example #2
0
        /// <summary>
        ///     Initialise the grid of sectors representing the current game area
        /// </summary>
        /// <param name="gameGrid">The list of Sectors to populate</param>
        private void InitGrid(ICollection<Sector> gameGrid)
        {
            int x = gamePanel.Width - SIZE;
            int y = gamePanel.Height - SIZE;

            for (int i = 0; i <= x; i += SIZE)
            {
                for (int j = 0; j <= y; j += SIZE)
                {
                    var gridSector = new Sector(i, j, SIZE, SIZE);
                    gameGrid.Add(gridSector);
                }
            }
        }
Example #3
0
 /// <summary>
 ///     Determines if two Sector objects can be considered equal.
 ///     Two Sectors are equal if both the x and y coordinates are the same
 /// </summary>
 /// <param name="a">The first Sector to test</param>
 /// <param name="b">The second Sector to test</param>
 /// <returns>True if both Sectors are equal, false otherwise</returns>
 public static bool Equals(Sector a, Sector b)
 {
     return a.X == b.X && a.Y == b.Y;
 }
Example #4
0
        /// <summary>
        ///     Method fired when the timer has ticked
        ///     Updates the Snake and checks if the food is hit or if the game is over
        /// </summary>
        /// <param name="sender">Sender of the event</param>
        /// <param name="e">Information about the event</param>
        private void GameTimerTick(object sender, EventArgs e)
        {
            // Only apply game logic when the game is not paused or ended
            if (!gamePaused && !gameEnded)
            {
                // Apply the most recent queued Direction
                if (turnQueue.Count >= 1)
                {
                    // If the new direction is compatible, change the moving direction of the snake
                    if (IsCompatible(turnQueue[0]))
                    {
                        snake.MovingDirection = turnQueue[0];
                    }

                    // Remove the queued Direction from the list
                    turnQueue.RemoveAt(0);
                }

                // Move the Snake
                snake.Move();

                // If the Snake has hit the food
                if (HasHitFood())
                {
                    // Relocate the food, make the Snake grow by 5 segments and increase the score
                    food = GetRandomSector();
                    snake.Grow(5);
                    score += 10;
                    PlaySound(SoundToPlay.AteFood);
                }

                // If the game is over and the snake is moving then the game has ended
                if (IsGameOver() && snake.MovingDirection != Direction.NotMoving)
                {
                    gameEnded = true;
                    PlaySound(SoundToPlay.GameOver);
                }
            }

            // Paint the current game state onto the PictureBox by firing the Paint event
            gamePanel.Invalidate();
        }
Example #5
0
 /// <summary>
 ///     Construct a new Sector object from an existing Sector
 /// </summary>
 /// <param name="sector">The existing Sector object</param>
 public Sector(Sector sector)
 {
     rect = sector.rect;
 }