【学习日志】2023.01.02 贪吃蛇2

using System;
using System.Collections.Generic;
using System.Threading;

namespace 贪吃蛇
{
    enum Dir
    {
        None,
        Up,
        Down,
        Left,
        Right
    }
    struct Pos
    {
        public int x;
        public int y;
        public Pos(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }
    class Snake
    {
        public int x;
        public int y;

        public List<Pos> bodies = new List<Pos>();  //bodies[0]代表头部
        public Dir dir = Dir.None;

        public void ChangeDir(Dir input)
        {
            if (input == Dir.None) { return; }
            if (input == dir) { return; }
            if (dir == Dir.Right && input == Dir.Left) { return; }
            if (dir == Dir.Left && input == Dir.Right) { return; }
            if (dir == Dir.Up && input == Dir.Down) { return; }
            if (dir == Dir.Down && input == Dir.Up) { return; }

            dir = input;
        }
        public void Move()
        {
            Pos head = bodies[0];
            switch (dir)
            {
                case Dir.Up:
                    {
                        head.y--;
                    }
                    break;
                case Dir.Down:
                    {
                        head.y++;
                    }
                    break;
                case Dir.Left:
                    {
                        head.x--;
                    }
                    break;
                case Dir.Right:
                    {
                        head.x++;
                    }
                    break;
                default:
                    {
                    }
                    break;
            }

            //蛇的身体,一节一节跟上前一节身体
            for (int i = bodies.Count - 1; i > 0; i--)
            {
                bodies[i] = bodies[i - 1];
            }
            bodies[0] = head;
        }
        public Snake()
        {
            bodies = new List<Pos>();
            for (int i = 0; i < 1; i++)
            {
                bodies.Add(new Pos(0, 0));
            }
        }
        public void OnEatApple()
        {
            bodies.Add(bodies[bodies.Count - 1]);
        }
    }


    class Program
    {
        static Random random;
        static Snake snake;
        static char[,] buffer; //屏幕缓冲区
        static ConsoleColor[,] colorBuffer; //屏幕颜色缓冲区
        static int width = 20;
        static int height = 20;
        static int ox = 6;
        static int oy = 6;
        static int score = 0;
        static Pos apple;

        static void RandApple()
        {
            apple.x = random.Next(0, width);
            apple.y = random.Next(0, height);
        }
        static void Main(string[] args)
        {
            //1初始化
            random = new Random();
            snake = new Snake();
            buffer = new char[height + oy + 1, width + ox + 1];
            colorBuffer = new ConsoleColor[buffer.GetLength(0), buffer.GetLength(1)];
            Dir inputDir = Dir.None;

            RandApple();

            Refresh();

            while (true)
            {
                //2输入处理
                ConsoleKeyInfo keyInfo = new ConsoleKeyInfo();
                while (Console.KeyAvailable)
                {
                    keyInfo = Console.ReadKey();
                }
                switch (keyInfo.Key)
                {
                    case ConsoleKey.UpArrow:
                        {
                            inputDir = Dir.Up;
                        }
                        break;
                    case ConsoleKey.DownArrow:
                        {
                            inputDir = Dir.Down;
                        }
                        break;
                    case ConsoleKey.LeftArrow:
                        {
                            inputDir = Dir.Left;
                        }
                        break;
                    case ConsoleKey.RightArrow:
                        {
                            inputDir = Dir.Right;
                        }
                        break;
                    default:
                        {
                            inputDir = Dir.None;
                        }
                        break;

                }

                //3蛇逻辑更新
                //蛇调整方向
                snake.ChangeDir(inputDir);

                Pos head = snake.bodies[0];
                

                //蛇撞墙结束
                if (head.y < 0 || head.y >= height || head.x < 0 || head.x > width)
                {
                    break;
                }           

                //如果蛇撞到自己的身体,游戏结束
                for (int i = 1; i < snake.bodies.Count-1; i++)
                {
                    if (head.x == snake.bodies[i].x && head.y == snake.bodies[i].y)
                    {
                        //Console.SetCursorPosition(0, 0);
                        //Console.WriteLine(head.x);
                        //Console.WriteLine(head.y);
                        //Console.WriteLine(snake.bodies[i].x);
                        //Console.WriteLine(snake.bodies[i].y);
                        //Console.ReadKey();
                        break;
                    }
                }

                //蛇前进
                snake.Move();

                //吃苹果判断
                //if (snake.bodies[0].x == apple.x && apple = snake.bodies[0].y == apple.y)
                if (snake.bodies[0].Equals(apple))
                {
                    snake.OnEatApple();
                    score++;
                    RandApple();
                }
                //4渲染
                Refresh();
                Thread.Sleep(100);
            }
            Console.WriteLine("游戏结束");
            Console.ReadKey();
        }
        static void Refresh()
        {
            //清空buffer
            for (int i = 0; i < buffer.GetLength(0); i++)
            {
                for (int j = 0; j < buffer.GetLength(1); j++)
                {
                    buffer[i, j] = ' ';
                    colorBuffer[i, j] = ConsoleColor.Gray;
                }
            }

            //画边界
            for (int i = oy - 1; i < height + oy + 1; i++)
            {
                for (int j = ox - 1; j < width + ox + 1; j++)
                {
                    if (i == oy - 1)
                    {
                        buffer[i, j] = '#';
                    }
                    else if (i == height + oy)
                    {
                        buffer[i, j] = '#';
                    }
                    else if (j == ox - 1)
                    {
                        buffer[i, j] = '#';
                    }
                    else if (j == width + ox)
                    {
                        buffer[i, j] = '#';
                    }
                }
            }
            //画蛇
            
            for (int i = 0; i < snake.bodies.Count; i++)
            {
                
                int tx = snake.bodies[i].x + ox;
                int ty = snake.bodies[i].y + oy;
                buffer[ty, tx] = 'o';
                colorBuffer[ty, tx] = ConsoleColor.Green;
            }
            //画苹果
            buffer[apple.y + oy, apple.x + ox] = 'x';
            colorBuffer[apple.y + oy, apple.x + ox] = ConsoleColor.Red;
            //写得分,在buffer中写不确定长度的字符串,不方便
            //buffer[oy - 2, ox - 1] = '得';
            //buffer[oy - 2, ox] = '分';
            //buffer[oy - 2, ox + 1] = ':';
            //buffer[oy - 2, ox + 2] = score.ToString()[0];

            //Console打印buffer
            Console.Clear();
            for (int i = 0; i < buffer.GetLength(0); i++)
            {
                for (int j = 0; j < buffer.GetLength(1); j++)
                {
                    //if (buffer[i, j] == '果')
                    //{
                    //    Console.ForegroundColor = ConsoleColor.Red;
                    //}
                    //else if (buffer[i, j] == 'o'){
                    //    Console.ForegroundColor = ConsoleColor.Green;
                    //}
                    Console.ForegroundColor = colorBuffer[i, j];

                    if (buffer[i, j] < 128)
                    {
                        Console.Write(buffer[i, j] + " ");
                    }
                    else
                    {
                        Console.Write(buffer[i, j]);
                    }
                    Console.ForegroundColor = ConsoleColor.Gray;
                }
                Console.WriteLine();
            }
            Console.SetCursorPosition((ox - 1) * 2, oy - 2);
            Console.Write($"得分:{score}");
        }
    }
}

猜你喜欢

转载自blog.csdn.net/Angelloveyatou/article/details/128522502
今日推荐