Beispiel #1
0
        private void UpdateSnake(object sender, EventArgs e)
        {
            if (!gameOver)
            {
                for (int i = snake.Count - 1; i >= 0; i--)
                {
                    if (i == 0)
                    {
                        switch (direction)
                        {
                            case 0: // Down
                                snake[i].Y++;
                                break;
                            case 1: // Left
                                snake[i].X--;
                                break;
                            case 2: // Right
                                snake[i].X++;
                                break;
                            case 3: // Up
                                snake[i].Y--;
                                break;
                        }
                        SnakePart head = snake[0];

                        // Out of Bound
                        //if (head.X >= 20 || head.X < 0 || head.Y >= 15 || head.Y < 0)
                        //GameOver();
                        if (head.X >= 20)
                            head.X = 0;
                        if (head.X < 0)
                            head.X = 20;
                        if (head.Y >= 15)
                            head.Y = 0;
                        if (head.Y < 0)
                            head.Y = 15;

                        // Collision with itself
                        for (int j = 1; j < snake.Count; j++)
                        {
                            if (head.X == snake[j].X && head.Y == snake[j].Y)
                                GameOver();
                        }

                        //Collision with obstacle
                        foreach (var collision in obstacles)
                        {
                            if (head.X == collision.X && head.Y == collision.Y)
                                GameOver();
                        }

                        // Collision with food
                        if (head.X == food.X && head.Y == food.Y)
                        {
                            SnakePart part = new SnakePart(snake[snake.Count - 1].X, snake[snake.Count - 1].Y);
                            snake.Add(part);
                            score++;
                            GenerateFood();
                            GenerateObstacle(snake.Count);
                        }
                    }
                    else
                    {
                        snake[i].X = snake[i - 1].X;
                        snake[i].Y = snake[i - 1].Y;
                    }
                }
            }
        }
Beispiel #2
0
 private void GenerateObstacle(int obstacleNumber)
 {
     Random randomObstacle = new Random();
     var newObstacle = new SnakePart(randomObstacle.Next(0, 10), randomObstacle.Next(0, 10));
     obstacles.Add(newObstacle);
 }
Beispiel #3
0
 private void StartGame()
 {
     gameOver = false;
     snake.Clear();
     obstacles.Clear();
     score = 0;
     SnakePart head = new SnakePart(10, 8);
     snake.Add(head);
     GenerateFood();
     GenerateObstacle(snake.Count);
 }
Beispiel #4
0
 private void GenerateFood()
 {
     Random random = new Random();
     int max_tile_w = pbCanvas.Size.Width / tile_width;
     int max_tile_h = pbCanvas.Size.Height / tile_height;
     food = new SnakePart(random.Next(0, max_tile_w),random.Next(0, max_tile_h));
 }