Ejemplo n.º 1
0
        public void update()
        {
            //Removes last graphic of snake
            base.undraw();

            //Check if key is pressed and set direction
            if (Console.KeyAvailable == true)
            {
                //Detects key pressed
                ConsoleKeyInfo cki = Console.ReadKey(true);

                //Changes directions based on key pressed
                switch (cki.Key)
                {
                case ConsoleKey.UpArrow:
                case ConsoleKey.W:
                    if (direction != "Down")
                    {
                        direction = "Up";
                    }
                    break;

                case ConsoleKey.DownArrow:
                case ConsoleKey.S:
                    if (direction != "Up")
                    {
                        direction = "Down";
                    }
                    break;

                case ConsoleKey.RightArrow:
                case ConsoleKey.D:
                    if (direction != "Left")
                    {
                        direction = "Right";
                    }
                    break;

                case ConsoleKey.LeftArrow:
                case ConsoleKey.A:
                    if (direction != "Right")
                    {
                        direction = "Left";
                    }
                    break;
                }
            }
            //Move Snake towards direction
            switch (direction)
            {
            case "Up":
                base.y--;
                break;

            case "Down":
                base.y++;
                break;

            case "Right":
                base.x++;
                break;

            case "Left":
                base.x--;
                break;
            }

            //Check if on top of apple
            if (this.x == apple.x && this.y == apple.y)
            {
                //If so respawn apple
                apple.respawn();

                //And grow tail
                if (cur_pos == 0)
                {
                    tail[cur_pos] = new SnakeTail(this.x, this.y, "X", tail, cur_pos, this.direction, this);
                    tail[cur_pos].create();
                }
                else
                {
                    tail[cur_pos] = new SnakeTail(tail[cur_pos - 1].x, tail[cur_pos - 1].y, "X", tail, cur_pos, tail[cur_pos - 1].direction, this);
                    tail[cur_pos].create();
                }
                cur_pos++;
            }
            //Draws updated
            base.draw();
        }
Ejemplo n.º 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;
                    }
                }
            }
        }