author_avatar
QcrTiMo

2025.05.26总结

2025-05-27 00:01

今天学了很多东西。

早上用C#写了许多案例小项目,贪吃蛇是最好的一个。

下午我就开始练习unity了,刚开始写移动脚本发现绑定的世界坐标轴,大意了。

晚上收集了制作2D自上而下像素游戏的必要资源,创建了一个玩家NPC,还有wasd平滑移动,明天就要开始做动画了,包括攻击、死亡、射箭等。

以下是我的贪吃蛇代码

using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics.CodeAnalysis;
using System.Linq; //用于 .any()和 .First()等LINQ操作
using System.Threading;  //用于Thread.Sleep()等线程操作

namespace study_5
{
    //定义一个结构体来表示坐标点
    public struct Point
    {
        public int X { get; set; }
        public int Y { get; set; }

        public Point(int x, int y)
        {
            X = x;
            Y = y;
        }

        //重写Equals和GetHashCode以便在List中正确比较Point对象
        public override bool Equals(object obj)
        {
            if (obj is Point other)
            {
                return X == other.X && Y == other.Y;
            }
            return false;
        }

        public override int GetHashCode()
        {
            return HashCode.Combine(X, Y);
        }

        public static bool operator ==(Point p1, Point p2)
        {
            return p1.Equals(p2);
        }

        public static bool operator !=(Point p1, Point p2)
        {
            return !p1.Equals(p2);
        }
    }

    //定义蛇的移动方向
    public enum Direction
    {
        Up,
        Down,
        Left,
        Right
    }

    class Program
    {
        //游戏设置
        const int GameWidth = 50;  //游戏区域宽度
        const int GameHeight = 40; //游戏区域高度
        const char SnakeChar = '■'; //蛇身体字符
        const char FoodChar = '●';  //食物字符 
        const char BorderChar = '#'; //边框字符
        static int Score = 0;
        static int GameSpeed = 200; //游戏速度,单位毫秒,越小越快

        //游戏状态
        static List<Point> snake;      //蛇的身体,List的第一个元素是蛇头
        static Point food;             //食物的位置
        static Direction currentDirection; //当前蛇的移动方向
        static bool gameOver;          //游戏是否结束
        static Random random = new Random(); //用于生成随机数

        static void Main(string[] args)
        {
            Console.Title = "贪吃蛇";
            Console.CursorVisible = false; //隐藏光标
            //设置控制台窗口大小,注意缓冲区大小可能也需要调整
            Console.SetWindowSize(GameWidth + 2, GameHeight + 3); // +2/+3是为了边框和分数显示
            Console.SetBufferSize(GameWidth + 2, GameHeight + 3);

            InitializeGame();

            //游戏主循环
            while (!gameOver)
            {
                if (Console.KeyAvailable) //如果有按键按下
                {
                    HandleInput(Console.ReadKey(true).Key);
                }

                UpdateGame();
                DrawGame();
                Thread.Sleep(GameSpeed); //控制游戏速度
            }

            DisplayGameOver();
            Console.ReadKey(); //等待按键后退出
        }

        //初始化游戏
        static void InitializeGame()
        {
            snake = new List<Point>();
            //初始蛇的位置和长度
            int startX = GameWidth / 2;
            int startY = GameHeight / 2;
            snake.Add(new Point(startX, startY));     //蛇头
            snake.Add(new Point(startX - 1, startY)); //蛇身
            snake.Add(new Point(startX - 2, startY)); //蛇尾 (初始向右移动)

            currentDirection = Direction.Right; //初始向右移动
            GenerateFood();
            Score = 0;
            GameSpeed = 200; //重置速度
            gameOver = false;
            Console.Clear(); //清屏开始新游戏
        }

        //生成食物
        static void GenerateFood()
        {
            //确保食物不会生成在蛇的身体上
            do
            {
                food = new Point(random.Next(0, GameWidth), random.Next(0, GameHeight));
            } while (snake.Contains(food)); //如果食物位置在蛇身上,则重新生成
        }

        //处理用户输入
        static void HandleInput(ConsoleKey key)
        {
            switch (key)
            {
                case ConsoleKey.UpArrow:
                    if (currentDirection != Direction.Down) //防止蛇直接反向
                        currentDirection = Direction.Up;
                    break;
                case ConsoleKey.DownArrow:
                    if (currentDirection != Direction.Up)
                        currentDirection = Direction.Down;
                    break;
                case ConsoleKey.LeftArrow:
                    if (currentDirection != Direction.Right)
                        currentDirection = Direction.Left;
                    break;
                case ConsoleKey.RightArrow:
                    if (currentDirection != Direction.Left)
                        currentDirection = Direction.Right;
                    break;
                case ConsoleKey.Escape:
                    gameOver = true; //按Esc键退出游戏
                    break;
            }
        }

        //更新游戏状态
        static void UpdateGame()
        {
            if (gameOver)
            {
                return;
            }

            //计算新的蛇头位置
            Point newHead = snake.First();  //获取当前蛇头
            switch (currentDirection)
            {
                case Direction.Up:
                    newHead.Y--;
                    break;
                case Direction.Down:
                    newHead.Y++;
                    break;
                case Direction.Left:
                    newHead.X--;
                    break;
                case Direction.Right:
                    newHead.X++;
                    break;
            }

            //检查碰撞
            //撞墙
            if (newHead.X < 0 || newHead.X >= GameWidth || newHead.Y < 0 || newHead.Y >= GameHeight)
            {
                gameOver = true; //撞墙,游戏结束
                return;
            }

            //撞到自己
            if (snake.Skip(1).Contains(newHead))
            {
                gameOver = true; //撞到自己,游戏结束
                return;
            }

            //将新蛇头添加到蛇的身体列表的开头
            snake.Insert(0, newHead);

            //检查是否吃到食物
            if (newHead == food)
            {
                Score += 10;
                GenerateFood();
                if (Score % 50 == 0 && GameSpeed > 50)
                {
                    GameSpeed -= 20;
                }
            }
            else
            {
                //没有吃到食物,移除蛇尾,保持长度不变
                snake.RemoveAt(snake.Count - 1);
            }
        }

        //绘制游戏画面
        static void DrawGame()
        {
            Console.SetCursorPosition(0, 0);

            //绘制边框
            for (int i = 0; i < GameWidth + 2; i++)
            {
                Console.Write(BorderChar);
            }
            Console.WriteLine();

            for (int y = 0; y < GameHeight; y++)
            {
                Console.Write(BorderChar);  //绘制左边框
                for (int x = 0; x < GameWidth; x++)
                {
                    Point currentPoint = new Point(x, y);
                    if (snake.First() == currentPoint)  //蛇头
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.Write(SnakeChar);
                        Console.ResetColor();
                    }
                    else if (snake.Contains(currentPoint))
                    {
                        Console.Write(SnakeChar); //蛇身
                    }
                    else if (food == currentPoint)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write(SnakeChar);
                        Console.ResetColor();
                    }
                    else
                    {
                        Console.Write(' '); //空白区域
                    }
                }
                Console.Write(BorderChar);  //绘制右边框
                Console.WriteLine();
            }

            //绘制底部边框
            for (int i = 0; i < GameWidth + 2; i++)
            {
                Console.Write(BorderChar);
            }
            Console.WriteLine();

            //绘制分数
            Console.SetCursorPosition(0, GameHeight + 2); //在边框下方显示分数
            Console.WriteLine($"分数:{Score} 速度:{(200 - GameSpeed) / (20 + 1)}");//显示速度分数
        }

        //显示游戏结束信息
        static void DisplayGameOver()
        {
            Console.SetCursorPosition(GameWidth / 2 - 5, GameHeight / 2);
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("游戏结束!");
            Console.SetCursorPosition(GameWidth / 2 - 8, GameHeight / 2 + 1);
            Console.WriteLine($"最终得分: {Score}");
            Console.SetCursorPosition(GameWidth / 2 - 10, GameHeight / 2 + 2);
            Console.WriteLine("按任意键重新开始...");
            Console.ResetColor();
            Console.ReadKey(); //等待按键
            InitializeGame(); //重新初始化游戏,允许重新开始
        }
    }
}
author_avatar
QcrTiMo

为了更好的学习

2025-05-25 23:41

坚持每天写总结,类似于写日记或进行书面反思,对大脑有多方面的好处。相关研究表明,这种习惯可以从认知、情绪和整体大脑健康等多个层面带来积极影响。

具体来说,每天写总结对大脑的好处可能包括:

  • 提高认知功能和记忆力: 研究表明,书写有助于我们更好地保留信息。 写日记可以增加工作记忆容量,改善认知处理能力。手脑并用的书写过程能增强大脑和双手之间的独特联系,有助于增强大脑中信息的传递、记忆和获取,从而不仅能更牢固地记事,还能增强理解力。
  • 促进情绪调节和减轻压力: 写日记是一种处理情绪和提高自我意识的重要方式。它有助于组织混乱的情绪和经历,使其更易于管理,从而减轻压力。研究发现,表达性写作与抑郁的显著减少有关,定期写日记能显著降低压力和焦虑水平。通过书写来处理思想和感受,有助于情绪的宣泄和管理。
  • 增强专注力: 写日记可以将人们带入一种全神贯注的思考状态,有助于“驯服”纷乱的思绪,改善注意力。
  • 提升情商: 通过写日记反思经历可以提高情商和韧性。写日记有助于更好地理解自身和他人的感受,从而在人际交往中达成更好的理解。
  • 激发创造力:将想法不加思索地写下来,即“意识流”写作,有助于激发和发掘全新的创意和想法。
  • 促进大脑神经可塑性: 大脑具有神经可塑性,即根据经验和学习进行自我重塑和调整的能力。学习新技能或进行认知活动(如写作)可以刺激神经连接的生成和巩固。 写作激活大脑的左半球(负责分析和推理),同时释放右半球进行创造力和情感的表达,这种平衡的活动可以增强情感调节和解决问题的能力。
  • 帮助实现目标: 以日记的方式写下目标能够刺激大脑中与实现目标相关的区域,使大脑更关注相关的机会和工具。
  • 改善沟通技巧: 写日记作为一种书面沟通形式,这种追踪自我思想的“默读”过程会对实际的口头表达能力产生积极影响。

需要注意的是,虽然很多益处是基于写日记的广泛研究,而“总结”可以视为一种特定形式的日记或反思性写作。其核心都在于通过书写来处理信息、情绪和想法,从而对大脑产生积极作用。