Esempio n. 1
0
        private void pictureBoxArena_Paint(object sender, PaintEventArgs e)
        {
            Graphics canvas = e.Graphics;

            if (!Settings.GameOver)
            {
                //Draw snake
                for (SnakeNode currentNode = Snake.headNode; currentNode != null; currentNode = currentNode.next)
                {
                    Brush snakeColour = currentNode == Snake.headNode ? Brushes.Beige : Brushes.Green;

                    //Draw snake
                    canvas.FillEllipse(snakeColour,
                                       new Rectangle(currentNode.x * Settings.Width,
                                                     currentNode.y * Settings.Height,
                                                     Settings.Width, Settings.Height));
                }

                //Draw Food
                canvas.FillEllipse(Brushes.Red,
                                   new Rectangle(food.x * Settings.Width,
                                                 food.y * Settings.Height, Settings.Width, Settings.Height));
            }
            else
            {
                string gameOver = "GAME OVER!!\nPress Enter to try again";
                gameoverlbl.Text    = gameOver;
                gameoverlbl.Visible = true;
            }
        }
Esempio n. 2
0
        //Place random food object
        private void GenerateFood()
        {
            int maxXPos = pictureBoxArena.Size.Width / Settings.Width;
            int maxYPos = pictureBoxArena.Size.Height / Settings.Height;

            Random random = new Random();

            food = new SnakeNode(random.Next(0, maxXPos), random.Next(0, maxYPos));
        }
Esempio n. 3
0
 public void AddToEnd(int x, int y)
 {
     if (next == null)
     {
         next = new SnakeNode(x, y);
     }
     else
     {
         next.AddToEnd(x, y);
     }
 }
Esempio n. 4
0
 public void AddToEnd(int x, int y)
 {
     if (headNode == null)
     {
         headNode = new SnakeNode(x, y);
     }
     else
     {
         headNode.AddToEnd(x, y);
     }
 }
Esempio n. 5
0
        private void StartGame()
        {
            new Settings();

            gameoverlbl.Visible = false;
            LaunchTimer();

            //Initialize SnakeSnake
            Snake.headNode = null;
            headNode       = new SnakeNode(35, 10);
            Snake.headNode = headNode;
            Snake.AddToEnd(35, 9);
            Snake.AddToEnd(35, 8);

            score.Text = Settings.Score.ToString();
            GenerateFood();
        }
Esempio n. 6
0
 public SnakeNode(int X, int Y)
 {
     x    = X;
     y    = Y;
     next = null;
 }
Esempio n. 7
0
 public SnakeNodes()
 {
     headNode = null;
 }
Esempio n. 8
0
        private void MovePlayer()
        {
            SnakeNodes NewSnake = new SnakeNodes();
            SnakeNode  prevNode = new SnakeNode();

            for (; Snake.headNode != null; Snake.headNode = Snake.headNode.next)
            {
                if (Snake.headNode.x == headNode.x && Snake.headNode.y == headNode.y)
                {
                    prevNode.x = Snake.headNode.x;
                    prevNode.y = Snake.headNode.y;

                    switch (Settings.direction)
                    {
                    case SnakeDirection.Right:
                        Snake.headNode.x++;
                        break;

                    case SnakeDirection.Left:
                        Snake.headNode.x--;
                        break;

                    case SnakeDirection.Up:
                        Snake.headNode.y--;
                        break;

                    case SnakeDirection.Down:
                        Snake.headNode.y++;
                        break;
                    }

                    NewSnake.AddToEnd(Snake.headNode.x, Snake.headNode.y);
                    headNode = Snake.headNode;

                    //Get maximum X and Y Pos
                    int maxXPos = pictureBoxArena.Size.Width / Settings.Width;
                    int maxYPos = pictureBoxArena.Size.Height / Settings.Height;

                    //Detect collission with game borders.
                    if (Snake.headNode.x < 0 || Snake.headNode.y < 0 ||
                        Snake.headNode.x >= maxXPos || Snake.headNode.y >= maxYPos)
                    {
                        Die();
                    }


                    //Detect collission with body
                    var bodyNode = Snake.headNode.next;
                    while (bodyNode != null)
                    {
                        if (bodyNode.x == Snake.headNode.x && bodyNode.y == Snake.headNode.y)
                        {
                            Die();
                        }
                        bodyNode = bodyNode.next;
                    }

                    //Detect collision with food piece
                    if (Snake.headNode.x == food.x && Snake.headNode.y == food.y)
                    {
                        Eat();
                    }
                }
                else
                {
                    NewSnake.AddToEnd(prevNode.x, prevNode.y);
                    prevNode.x = Snake.headNode.x;
                    prevNode.y = Snake.headNode.y;
                }
            }
            Snake = NewSnake;
        }