Esempio n. 1
0
        private void UpdateGame(Object source, System.Timers.ElapsedEventArgs e)
        {
            //System.Diagnostics.Debug.WriteLine(move);
            MyPanel panel = window.GetPanel();

            panel.Invalidate();

            Snake snake = panel.GetSnake();

            if (VerifyHit(snake, panel.GetApple()))
            {
                snake.IncreaseSnake();
                panel.GetApple().ChangePos();
                if (movement == 0)
                {
                    snake.ChangePosX(snake.SnakeLength() - 1, snake.GetPosX(snake.SnakeLength() - 2) - 10);
                    snake.ChangePosY(snake.SnakeLength() - 1, snake.GetPosY(snake.SnakeLength() - 2));
                }
                if (movement == 1)
                {
                    snake.ChangePosX(snake.SnakeLength() - 1, snake.GetPosX(snake.SnakeLength() - 2) + 10);
                    snake.ChangePosY(snake.SnakeLength() - 1, snake.GetPosY(snake.SnakeLength() - 2));
                }
                if (movement == 2)
                {
                    snake.ChangePosX(snake.SnakeLength() - 1, snake.GetPosX(snake.SnakeLength() - 2));
                    snake.ChangePosY(snake.SnakeLength() - 1, snake.GetPosY(snake.SnakeLength() - 2) - 10);
                }
                if (movement == 3)
                {
                    snake.ChangePosX(snake.SnakeLength() - 1, snake.GetPosX(snake.SnakeLength() - 2));
                    snake.ChangePosY(snake.SnakeLength() - 1, snake.GetPosY(snake.SnakeLength() - 2) + 10);
                }
            }

            for (int i = snake.SnakeLength() - 1; i > 0; i--)
            {
                if (snake.GetPosX(i) == snake.GetPosX(0) && snake.GetPosY(i) == snake.GetPosY(0))
                {
                    timer.Stop();
                    panel.Invalidate();
                    this.GameOver();
                }
                snake.ChangePosX(i, snake.GetPosX(i - 1));
                snake.ChangePosY(i, snake.GetPosY(i - 1));
            }
            if (movement == 0)
            {
                snake.DecreasePosY(0);
            }
            else if (movement == 1)
            {
                snake.IncreasePosY(0);
            }
            else if (movement == 2)
            {
                snake.DecreasePosX(0);
            }
            else
            {
                snake.IncreasePosX(0);
            }

            if (snake.GetPosX(0) == 600)
            {
                snake.ChangePosX(0, 0);
            }
            else if (snake.GetPosX(0) == 0)
            {
                snake.ChangePosX(0, 600);
            }
            else if (snake.GetPosY(0) == 600)
            {
                snake.ChangePosY(0, 0);
            }
            else if (snake.GetPosY(0) == 0)
            {
                snake.ChangePosY(0, 600);
            }
        }