Unity基础之C#入门篇笔记实践小项目5:项目实践

简单需求分析

1.开始界面功能

游戏界面
在这里插入图片描述

功能:
控制台输入
控制台输出
控制台颜色变化

2.游戏界面功能

游戏界面
在这里插入图片描述
功能:
控制台输入
控制台输出
控制台颜色变化
while,switch
判断(条件运算符,if语句)
回合制战斗(随机数,循环,if语句)

3.结束界面功能

结束界面
在这里插入图片描述
功能:
控制台输入
控制台输出
控制台颜色变化
界面之间的相互切换

控制台基础设置


            //隐藏光标
            Console.CursorVisible = false;

            int w = 50;
            int h = 30;
            //设置舞台(控制台)的大小
            Console.SetWindowSize(w, h);
            Console.SetBufferSize(w, h); 

游戏逻辑代码

设置编号1为开始场景,编号2为游戏场景,编号3为结束场景,通过改变编号来切换场景;

static void Main(string[] args)
        {
            #region  控制台基础设置
            //隐藏光标
            Console.CursorVisible = false;
            //通过两个变量来存储 舞台的大小
            int w = 50;
            int h = 30;
            //设置舞台(控制台)的大小
            Console.SetWindowSize(w, h);
            Console.SetBufferSize(w, h);
            #endregion

            #region  多个场景
            //当前所在场景的编号
            int nowSceneID = 1;
            //游戏结束场景,场景显示内容
            string gameOverInfo = "游戏通关";
            while (true)
            {
                //不同的场景ID 进行不同的逻辑处理
                switch (nowSceneID)
                {
                    //开始场景
                    case 1:
                        Console.Clear();
                        #region  开始场景逻辑
                        Console.SetCursorPosition(w / 2 - 6, 8);
                        Console.Write("小白营救公主");
                        //当前选项的编号
                        int nowSelIndex = 0;
                        //因为要输入 我们可以构造一个 开始界面自己的 死循环
                        //专门用来处理 开始场景相关的逻辑
                        while (true)
                        {
                            //用一个标识  来处理 想要在while循环内部的switch逻辑执行时 希望退出外层while循环时
                            // 改变标识即可
                            bool isQuitWhile = false;
                            // 显示 内容
                            //先设置光标位置 再显示内容
                            Console.SetCursorPosition(w / 2 - 4, 13);
                            //根据当前选择的编号 来决定 是否变色
                            Console.ForegroundColor = nowSelIndex == 0 ? ConsoleColor.Red : ConsoleColor.White;
                            Console.Write("开始游戏");
                            Console.SetCursorPosition(w / 2 - 4, 15);
                            Console.ForegroundColor = nowSelIndex == 1 ? ConsoleColor.Red : ConsoleColor.White;
                            Console.Write("退出游戏");
                            // 检测 输入
                            // 检测玩家 输入的一个键内容 并且不会再控制台上显示输入的内容
                            char input = Console.ReadKey(true).KeyChar;
                            switch (input)
                            {
                                case 'W':
                                case 'w':
                                    //--nowSelIndex;
                                    //if (nowSelIndex < 0)
                                    //{
                                    //    nowSelIndex = 0;
                                    //}
                                    nowSelIndex = 0;
                                    break;
                                case 'S':
                                case 's':
                                    //++nowSelIndex;
                                    //if (nowSelIndex > 1)
                                    //{
                                    //    nowSelIndex = 1;
                                    //}
                                    nowSelIndex = 1;
                                    break;
                                case 'J':
                                case 'j':
                                    if (nowSelIndex == 0)
                                    {
                                        //1.改变当前选择的场景ID
                                        nowSceneID = 2;
                                        //2.要退出 内层while循环
                                        isQuitWhile = true;
                                    }
                                    else
                                    {
                                        //关闭控制台
                                        Environment.Exit(0);
                                    }
                                    break;
                            }

                            if (isQuitWhile)
                            {
                                break;
                            }
                        }
                        #endregion
                        break;
                    //游戏场景
                    case 2:
                        Console.Clear();

                        #region  不变的红墙
                        //设置颜色为红色
                        Console.ForegroundColor = ConsoleColor.Red;
                        //画墙
                        
                        for (int i = 0; i < w; i += 2)
                        {
                            //上方墙
                            Console.SetCursorPosition(i, 2);
                            Console.Write("■");
                            //下方墙
                            Console.SetCursorPosition(i, h - 1);
                            Console.Write("■");
                            //中间墙
                            Console.SetCursorPosition(i, h - 6);
                            Console.Write("■");
                        }

                        for (int i = 0; i < h; i++)
                        {
                            //左边的墙
                            Console.SetCursorPosition(0, i);
                            Console.Write("■");
                            //右边的墙
                            Console.SetCursorPosition(w - 2, i);
                            Console.Write("■");
                        }

                        #endregion

                        #region Boss属性相关
                        int bossX = 24;
                        int bossY = 15;
                        int bossAtkMin = 7;
                        int bossAtkMax = 13;
                        int bossHp = 100;
                        string bossIcon = "■";
                        //申明一个颜色变量
                        ConsoleColor bossColor = ConsoleColor.Green;
                        #endregion

                        #region 玩家属性相关
                        int playerX = 4;
                        int playerY = 5;
                        int playerAtkMin = 8;
                        int playerAtkMax = 12;
                        int playerHp = 100;
                        string playerIcon = "●";
                        ConsoleColor playerColor = ConsoleColor.Yellow;
                        char playerInput;

                        #endregion

                        #region 玩家战斗相关

                        //战斗状态
                        bool isFight = false;
                        //作用是从while循环内部的switch改变标识用来跳出外层的while循环
                        bool isOver = false;

                        #endregion

                        #region 公主属性相关
                        int princessX = 24;
                        int princessY = 5;
                        string princessIcon = "★";
                        ConsoleColor princessColor = ConsoleColor.Blue;
                        #endregion

                        //游戏场景的死循环 专门用来 检测 玩家输入相关循环
                        while (true)
                        {
                            #region boss属性相关
                            if (bossHp > 0)
                            {
                                //绘制boos图标
                                Console.SetCursorPosition(bossX, bossY);
                                Console.ForegroundColor = bossColor;
                                Console.Write(bossIcon);
                            }
                            #endregion

                            #region 公主相关
                            else
                            {
                                Console.SetCursorPosition(princessX, princessY);
                                Console.ForegroundColor = princessColor;
                                Console.Write(princessIcon);
                            }
                            #endregion
                           
                            #region 玩家属性相关
                            //画出玩家
                            Console.SetCursorPosition(playerX, playerY);
                            Console.ForegroundColor = playerColor;
                            Console.Write(playerIcon);
                            #endregion



                            //玩家输入
                            playerInput = Console.ReadKey(true).KeyChar;
                            //战斗状态处理逻辑
                            if (isFight)
                            {
                                if(playerInput == 'J' || playerInput == 'j')
                                {
                                    // 在这判断玩家或者怪物是否死亡,如果死亡了,维续之后的流程 
                                    if(playerHp<=0)
                                    {
                                        //游戏结束
                                        nowSceneID = 3;
                                        break;
                                    }
                                    else if (bossHp<=0)
                                    {
                                        //去营救公主
                                        //boss擦除
                                        Console.SetCursorPosition(bossX, bossY);
                                        Console.Write("  ");
                                        isFight = false;
                                    }
                                    else
                                    {
                                        //玩家打怪物
                                        Random r = new Random();
                                        //得到随机攻击力
                                        int atk = r.Next(playerAtkMin, playerAtkMax);
                                        //怪物减血
                                        bossHp -= atk;
                                        //打印信息(先擦除,在显示)
                                        Console.ForegroundColor = ConsoleColor.Green;
                                        Console.SetCursorPosition(2, h - 6);
                                        Console.Write("                                         ");
                                        Console.SetCursorPosition(2, h - 6);
                                        Console.Write("你对Boss造成{0}伤害,Boss剩余血量为:{1}", atk, bossHp);
                                        if (bossHp > 0)
                                        {
                                            //怪物打玩家
                                            //得到随机攻击力
                                            atk = r.Next(bossAtkMin, bossAtkMax);
                                            //玩家减血
                                            playerHp -= atk;
                                            //打印信息(先擦除,在显示)
                                            Console.ForegroundColor = ConsoleColor.Yellow;
                                            Console.SetCursorPosition(2, h - 5);
                                            Console.Write("                                         ");
                                            if (playerHp <= 0)
                                            {
                                                //显示失败信息
                                                Console.SetCursorPosition(2, h - 5);
                                                Console.Write("你已经死亡,战斗失败!");
                                                gameOverInfo = "游戏失败";
                                            }
                                            else
                                            {
                                                Console.SetCursorPosition(2, h - 5);
                                                Console.Write("Boss对你造成{0}伤害,你剩余血量为:{1}", atk, playerHp);
                                            }
                                        }
                                        else
                                        {
                                            //擦除之前战斗信息
                                            Console.SetCursorPosition(2, h - 7);
                                            Console.Write("                                         ");
                                            Console.SetCursorPosition(2, h - 6);
                                            Console.Write("                                         ");
                                            Console.SetCursorPosition(2, h - 5);
                                            Console.Write("                                         ");
                                            //显示胜利信息
                                            Console.SetCursorPosition(2, h - 7);
                                            Console.Write("你战胜了Boss,快去营救公主");
                                            Console.SetCursorPosition(2, h - 6);
                                            Console.Write("请前往公主身边!");
                                            Console.SetCursorPosition(2, h - 5);
                                            Console.Write(" 按J键继续");
                                        }

                                    }
                                    
                                }
                                
                            }
                            //非战斗状态处理逻辑
                            else
                            {

                                #region 玩家移动相关
                                //擦除
                                Console.SetCursorPosition(playerX, playerY);
                                Console.Write("  ");
                                //改位置
                                switch (playerInput)
                                {
                                    case 'W':
                                    case 'w':
                                        --playerY;
                                        if (playerY < 1)
                                        {
                                            playerY = 1;
                                        }
                                        else if (playerX == bossX && playerY == bossY && bossHp > 0)
                                        {
                                            ++playerY;
                                        }
                                        else if (playerX == princessX && playerY == princessY && bossHp<=0)
                                        {
                                            ++playerY;
                                        }
                                        break;
                                    case 'A':
                                    case 'a':
                                        playerX -= 2;
                                        if (playerX < 2)
                                        {
                                            playerX = 2;
                                        }
                                        else if (playerX == bossX && playerY == bossY && bossHp > 0)
                                        {
                                            playerX += 2;
                                        }
                                        else if (playerX == princessX && playerY == princessY && bossHp <= 0)
                                        {
                                            playerX += 2;
                                        }
                                        break;
                                    case 'S':
                                    case 's':
                                        ++playerY;
                                        if (playerY > h - 9)
                                        {
                                            playerY = h - 9;
                                        }
                                        else if (playerX == bossX && playerY == bossY && bossHp > 0)
                                        {
                                            playerY--;
                                        }
                                        else if (playerX == princessX && playerY == princessY && bossHp <= 0)
                                        {
                                            playerY--;
                                        }
                                        break;
                                    case 'D':
                                    case 'd':
                                        playerX += 2;
                                        if (playerX > w - 4)
                                        {
                                            playerX = w - 4;
                                        }
                                        else if (playerX == bossX && playerY == bossY && bossHp > 0)
                                        {
                                            playerX -= 2;
                                        }
                                        else if (playerX == princessX && playerY == princessY && bossHp <= 0)
                                        {
                                            playerX -= 2;
                                        }
                                        break;
                                    case 'J':
                                    case 'j':
                                        //开始战斗
                                        if ((playerX == bossX && playerY == bossY - 1 ||
                                             playerX == bossX && playerY == bossY + 1 ||
                                             playerX == bossX - 2 && playerY == bossY ||
                                             playerX == bossX + 2 && playerY == bossY) && bossHp > 0)
                                        {
                                            isFight = true;
                                            //可以开始战斗
                                            Console.SetCursorPosition(2, h - 7);
                                            Console.ForegroundColor = ConsoleColor.White;
                                            Console.Write("开始和Boss战斗,按J键继续");
                                            Console.SetCursorPosition(2, h - 6);
                                            Console.Write("玩家当前血量{0}", playerHp);
                                            Console.SetCursorPosition(2, h - 5);
                                            Console.Write("Boss当前血量{0}", bossHp);

                                        }
                                        else if ((playerX == princessX && playerY == princessY - 1 ||
                                             playerX == princessX && playerY == princessY + 1 ||
                                             playerX == princessX - 2 && playerY == princessY ||
                                             playerX == princessX + 2 && playerY == princessY) && bossHp <= 0)
                                        {

                                            //改变场景ID
                                            nowSceneID = 3;
                                            gameOverInfo = "游戏通关";
                                            //跳出游戏界面的while循环回到主循环
                                            isOver = true;
                                            break;
                                        }
                                        break;
                                }
                                #endregion
                            }
                            //外层while循环逻辑
                            if (isOver)
                            {
                                break;
                            }
                        }
                        break;
                    //结束场景
                    case 3:
                        Console.Clear();
                        //标题的显示
                        Console.SetCursorPosition(w / 2 - 4, 5);
                        Console.ForegroundColor = ConsoleColor.White;
                        Console.Write("GameOver");
                        //可变内容的显示 根据失败或者成功 显示的内容不一样
                        Console.SetCursorPosition(w / 2 - 4, 5);
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.Write(gameOverInfo);

                        int nowSelEndIndex = 0;//结束界面索引
                        while (true)
                        {
                            bool isQuitEndWhile = false;

                            Console.SetCursorPosition(w / 2 - 6, 9);
                            Console.ForegroundColor = nowSelEndIndex == 0 ? ConsoleColor.Red : ConsoleColor.White;
                            Console.Write("回到开始界面");

                            Console.SetCursorPosition(w / 2 - 4, 11);
                            Console.ForegroundColor = nowSelEndIndex == 1 ? ConsoleColor.Red : ConsoleColor.White;
                            Console.Write("退出游戏");


                            char input = Console.ReadKey(true).KeyChar;

                            switch (input)
                            {
                                case 'W':
                                case 'w':
                                    --nowSelEndIndex;
                                    if(nowSelEndIndex<0)
                                    {
                                        nowSelEndIndex = 0;
                                    }
                                    break;
                                case 'S':
                                case 's':
                                    ++nowSelEndIndex;
                                    if (nowSelEndIndex >1)
                                    {
                                        nowSelEndIndex = 1;
                                    }
                                    break;
                                case 'J':
                                case 'j':
                                    if (nowSelEndIndex == 0)
                                    {
                                        nowSceneID = 1;
                                        isQuitEndWhile = true;
                                    }
                                    else
                                    {
                                        Environment.Exit(0);
                                    }
                                    break;
                            }
                            if(isQuitEndWhile)
                            {
                                break;
                            }
                        }
                        break;
                }
            }
            #endregion
        }
  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

四月的白羊座

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

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

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

打赏作者

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

抵扣说明:

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

余额充值