Esempio n. 1
0
        /// <summary>
        /// Generate new location for food
        /// </summary>
        private Region GenerateNewFood()
        {
            Region regToRe = Food.GetRegion();

            if (foodQueue.Count > 1)
            {
                if (foodEated > 0)
                {
                    foodQueue.Enqueue(foodQueue.Dequeue());
                }
                ResetFoodEvents();
                RestartFoodEvents();
            }
            Point     newLocation = new Point();
            IFood     food        = (IFood)Food.Clone();
            Random    rnd         = new Random();
            Rectangle foodRect;

            do
            {
                newLocation.X = rnd.Next(borders.LeftBorder.Size.Width + borders.LeftBorder.Location.X, borders.RightBorder.Location.X);
                newLocation.Y = rnd.Next(borders.TopBorder.Size.Height + borders.TopBorder.Location.Y, borders.BottomBorder.Location.Y);
                food.Location = newLocation;
                foodRect      = new Rectangle(food.Location, food.Size);
            } while (snake.GetRegion().IsVisible(foodRect) || borderArea.IsVisible(foodRect));
            this.Food.Location = newLocation;
            return(regToRe);
        }