Example #1
0
 public SnakeHead(int x, int y, string graphic, Apple apple, SnakeTail[] tail) : base(x, y, graphic)
 {
     this.apple = apple;
     this.tail  = tail;
 }
Example #2
0
        public void start_new()
        {
            //Instantiate objects and set up predefined definitions
            Console.CursorVisible = false;

            //Set up random object
            Random random = new Random();

            //Creates array for snaketails
            SnakeTail[] tail = new SnakeTail[(Console.WindowWidth - 2) * (Console.WindowHeight - 2)];

            //Set up the apple object
            Apple apple = new Apple(random.Next(1, Console.WindowWidth - 2), random.Next(1, Console.WindowHeight - 2), "@", random);

            //Creates player object
            SnakeHead player = new SnakeHead((Console.WindowWidth / 2), (Console.WindowHeight / 2), "X", apple, tail);

            //Draws walls
            this.walls();

            //Draws apple
            apple.draw();

            //Draws player
            player.draw();

            while (true)
            {
                player.update();

                if (player.cur_pos != 0)
                {
                    tail[0].update(player.direction);
                }

                if (player.direction == "Down" || player.direction == "Up")
                {
                    System.Threading.Thread.Sleep(100);
                }
                else
                {
                    System.Threading.Thread.Sleep(55);
                }

                //Checks if snake is in wall
                if (player.x == 0 || player.x == Console.WindowWidth - 1 || player.y == 0 || player.y == Console.WindowHeight - 1)
                {
                    this.game_over(player);
                    break;
                }
                //Checks if player is in snake tail
                for (int i = 0; i <= player.cur_pos - 1; i++)
                {
                    if (player.x == tail[i].x && player.y == tail[i].y)
                    {
                        this.game_over(player);
                        return;
                    }
                }
            }
        }