Exemplo n.º 1
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (snake.Head.data.HitBox.IntersectsWith(food.HitBox))
            {
                x = rand.Next(30, canvas.Width - 30);
                y = rand.Next(30, canvas.Height - 30);
                if (x % 30 > 0)
                {
                    while (x % 30 > 0)
                    {
                        x = rand.Next(30, canvas.Width - 30);
                    }
                }
                if (y % 30 > 0)
                {
                    while (y % 30 > 0)
                    {
                        y = rand.Next(30, canvas.Height - 30);
                    }
                }
                food.X = x;
                food.Y = y;
                Snake Tail = snake.Tail.data;
                if (currentDirection == direction.left)
                {
                    snakebody = new Snake(Tail.X + Tail.W, Tail.Y, 30, 30, 10, 3);
                }
                else if (currentDirection == direction.right)
                {
                    snakebody = new Snake(Tail.X - Tail.W, Tail.Y, 30, 30, 10, 4);
                }
                else if (currentDirection == direction.up)
                {
                    snakebody = new Snake(Tail.X, Tail.Y - Tail.W, 30, 30, 10, 1);
                }
                else if (currentDirection == direction.down)
                {
                    snakebody = new Snake(Tail.X, Tail.Y + Tail.W, 30, 30, 10, 2);
                }
                snake.AddNodeToLast(snakebody);
                score      += 10;
                label1.Text = $"Score: {score}";
            }
            g.FillRectangle(Brushes.DimGray, 0, 0, canvas.Width, canvas.Height);
            pictureBox1.Image = canvas;
            CircularLinkedListNode <Snake> runner = snake.Head;

            for (int i = 0; i < snake.Count; i++)
            {
                runner.data.Draw(g);
                runner = runner.next;
            }
            food.Draw(g);
            if (snake.Head.data.X < 0 || snake.Head.data.Y < 0 || snake.Head.data.X + snake.Head.data.W > canvas.Width || snake.Head.data.Y + snake.Head.data.H > canvas.Height)
            {
                timer1.Enabled = false;
                restart        = true;
                score          = 0;
                MessageBox.Show("Game Over.");
                MessageBox.Show("Press any button to restart.");
            }
            else
            {
                runner = snake.Head;
                if (currentDirection == direction.left)
                {
                    snake.Head.data.X -= snake.Head.data.S;
                }
                if (currentDirection == direction.right)
                {
                    snake.Head.data.X += snake.Head.data.S;
                }
                if (currentDirection == direction.up)
                {
                    snake.Head.data.Y -= snake.Head.data.S;
                }
                if (currentDirection == direction.down)
                {
                    snake.Head.data.Y += snake.Head.data.S;
                }
                for (int i = snake.Count - 1; i > 0; i--)
                {
                    if (i == 1)
                    {
                        runner.data.X = previousX;
                        runner.data.Y = previousY;
                    }
                    if (snake.Count > 1)
                    {
                        runner.data.X = runner.prev.data.X;
                        runner.data.Y = runner.prev.data.Y;
                    }
                    runner = runner.next;
                    if (snake.Head.data.HitBox.IntersectsWith(runner.data.HitBox) && runner.data != snake.Head.next.data)
                    {
                        timer1.Enabled = false;
                        restart        = true;
                        MessageBox.Show("Game Over.");
                        MessageBox.Show("Press any button to restart.");
                    }
                }
                previousX = snake.Head.data.X;
                previousY = snake.Head.data.Y;
            }
        }