Esempio n. 1
0
        void timer_Tick(object sender, EventArgs e)
        {
            //game over
            if (snake.IsDead() == true)
            {
                timer.Stop();
                timerSpec.Stop();
                MessageBox.Show("Your score is " + snake.score, "Elapsed time : " + string.Format("{0:00}:{1:00}", minutesElapsed, secondsElapsed));
                DialogResult result = MessageBox.Show("New game?", "You lost.", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    NewGame();
                }
                else
                {
                    this.Close();
                }
            }
            //elapsed time
            sec--;
            if (sec == 0)
            {
                sec             = 10;
                secondsElapsed += 1;
                if (secondsElapsed % 60 == 0)
                {
                    secondsElapsed  = 0;
                    minutesElapsed += 1;
                }
            }
            //snake movement and food status

            snake.Move();

            food.CheckIfEaten(snake);
            if (foodSpec != null)
            {
                foodSpec.CheckIfEaten(snake);
                if (foodSpec.isEaten == true)
                {
                    timerSpec.Stop();
                    snake.score                  += secondsLeft * 3;
                    secondsLeft                   = 10;
                    toolStripLabel3.Text          = secondsLeft.ToString();
                    foods[foodSpec.Y][foodSpec.X] = false;
                    foodSpec = null;
                }
            }
            if (food.isEaten == true)
            {
                snake.AddPart(bodyColor);
                food = new SnakeFood(SIZE);
                GenerateFood();
                snake.foodIsEaten = true;
            }
            Invalidate(true);
        }