【学习日志】2022.08.12 C#数据类型 C++控制台小游戏_贪吃蛇

C#数据类型 

 var

 控制台小游戏_贪吃蛇

/*
* EndlessDaydream
* endlessdaydream@qq.com
* Communication University of China / Electronic and Information Engineering
* https://github.com/Angelyatou
*/

//控制台小游戏_贪吃蛇(C#)
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SnakeGame
{
    struct Pos
    {
        public int x;
        public int y;
        public Pos(int x, int y)
        {

            this.x = x;
            this.y = y;
        }
        public Pos(Pos p)
        {
            this.x = p.x;
            this.y = p.y;
        }
    }

    class Snake
    {
        public LinkedList<Pos> bodies;
        public int dir;

        public Snake()
        {
            bodies = new LinkedList<Pos>();
            bodies.AddLast(new Pos(2, 0));
            bodies.AddLast(new Pos(1, 0));
            bodies.AddLast(new Pos(0, 0));

            dir = 0;
        }
    }

    class Apple
    {
        public Pos pos;
    }
    class Snake_2
    {
        static Snake snake;
        static Apple apple;
        static bool gameOver;

        static Random random;

        static int W = 30;
        static int H = 20;

        static string[,] buffer;

        static void Init()
        {
            random = new Random();

            snake = new Snake();
            apple = new Apple();
            apple.pos = new Pos(5, 5);
            gameOver = false;
            buffer = new string[H + 2, W + 2];
            Console.CursorVisible = false;
        }

        static int Input()
        {

            ConsoleKey key = ConsoleKey.Spacebar;
            while (Console.KeyAvailable)
            {
                key = Console.ReadKey(true).Key;
            }

            switch (key)
            {
                case ConsoleKey.W:
                    return 1;
                case ConsoleKey.S:
                    return 2;
                case ConsoleKey.A:
                    return 3;
                case ConsoleKey.D:
                    return 4;
                default:
                    return 0;
            }
        }

        static void ChangeSnakeDir(int dir)
        {
            if (dir == 0)
            {
                return;
            }
            if (dir == snake.dir)
            {
                return;
            }
            if (dir == 1 && snake.dir == 2)
            {
                return;
            }
            if (dir == 2 && snake.dir == 1)
            {
                return;
            }
            if (dir == 3 && snake.dir == 4)
            {
                return;
            }
            if (dir == 4 && snake.dir == 3)
            {
                return;
            }

            snake.dir = dir;

        }

        static void GameLogic(int dir)
        {
            if (gameOver)
            {
                return;
            }

            ChangeSnakeDir(dir);

            int x = snake.bodies.First.Value.x;
            int y = snake.bodies.First.Value.y;

            switch (snake.dir)
            {
                case 1:
                    y -= 1;
                    break;
                case 2:
                    y += 1;
                    break;
                case 3:
                    x -= 1;
                    break;
                case 4:
                    x += 1;
                    break;
                default:
                    break;
            }
            if (x < 0 || x >= W || y < 0 || y >= H)
            {
                gameOver = true;
                return;
            }

            //复制蛇身体,移动
            //for (int i = snake.bodies.Count - 1; i >= 1; i--)
            //{
            //    snake.bodies[i] = snake.bodies[i - 1];
            //}
            //snake.bodies[0] = new Pos(x, y);
            snake.bodies.RemoveLast();
            snake.bodies.AddFirst(new Pos(x, y));

            if (snake.bodies.First.Value.Equals(apple.pos))
            {
                //
                apple = new Apple();
                //apple.pos = new Pos(random.Next(0, W), random.Next(0, H));
                int n = W * H - snake.bodies.Count;
                int rand = random.Next(0, n);
                bool[,] temp = new bool[H, W];

                LinkedListNode<Pos> p = snake.bodies.First;
                while (p != null)
                {
                    temp[p.Value.y, p.Value.x] = true;
                    p = p.Next;
                    //buffer[p.Value.y+1,p.Value.x+1]="o"
                    for (int i = 0; i < H; i++)
                    {
                        bool flag = false;
                        for(int j = 0; j < W; j++)
                        {
                            if (!temp[i, j])
                            {
                                if (rand == 0)
                                {
                                    apple.pos = new Pos(j, i);
                                    flag = true;
                                    break;
                                }
                                rand--;
                            }
                            
                        }
                        if (flag)
                        {
                            break;
                        }
                    }
                }

                //蛇变长
                //snake.bodies.Add(new Pos(snake.bodies[snake.bodies.Count-1]));
                var tail = snake.bodies.Last.Value;
                snake.bodies.AddLast(tail);
            }

            Debug.WriteLine("snake:{0} {1}", x, y);

        }

        static void DrawAll()
        {
            for (int i = 0; i < buffer.GetLength(0); i++)
            {
                for (int j = 0; j < buffer.GetLength(1); j++)
                {
                    buffer[i, j] = " ";
                }

            }
            for (int i = 0; i < buffer.GetLength(0); i++)
            {
                buffer[i, 0] = "#";
            }
            for (int i = 0; i < buffer.GetLength(0); i++)
            {
                buffer[i, W] = "#";
            }
            for (int j = 0; j < buffer.GetLength(1); j++)
            {
                buffer[0, j] = "#";
            }
            for (int j = 0; j < buffer.GetLength(1); j++)
            {
                buffer[H + 1, j] = "#";
            }
            //画蛇
            LinkedListNode<Pos> p = snake.bodies.First;
            while (p != null)
            {
                buffer[p.Value.y + 1, p.Value.x + 1] = "o";
                p = p.Next;
            }
            //for (int i = 0; i < snake.bodies.Count; i++)
            //{
            //    var b = snake.bodies[i];
            //    buffer[b.y + 1, b.x + 1] = "o";
            //}
            //画苹果
            if (apple != null)
            {
                buffer[apple.pos.y + 1, apple.pos.x + 1] = "w";
            }
            //buffer[snake.y + 1, snake.x + 1] = "o";

            Refresh();

        }

        static void Refresh()
        {

            Console.Clear();

            for (int i = 0; i < buffer.GetLength(0); i++)
            {
                for (int j = 0; j < buffer.GetLength(1); j++)
                {
                    Console.Write(buffer[i, j]);
                }
                Console.WriteLine();
            }
        }


        static void Main(string[] args)
        {

            Init();
            DrawAll();
            while (true)
            {
                int dir = Input();
                GameLogic(dir);
                DrawAll();
                Thread.Sleep(1000);
            }
        }
    }
}

阅读终点,创作起航,您可以撰写心得或摘录文章要点写篇博文。去创作
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,下面是基于C#控制台应用的贪吃游戏的代码实现。 ```csharp using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleSnake { class Program { static void Main(string[] args) { Console.CursorVisible = false; //隐藏光标 Console.SetWindowSize(60, 30); //设置窗口大小 Console.SetBufferSize(60, 30); //设置缓冲区大小 //画边框 for (int i = 0; i < 60; i++) { Console.SetCursorPosition(i, 0); Console.Write("#"); Console.SetCursorPosition(i, 29); Console.Write("#"); } for (int i = 0; i < 30; i++) { Console.SetCursorPosition(0, i); Console.Write("#"); Console.SetCursorPosition(59, i); Console.Write("#"); } //初始化 List<Point> snake = new List<Point>(); snake.Add(new Point(30, 15)); snake.Add(new Point(29, 15)); snake.Add(new Point(28, 15)); snake.Add(new Point(27, 15)); snake.Add(new Point(26, 15)); //初始化食物 Point food = new Point(RandomNumber(1, 58), RandomNumber(1, 28)); Console.SetCursorPosition(food.x, food.y); Console.Write("*"); //初始化方向 Direction direction = Direction.Right; //初始化分数 int score = 0; while (true) { //获取键盘输入 if (Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey(true); switch (key.Key) { case ConsoleKey.UpArrow: if (direction != Direction.Down) direction = Direction.Up; break; case ConsoleKey.DownArrow: if (direction != Direction.Up) direction = Direction.Down; break; case ConsoleKey.LeftArrow: if (direction != Direction.Right) direction = Direction.Left; break; case ConsoleKey.RightArrow: if (direction != Direction.Left) direction = Direction.Right; break; } } //移动 Point head = snake[0]; Point newHead = new Point(head.x, head.y); switch (direction) { case Direction.Up: newHead.y--; break; case Direction.Down: newHead.y++; break; case Direction.Left: newHead.x--; break; case Direction.Right: newHead.x++; break; } snake.Insert(0, newHead); Console.SetCursorPosition(newHead.x, newHead.y); Console.Write("@"); if (newHead.x == food.x && newHead.y == food.y) //吃到食物 { food = new Point(RandomNumber(1, 58), RandomNumber(1, 28)); Console.SetCursorPosition(food.x, food.y); Console.Write("*"); score += 10; } else //没有吃到食物 { Point tail = snake[snake.Count - 1]; Console.SetCursorPosition(tail.x, tail.y); Console.Write(" "); snake.RemoveAt(snake.Count - 1); } //判断是否撞到边框或自己 if (newHead.x == 0 || newHead.x == 59 || newHead.y == 0 || newHead.y == 29) break; for (int i = 1; i < snake.Count; i++) { if (newHead.x == snake[i].x && newHead.y == snake[i].y) { Console.SetCursorPosition(0, 31); Console.Write("Game Over! Your score is: " + score); return; } } //延迟一段时间 System.Threading.Thread.Sleep(100); } } static int RandomNumber(int min, int max) //生成指定范围内的随机数 { Random random = new Random(); return random.Next(min, max + 1); } } enum Direction //方向枚举 { Up, Down, Left, Right } class Point //坐标类 { public int x; public int y; public Point(int x, int y) { this.x = x; this.y = y; } } } ``` 希望这个代码对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EndlessDaydream

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值