示例#1
0
        public void SpawnFood()
        {
            foodTimer.Stop();

            //remove food
            if (CurrentFood != null)
            {
                if (Board[CurrentFood.Position.X, CurrentFood.Position.Y] == FoodService.GetBoardValue(CurrentFood.Id))
                {
                    Board[CurrentFood.Position.X, CurrentFood.Position.Y] = 0;
                    drawArea.DrawSquare(CurrentFood.Position, "Background");
                }
                CurrentFood = null;
            }

            //pick position
            Point randomPosition;

            do
            {
                randomPosition = new Point(random.Next(1, BoardSizeX - 2), random.Next(1, BoardSizeY - 2));
            } while (Board[randomPosition.X, randomPosition.Y] != 0); //we need an empty tile

            //spawn food, place it on board, draw it
            CurrentFood = FoodService.NewFood(FoodService.RandomId(), randomPosition);

            Board[CurrentFood.Position.X, CurrentFood.Position.Y] = FoodService.GetBoardValue(CurrentFood.Id);

            drawArea.DrawFood(CurrentFood);

            //set up timer
            foodTimer.Interval = CurrentFood.LifeSpan;
            foodTimer.Start();
        }