首页 > 编程语言 >c#控制台上开发的2D闯关小游戏

c#控制台上开发的2D闯关小游戏

时间:2023-07-22 09:11:31浏览次数:43  
标签:case break Console Player c# 2D int 小游戏 WriteLine

初学者可以看看  学习一下编程思想还有游戏思想.

好了 话不多说上代码大家自己看哈.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Threading;

namespace DevilDungeonCV

{

    class Program

    {

        static string[,] map = new string[20, 20];

        static List enemyList = new List();

        static List chestList = new List();

        static string msgbox = "";

        static bool isBattle = false, isDead = false;

        static Enemy tarEnemy = null;

        const int BOSS_LEVEL = 10;

        static Enemy boss = null;

        static int level = 0, enemyDizzy = 0, battleRound = 0;

        static void Main()

        {

            Console.SetBufferSize(77, 30);

            Console.SetWindowSize(77, 30);

            Console.CursorVisible = false;

            Start();

            while (true)

            {

                Update();

            }

        }

        static void Start()

        {

            Player.Reset();

            isDead = false;

            isBattle = false;

            tarEnemy = null;

            boss = new Enemy(0,0,true);

            level = 0;

            battleRound = 0;

            enemyDizzy = 0;

            msgbox = "欢迎来到恶魔地牢  (方向键移动)\n★ = 玩家\nθ = 道具\n︾ = 下一层\n<请按键分配属性点>\n1:力量 2:防御 3:敏捷 4:智力\n";

            MapGenerator();

        }

        static void Update()

        {

            PrintMap();

            StatusBar();

            MsgboxBar();

            BattleBar();

            Info();

            if (isDead)

            {

                ConfirmDead();

            }

            else if (boss==null)

            {

                MissionCompeleted();

            }

            else if (isBattle)

            {

                Battle(tarEnemy);

            }

            else

            {

                EnemyController();

                PlayerController();

            }

            Console.Clear();

        }

        static Enemy GetTargetEnemy(int x, int y, bool isBoss = false)

        {

            if (isBattle)

                return null;

            msgbox += "进入战斗\n";

            if (isBoss)

            {

                isBattle = true;

                battleRound = 0;

                enemyDizzy = 0;

                return boss;

            }

            for (int i = 0; i < enemyList.Count; i++)

            {

                Enemy enemy = enemyList[i];

                if (enemy.x == x && enemy.y == y)

                {

                    isBattle = true;

                    battleRound = 0;

                    enemyDizzy = 0;

                    return enemy;

                }

            }

            return null;

        }

        static void Battle(Enemy enemy)

        {

            const int ATTACK = 0, SKILL1 = 1, SKILL2 = 2, SKILL3 = 3, ESCAPE = 4;

            int action = ATTACK;

        c:

            ConsoleKey key = Console.ReadKey(true).Key;

            switch (key)

            {

                case ConsoleKey.Spacebar:

                    action = ATTACK;

                    break;

                case ConsoleKey.Q:

                    if (CanSpell(1) == "")

                        action = SKILL1;

                    else

                    {

                        msgbox += CanSpell(1);

                        MsgboxBar();

                        goto c;

                    }

                    break;

                case ConsoleKey.W:

                    if (CanSpell(2) == "")

                        action = SKILL2;

                    else

                    {

                        msgbox += CanSpell(2);

                        MsgboxBar();

                        goto c;

                    }

                    break;

                case ConsoleKey.E:

                    if (CanSpell(3) == "")

                        action = SKILL3;

                    else

                    {

                        msgbox += CanSpell(3);

                        MsgboxBar();

                        goto c;

                    }

                    break;

                case ConsoleKey.R:

                    action = ESCAPE;

                    break;

                default:

                    goto c;

            }

            battleRound++;

            msgbox += "<回合 " + battleRound + ">\n";

            MsgboxBar();

            //玩家动作

            switch (action)

            {

                case ATTACK:

                    int dmg = enemy.GetHurt(Player.atk, Player.dex);

                    switch (dmg)

                    {

                        case -1:

                            msgbox += enemy.name + " 闪避了攻击\n";

                            break;

                        case 0:

                            msgbox += enemy.name + " 格挡了伤害\n";

                            break;

                        default:

                            msgbox += enemy.name + " 受到 " + dmg + " 伤害\n";

                            break;

                    }

                    break;

                case SKILL1:

                    dmg = Event.rnd.Next(4, 7) + Player.Int;

                    msgbox += "施放<火球术> " + enemy.name + " 受到 " + dmg + " 伤害\n";

                    MsgboxBar();

                    enemy.life -= dmg;

                    Player.mana -= 3;

                    break;

                case SKILL2:

                    dmg = Event.rnd.Next(8, 13) + Player.Int * 3;

                    msgbox += "施放<治愈术> 回复 " + dmg + " 生命\n";

                    Player.life += dmg;

                    Player.mana -= 5;

                    break;

                case SKILL3:

                    dmg = Event.rnd.Next(1, 3) + Player.Int;

                    msgbox += "施放<眩晕术> " + enemy.name + " 眩晕 " + (dmg + 1) + " 回合\n";

                    MsgboxBar();

                    Player.mana -= 4;

                    enemyDizzy = dmg;

                    return;

                case ESCAPE:

                    if (Event.rnd.Next(0, 2) == 0)

                    {

                        isBattle = false;

                        msgbox += "<逃跑成功>\n";

                        return;

                    }

                    else

                    {

                        msgbox += "<逃跑失败>\n";

                    }

                    break;

            }

            MsgboxBar();

            //msgbox += "> ";

            if (enemy.life > 0)

            {

                if (enemyDizzy == 0)

                {

                    int dmg = Player.GetHurt(enemy.atk, enemy.dex);

                    switch (dmg)

                    {

                        case -1:

                            msgbox += "你闪避了攻击\n";

                            break;

                        case 0:

                            msgbox += "你格挡了伤害\n";

                            break;

                        default:

                            if (Player.life > 0)

                            {

                                msgbox += "你受到 " + dmg + " 伤害\n";

                            }

                            else

                            {

                                Player.life = 0;

                                msgbox += "你死了\n(按R键重新开始)\n";

                                MsgboxBar();

                                isDead = true;

                                return;

                            }

                            break;

                    }

                }

                else

                {

                    msgbox += enemy.name + " 眩晕中\n";

                    enemyDizzy--;

                }

            }

            else

            {

 

                msgbox += "敌人死亡\n";

                //如果敌人是BOSS

                if (enemy == boss)

                {

                    boss = null;

                    isBattle = false;

                }

                Player.exp += enemy.exp;

                if (Player.exp >= Player.lv * 2)

                {

                    msgbox += "<等级提升> (获得 1 属性点)\n";

                    MsgboxBar();

                    Player.exp -= Player.lv * 2;

                    Player.lv++;

                    Player.life += 8;

                    Player.mana += 8;

                    Player.ap++;

                }

                enemyList.Remove(enemy);

                isBattle = false;

            }

        }

        static bool isHeight(int _val)

        {

            for (int i = 0; i < 4; i++)

            {

                switch (i)

                {

                    case 0:

                        if (_val < Player.atk)

                            return false;

                        break;

                    case 1:

                        if (_val < Player.def)

                            return false;

                        break;

                    case 2:

                        if (_val < Player.dex)

                            return false;

                        break;

                    case 3:

                        if (_val < Player.Int)

                            return false;

                        break;

                }

            }

            return true;

        }

        static void StatusBar()

        {

            Console.SetCursorPosition(41, 0);

            Console.WriteLine("┌───────状态───────┐");

            Console.SetCursorPosition(41, 1);

            Console.WriteLine("│等级:    经验:    点数:    │");

            Console.ForegroundColor = ConsoleColor.White;

            Console.SetCursorPosition(49, 1); Console.Write(Player.lv);

            Console.ForegroundColor = ConsoleColor.Green;

            Console.SetCursorPosition(60, 1); Console.Write(Player.exp);

            Console.ForegroundColor = Player.ap >0 ? ConsoleColor.Yellow : ConsoleColor.DarkYellow;

            Console.SetCursorPosition(71, 1); Console.Write(Player.ap);

            Console.ForegroundColor = ConsoleColor.Gray;

            Console.SetCursorPosition(41, 2);

            Console.WriteLine("│生命:    力量:    防御:    │");

            Console.ForegroundColor = ConsoleColor.Red;

            Console.SetCursorPosition(49, 2); Console.Write(Player.life);

            Console.ForegroundColor = isHeight(Player.atk) ? ConsoleColor.Red :ConsoleColor.DarkRed;

            Console.SetCursorPosition(60, 2); Console.Write(Player.atk);

            Console.ForegroundColor = isHeight(Player.def) ? ConsoleColor.Yellow : ConsoleColor.DarkYellow;

            Console.SetCursorPosition(71, 2); Console.Write(Player.def);

            Console.ForegroundColor = ConsoleColor.Gray;

            Console.SetCursorPosition(41, 3);

            Console.WriteLine("│法力:    敏捷:    智力:    │");

            Console.ForegroundColor = ConsoleColor.Blue;

            Console.SetCursorPosition(49, 3); Console.Write(Player.mana);

            Console.ForegroundColor = isHeight(Player.dex) ? ConsoleColor.Magenta : ConsoleColor.DarkMagenta;

            Console.SetCursorPosition(60, 3); Console.Write(Player.dex);

            Console.ForegroundColor = isHeight(Player.Int) ? ConsoleColor.Cyan : ConsoleColor.DarkCyan;

            Console.SetCursorPosition(71, 3); Console.Write(Player.Int);

            Console.ForegroundColor = ConsoleColor.Gray;

            Console.SetCursorPosition(41, 4);

            Console.WriteLine("└────────────────┘");

            Console.WriteLine();

        }

        static void BattleBar()

        {

            if (!isBattle)

                return;

            int i = 5, l = 41;

            Console.SetCursorPosition(41, i);

            Console.WriteLine("┌──战斗──┐"); i++;

            Console.SetCursorPosition(l, i);

            Console.WriteLine("│敌人:      │"); Console.SetCursorPosition(l + 8, i); Console.Write(tarEnemy.name); i++;

            Console.SetCursorPosition(l, i);

            Console.WriteLine("│生命:      │"); Console.SetCursorPosition(l + 8, i); Console.Write(tarEnemy.life); i++;

            Console.SetCursorPosition(l, i);

            Console.WriteLine("│攻击:      │"); Console.SetCursorPosition(l + 8, i); Console.Write(tarEnemy.atk); i++;

            Console.SetCursorPosition(l, i);

            Console.WriteLine("│防御:      │"); Console.SetCursorPosition(l + 8, i); Console.Write(tarEnemy.def); i++;

            Console.SetCursorPosition(l, i);

            Console.WriteLine("│敏捷:      │"); Console.SetCursorPosition(l + 8, i); Console.Write(tarEnemy.dex); i++;

            Console.SetCursorPosition(l, i);

            Console.WriteLine("└──────┘");

            Console.WriteLine();

            //操作

            i = 5; l = 57;

            Console.SetCursorPosition(l, i);

            Console.WriteLine("┌───操作───┐"); i++;

            Console.SetCursorPosition(l, i);

            Console.WriteLine("│空格键:<攻击>  │"); i++;

            Console.SetCursorPosition(l, i);

            Console.WriteLine("│技能 Q:        │"); Console.SetCursorPosition(l + 10, i);

            if (Player.skill1) Console.Write("<火球术>"); else Console.Write("<无>"); i++;

            Console.SetCursorPosition(l, i);

            Console.WriteLine("│技能 W:        │"); Console.SetCursorPosition(l + 10, i);

            if (Player.skill2) Console.Write("<治愈术>"); else Console.Write("<无>"); i++;

            Console.SetCursorPosition(l, i);

            Console.WriteLine("│道具 E:        │"); Console.SetCursorPosition(l + 10, i);

            if (Player.skill3) Console.Write("<眩晕术>"); else Console.Write("<无>"); i++;

            Console.SetCursorPosition(l, i); i++;

            Console.WriteLine("│按键 R:<逃跑>  │");

            Console.SetCursorPosition(l, i);

            Console.WriteLine("└────────┘");

            Console.WriteLine();

        }

        static void MsgboxBar()

        {

            Console.SetCursorPosition(41, 12);

            Console.WriteLine("┌───────信息───────┐");

            for (int i = 1; i < 7; i++)

            {

                Console.SetCursorPosition(41, 12 + i);

                Console.WriteLine("│                                │");

            }

            Console.SetCursorPosition(41, 19);

            Console.WriteLine("└────────────────┘");

            List msgList = new List();

            msgList.Clear();

            int t = 0;

            for (int i = 0; i < msgbox.Length; i++)

            {

                i = msgbox.IndexOf("\n", i);

                if (i != -1)

                {

                    t++;

                }

            }

            //1\n2\n3\n4\n5\n6\n

            if (t > 6)

            {

                msgbox = msgbox.Substring(msgbox.IndexOf("\n") + 1, msgbox.Length - msgbox.IndexOf("\n") - 1);

            }

            string s = "";

            int p0 = 0, p1 = 0;

            for (int i = 0; i < 6; i++)

            {

                p1 = msgbox.IndexOf("\n", p0);

                if (p1 == -1)

                    break;

                s = p1 - p0 > 20 ? msgbox.Substring(p0, 19) + "..." : msgbox.Substring(p0, p1 - p0);

                p0 = p1 + 1;

                Console.SetCursorPosition(43, 13 + i);

                if (s.Contains("欢迎来到")|| s.Contains("按R键") || s.Contains("恭喜通关"))

                    Console.ForegroundColor = ConsoleColor.Cyan;

                else if (s.Contains("★"))

                    Console.ForegroundColor = ConsoleColor.Yellow;

                else if (s.Contains("θ") || s.Contains("等级提升"))

                    Console.ForegroundColor = ConsoleColor.DarkCyan;

                else if (s.Contains("︾"))

                    Console.ForegroundColor = ConsoleColor.DarkYellow;

                else if (s.Contains("进入战斗") || s.Contains("你死了") || s.Contains("您已击败恶魔"))

                    Console.ForegroundColor = ConsoleColor.DarkRed;

                else if (s.Contains("发现"))

                    Console.ForegroundColor = ConsoleColor.DarkCyan;

                else

                    Console.ForegroundColor = ConsoleColor.Gray;

                Console.WriteLine(s);

                Console.ForegroundColor = ConsoleColor.Gray;

            }

        }

        static void ConfirmDead()

        {

        c:

            ConsoleKey key = Console.ReadKey(true).Key;

            if (key == ConsoleKey.R)

                Start();

            else

                goto c;

        }

        static void MissionCompeleted()

        {

            msgbox += "恭喜通关\n您已击败恶魔,并成为了新的恶魔!\n";

            MsgboxBar();

            MsgboxBar();

            MsgboxBar();

        c:

            ConsoleKey key = Console.ReadKey(true).Key;

            if (key == ConsoleKey.R)

                Start();

            else

                goto c;

        }

        static void PlayerController()

        {

        c:

            int x = Player.x, y = Player.y;

            ConsoleKey key = Console.ReadKey(true).Key;

            switch (key)

            {

                case ConsoleKey.UpArrow:

                    y--;

                    break;

                case ConsoleKey.DownArrow:

                    y++;

                    break;

                case ConsoleKey.LeftArrow:

                    x--;

                    break;

                case ConsoleKey.RightArrow:

                    x++;

                    break;

                case ConsoleKey.D1:

                    AddAp(1); goto c;

                case ConsoleKey.D2:

                    AddAp(2); goto c;

                case ConsoleKey.D3:

                    AddAp(3); goto c;

                case ConsoleKey.D4:

                    AddAp(4); goto c;

                default:

                    goto c;

            }

            //越界判断

            if (x < 1 && x < map.GetLength(0) - 1 && y < 1 && y < map.GetLength(1) - 1)

                return;

            //碰撞判断

            Object obj = HitTest(x, y);

            switch (obj)

            {

                case Object.Wall:

                    //msgbox += "无作为\n";

                    goto c;

                case Object.Enemy:

                    //msgbox += "【进入战斗】\n";

                    tarEnemy = GetTargetEnemy(x, y);

                    break;

                case Object.Chest:

                    OpenChest(x, y);

                    break;

                case Object.Boss:

                    tarEnemy = GetTargetEnemy(0, 0,true);

                    break;

                case Object.Exit:

                    MapGenerator();

                    break;

                default:

                    msgbox += "玩家移动\n";

                    Player.x = x;

                    Player.y = y;

                    break;

            }

        }

        static void EnemyController()

        {

            for (int i = 0; i < enemyList.Count; i++)

            {

                Enemy enemy = enemyList[i];

                //移动部分

                int x = enemy.x, y = enemy.y;

                int r = Event.rnd.Next(3, 8);

                for (int _y = y - r; _y <= y + r; _y++)

                {

                    for (int _x = x - r; _x <= x + r; _x++)

                    {

                        //如果玩家在视野内

                        if (_x == Player.x && _y == Player.y)

                        {

                            //优先上下

                            if (Event.rnd.Next(0, 2) == 0)

                            {

                                if (Player.y < y)

                                    y--;

                                else if (Player.y > y)

                                    y++;

                                else if (Player.x < x)

                                    x--;

                                else if (Player.x > x)

                                    x++;

                            }

                            //优先左右

                            else

                            {

                                if (Player.x < x)

                                    x--;

                                else if (Player.x > x)

                                    x++;

                                else if (Player.y < y)

                                    y--;

                                else if (Player.y > y)

                                    y++;

                            }

                            goto c;

                        }

                    }

                }

                int dir = Event.rnd.Next(0, 4);

                switch (dir)

                {

                    case 0:

                        y--;

                        break;

                    case 1:

                        y++;

                        break;

                    case 2:

                        x--;

                        break;

                    case 3:

                        x++;

                        break;

                }

            c:

                Object obj = HitTest(x, y);

                switch (obj)

                {

                    case Object.Wall:

                        break;

                    case Object.Enemy:

                        break;

                    case Object.Player:

                        break;

                    case Object.Chest:

                        break;

                    case Object.Boss:

                        break;

                    case Object.Exit:

                        break;

                    default:

                        enemy.x = x;

                        enemy.y = y;

                        break;

                }

            }

        }

        static void AddAp(int att)

        {

            if (Player.ap <= 0)

                return;

            Player.ap--;

            switch (att)

            {

                case 1:

                    Player.atk++;

                    break;

                case 2:

                    Player.def++;

                    break;

                case 3:

                    Player.dex++;

                    break;

                case 4:

                    Player.Int++;

                    break;

            }

            msgbox += "<剩余属性点 " + Player.ap + " >\n";

            StatusBar();

            MsgboxBar();

        }

        static string CanSpell(int skill)

        {

            string msg = "";

            switch (skill)

            {

                case 1:

                    if (Player.skill1)

                    {

                        if (Player.mana < 3)

                        {

                            msg = "<法力不足>\n";

                        }

                    }

                    else

                        msg = "<尚未习得该技能>\n";

                    break;

                case 2:

                    if (Player.skill2)

                    {

                        if (Player.mana < 5)

                        {

                            msg = "<法力不足>\n";

                        }

                    }

                    else

                        msg = "<尚未习得该技能>\n";

                    break;

                case 3:

                    if (Player.skill3)

                    {

                        if (Player.mana < 4)

                        {

                            msg = "<法力不足>\n";

                        }

                    }

                    else

                        msg = "<尚未习得该技能>\n";

                    break;

            }

            return msg;

        }

        static void OpenChest(int x, int y)

        {

            for (int i = 0; i < chestList.Count; i++)

            {

                Chest chest = chestList[i];

                if (chest.x == x && chest.y == y)

                {

                    msgbox += "发现 " + chest.treasure + " ";

                    switch (chest.treasure)

                    {

                        case "生命药水":

                            int val = Event.rnd.Next(5, 11);

                            msgbox += "增加 " + val + " 生命";

                            Player.life += val;

                            break;

                        case "法力药水":

                            val = Event.rnd.Next(5, 11);

                            msgbox += "增加 " + val + " 法力";

                            Player.mana += val;

                            break;

                        case "短剑":

                            val = Event.rnd.Next(1, 4);

                            msgbox += "增加 " + val + " 力量";

                            Player.atk += val;

                            break;

                        case "盔甲":

                            val = Event.rnd.Next(1, 3);

                            msgbox += "增加 " + val + " 防御";

                            Player.def += val;

                            break;

                        case "火球术卷轴":

                            msgbox += "学会了 火球术";

                            Player.skill1 = true;

                            break;

                        case "治愈术卷轴":

                            msgbox += "学会了 治愈术";

                            Player.skill2 = true;

                            break;

                        case "眩晕术卷轴":

                            msgbox += "学会了 眩晕术";

                            Player.skill3 = true;

                            break;

                    }

                    msgbox += "\n";

                    chestList.Remove(chest);

                }

            }

        }

        enum Object

        {

            Ground, Wall, Enemy, Player, Chest, Boss, Exit

        };

        static Object HitTest(int x, int y)

        {

            if (map[x, y] == "□")

            {

                return Object.Wall;

            }

            if (x == map.GetLength(0) - 2 && y == map.GetLength(1) - 1)

            {

                return Object.Exit;

            }

            //敌人

            for (int i = 0; i < enemyList.Count; i++)

            {

                Enemy enemy = enemyList[i];

                if (x == enemy.x && y == enemy.y)

                {

                    return Object.Enemy;

                }

            }

            //宝箱

            for (int i = 0; i < chestList.Count; i++)

            {

                Chest chest = chestList[i];

                if (x == chest.x && y == chest.y)

                {

                    return Object.Chest;

                }

            }

            if (x == Player.x && y == Player.y)

            {

                return Object.Player;

            }

            //BOSS

            if(level == BOSS_LEVEL)

            {

                if(x>=8 && x<=11 && y>=8 && y <= 11)

                {

                    if (!(x == 8 && y == 8) && !(x == 11 && y == 8) && !(x == 11 && y == 9) && !(x == 11 && y == 11))

                        return Object.Boss;

                }

            }

            return Object.Ground;

        }

        static void PrintMap()

        {

            for (int y = 0; y < map.GetLength(1); y++)

            {

                for (int x = 0; x < map.GetLength(0); x++)

                {

                    if (x == Player.x && y == Player.y)

                    {

                        Console.ForegroundColor = ConsoleColor.Yellow;

                        Console.Write("★");

                        continue;

                    }

                    //敌人列表

                    for (int i = 0; i < enemyList.Count; i++)

                    {

                        Enemy enemy = enemyList[i];

                        if (x == enemy.x && y == enemy.y)

                        {

                            Console.ForegroundColor = ConsoleColor.DarkMagenta;

                            Console.Write(enemy.display);

                            goto w;

                        }

                    }

                    //宝箱列表

                    for (int i = 0; i < chestList.Count; i++)

                    {

                        Chest chest = chestList[i];

                        if (x == chest.x && y == chest.y)

                        {

                            Console.ForegroundColor = ConsoleColor.DarkCyan;

                            Console.Write("θ");

                            goto w;

                        }

                    }

                    //BOSS

                    if (level == BOSS_LEVEL)

                    {

                        map[9, 8] = "△"; map[10, 8] = "△";

                        map[8, 9] = "ψ"; map[9, 9] = "╲"; map[10, 9] = "╱";

                        map[8, 10] = "|"; map[9, 10] = ")"; map[10, 10] = "("; map[11, 10] = "w ";

                        map[8, 11] = "|"; map[9, 11] = "╱"; map[10, 11] = "╲";

                    }

                    //BOSS颜色渲染

                    if (level == BOSS_LEVEL && x >= 8 && x <= 11 && y >= 8 && y <= 11)

                        Console.ForegroundColor = ConsoleColor.Red;

                    else if (x == map.GetLength(0) - 2 && y == map.GetLength(1) - 1)

                        Console.ForegroundColor = ConsoleColor.DarkYellow;

                    else

                        Console.ForegroundColor = ConsoleColor.Gray;

                    Console.Write(map[x, y]);

                    w:;

                }

                Console.WriteLine();

            }

            if (isDead)

            {

                Console.SetCursorPosition(0, 0);

                Console.WriteLine("□□□□□□□□□□□□□□□□□□□□");

                Console.WriteLine("□□□□□□□□□□□□□□□□□□□□");

                Console.WriteLine("□□□□□                    □□□□□");

                Console.WriteLine("□□□□  □□□□□□□□□□  □□□□");

                Console.WriteLine("□□□  □□□□□□□□□□□□  □□□");

                Console.WriteLine("□□  □□□□□□□□□□□□□□  □□");

                Console.WriteLine("□□  □□□□□□□□□□□□□□  □□");

                Console.WriteLine("□□  □□  □  □□□□  □  □□  □□");

                Console.WriteLine("□□  □□□  □□□□□□  □□□  □□");

                Console.WriteLine("□□  □□  □  □□□□  □  □□  □□");

                Console.WriteLine("□□  □□□□□□□□□□□□□□  □□");

                Console.WriteLine("□□  □□□□□□□□□□□□□□  □□");

                Console.WriteLine("□□  □□□□□□□□□□□□□□  □□");

                Console.WriteLine("□□  □□□□□        □□□□□  □□");

                Console.WriteLine("□□  □□□□  □□□□  □□□□  □□");

                Console.WriteLine("□□□  □□□□□□□□□□□□  □□□");

                Console.WriteLine("□□□□  □□□□□□□□□□  □□□□");

                Console.WriteLine("□□□□□                    □□□□□");

                Console.WriteLine("□□□□□□□□□□□□□□□□□□□□");

                Console.WriteLine("□□□□□□□□□□□□□□□□□□□□");

                return;

            }

            if (boss==null)

            {

                Console.SetCursorPosition(0, 0);

                Console.WriteLine("□□□□□□□□□□□□□□□□□□□□");

                Console.WriteLine("□□□□□□□□□□□□□□□□□□□□");

                Console.WriteLine("□□□□□                    □□□□□");

                Console.WriteLine("□□□□  □□□□□□□□□□  □□□□");

                Console.WriteLine("□□□  □□□□□□□□□□□□  □□□");

                Console.WriteLine("□□  □□□□□□□□□□□□□□  □□");

                Console.WriteLine("□□  □□□□□□□□□□□□□□  □□");

                Console.WriteLine("□□  □□□  □□□□□□  □□□  □□");

                Console.WriteLine("□□  □□  □  □□□□  □  □□  □□");

                Console.WriteLine("□□  □□□□□□□□□□□□□□  □□");

                Console.WriteLine("□□  □□□□□□□□□□□□□□  □□");

                Console.WriteLine("□□  □□□□□□□□□□□□□□  □□");

                Console.WriteLine("□□  □□□□□□□□□□□□□□  □□");

                Console.WriteLine("□□  □□□□  □□□□  □□□□  □□");

                Console.WriteLine("□□  □□□□□        □□□□□  □□");

                Console.WriteLine("□□□  □□□□□□□□□□□□  □□□");

                Console.WriteLine("□□□□  □□□□□□□□□□  □□□□");

                Console.WriteLine("□□□□□                    □□□□□");

                Console.WriteLine("□□□□□□□□□□□□□□□□□□□□");

                Console.WriteLine("□□□□□□□□□□□□□□□□□□□□");

                return;

            }

            Console.SetCursorPosition(0, 0);

            Console.Write("第 " + level + " 层");

        }

        static void MapGenerator()

        {

            level++;

            Player.x = 1; Player.y = 1;

            enemyList.Clear();

            chestList.Clear();

            for (int y = 0; y < map.GetLength(1); y++)

            {

                for (int x = 0; x < map.GetLength(0); x++)

                {

                    //边界

                    if (x == 0 || y == 0 || x == map.GetLength(0) - 1 || y == map.GetLength(1) - 1)

                    {

                        map[x, y] = "□";

                        if (x == map.GetLength(0) - 2 && y == map.GetLength(1) - 1 && level != BOSS_LEVEL)

                        {

                            map[x, y] = "︾";

                        }

                    }

                    else

                    {

                        if (level == BOSS_LEVEL)

                        {

                            map[x, y] = "  ";

                            if (y == 5 || y == 14)

                            {

                                if (x >= 5 && x <= 14 && x != 9 && x != 10)

                                {

                                    map[x, y] = "□";

                                }

                            }

                            else if (x == 14 || x == 5)

                            {

                                if (y >= 5 && y <= 14 && y != 9 && y != 10)

                                {

                                    map[x, y] = "□";

                                }

                            }

                            continue;

                        }

                        if (Event.rnd.Next(0, 8) == 0 && x != 1 && y != 1 && x != map.GetLength(0) - 2 && y != map.GetLength(1) - 2)

                        {

                            map[x, y] = "□" ;

                        }

                        else

                        {

                            map[x, y] = "  ";

                        }

 

                        //生成敌人

                        if (map[x, y] == "  ")

                        {

                            if (Event.rnd.Next(0, 20) == 0 && x != 1 && y != 1 && x != map.GetLength(0) - 2 && y != map.GetLength(1) - 2)

                            {

                                enemyList.Add(new Enemy(x, y));

                                continue;

                            }

                            if (Event.rnd.Next(0, 80) == 0 && x != 1 && y != 1 && x != map.GetLength(0) - 2 && y != map.GetLength(1) - 2)

                            {

                                chestList.Add(new Chest(x, y));

                                continue;

                            }

                        }

                    }

                }

            }

        }

        static void Info()

        {

            Console.SetCursorPosition(55, 20);

            Console.WriteLine("Developed by Linus C.");

        }

    }

    class Event

    {

        public static Random rnd = new Random();

    }

    class Player

    {

        public static int lv = 1, exp = 0, ap = 3, x = 1, y = 1, life = 20, mana = 10;

        public static bool skill1 = false, skill2 = false, skill3 = false;

        private static int _atk, _def, _dex, _int;

        public static int atk { get { return _atk; } set { _atk = value; } }

        public static int def { get { return _def; } set { _def = value; } }

        public static int dex { get { return _dex; } set { _dex = value; } }

        public static int Int { get { return _int; } set { _int = value; } }

        public static void Reset()

        {

            lv = 1; exp = 0; ap = 3; x = 1; y = 1;

            life = 20; mana = 10;

            skill1 = false; skill2 = false; skill3 = false;

            _atk = 2; _def = 1; _dex = 1; _int = 0;

        }

        //受伤判断

        public static int GetHurt(int _atk, int _dex)

        {

            Random rnd = Event.rnd;

            _atk = _atk + rnd.Next(0, (int)(_atk * 0.5f));

            int mrate = 0;

            //对方敏捷-我方敏捷 

            int dex0 = _dex - dex;

            //如果对方敏捷高,从0~dex 为正值,否则 -dex~1

            if (dex0 > 0)

                mrate = Event.rnd.Next(0, dex0);

            else

                mrate = Event.rnd.Next(dex0, 1);

            //如果随机数为负数

            if (mrate < 0)

                _atk = -1;

            else if (mrate > 0)

                _atk = _atk - def > 0 ? _atk - def : Event.rnd.Next(0, 2);

            //如果随机数为0 则1/2几率闪避

            else

            {

                if (Event.rnd.Next(0, 2) == 0)

                    _atk = -1;

                else _atk = _atk - def > 0 ? _atk - def : Event.rnd.Next(0, 2);

            }

            life -= _atk > 0 ? _atk : 0;

            return _atk;

        }

    }

    class Enemy

    {

        public string name;

        public char display;

        public int x, y, life, atk, def, dex, exp;

        public Enemy(int _x, int _y,bool isBoss=false)

        {

            x = _x;

            y = _y;

            int r = Event.rnd.Next(0, 6);

            if (isBoss)

                r = 6;

            switch (r)

            {

                case 0:

                    display = '鼠';

                    name = "老鼠";

                    life = 5;

                    atk = 2;

                    def = 1;

                    dex = 0;

                    exp = 1;

                    break;

                case 1:

                    display = '蝙';

                    name = "蝙蝠";

                    life = 4;

                    atk = 2;

                    def = 1;

                    dex = 2;

                    exp = 1;

                    break;

                case 2:

                    display = '蛛';

                    name = "蜘蛛";

                    life = 3;

                    atk = 1;

                    def = 0;

                    dex = 4;

                    exp = 1;

                    break;

                case 3:

                    display = '蛇';

                    name = "蛇";

                    life = 5;

                    atk = 3;

                    def = 1;

                    dex = 3;

                    exp = 2;

                    break;

                case 4:

                    display = '幽';

                    name = "幽灵";

                    life = 5;

                    atk = 3;

                    def = 4;

                    dex = 2;

                    exp = 2;

                    break;

                case 5:

                    display = '骷';

                    name = "骷髅";

                    life = 8;

                    atk = 4;

                    def = 3;

                    dex = 2;

                    exp = 3;

                    break;

                case 6:

                    display = '王';

                    name = "恶魔";

                    life = 30;

                    atk = 8;

                    def = 6;

                    dex = 5;

                    exp = 20;

                    break;

            }

        }

        public int GetHurt(int _atk, int _dex)

        {

            Random rnd = Event.rnd;

            _atk = _atk + rnd.Next(0, (int)(_atk * 0.5f));

            int mrate = 0;

            //对方敏捷-我方敏捷 

            int dex0 = _dex - dex;

            //如果对方敏捷高,从0~dex 为正值,否则 -dex~1

            if (dex0 > 0)

                mrate = Event.rnd.Next(0, dex0);

            else

                mrate = Event.rnd.Next(dex0, 1);

            //如果随机数为负数

            if (mrate < 0)

                _atk = -1;

            else if (mrate > 0)

                _atk = _atk - def > 0 ? _atk - def : Event.rnd.Next(0, 2);

            //如果随机数为0 则1/2几率闪避

            else

            {

                if (Event.rnd.Next(0, 2) == 0)

                    _atk = -1;

                else _atk = _atk - def > 0 ? _atk - def : Event.rnd.Next(0, 2);

            }

            life -= _atk > 0 ? _atk : 0;

            return _atk;

        }

    }

    class Chest   

    {

        public int x, y;

        public string treasure;

        public string[] treasureList = { "生命药水", "法力药水", "短剑", "盔甲", "火球术卷轴", "治愈术卷轴", "眩晕术卷轴" };

        public Chest(int _x, int _y)

        {

            x = _x;

            y = _y;

            int rnd = Event.rnd.Next(0, 185);

            if (rnd < 15)

                treasure = treasureList[6];

            else if (rnd >= 15 && rnd < 30)

                treasure = treasureList[5];

            else if (rnd >= 30 && rnd < 45)

                treasure = treasureList[4];

            else if (rnd >= 45 && rnd < 65)

                treasure = treasureList[3];

            else if (rnd >= 65 && rnd < 85)

                treasure = treasureList[2];

            else if (rnd >= 85 && rnd < 135)

                treasure = treasureList[0];

            else

                treasure = treasureList[1];

        }

    }

}

标签:case,break,Console,Player,c#,2D,int,小游戏,WriteLine
From: https://www.cnblogs.com/yeci/p/17572839.html

相关文章

  • 【雕爷学编程】Arduino动手做(52)---MicroSD卡读写模块4
    37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的。鉴于本人手头积累了一些传感器和执行器模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里准备逐一动手试试多做实验,不管成功与否,都会记录下来——小小的进步或是搞不掂的问题......
  • ERROR 1709 (HY000): Index column size too large. The maximum column size is 767
    MySQL版本5.6.35在一个长度为512字符的字段上创建uniquekey报错CREATEDATABASEdpcs_metadataDEFAULTCHARACTERSETutf8;select*frominformation_schema.SCHEMATA;+--------------+--------------------+----------------------------+------------------------+---......
  • 使用 Dockerfile 构建生产环境镜像
    传统部署的坑:1202年了,如果你连Docker都不知道是什么,我建议买一本书看看——或者谷歌一下,博客已经写烂了。为什么有这篇文章,是因为我在真正做容器化改造的时候,发现公司生产环境存在大量的坑:传统虚拟机部署,基本依赖克隆或者手工编译。由于人力原因,SRE历来单传,编译出来的PHP......
  • m基于扩频解扩+LDPC编译码的通信链路matlab误码率仿真,调制对比QPSK,16QAM,64QAM,扩频
    1.算法仿真效果matlab2022a仿真结果如下:     2.算法涉及理论知识概要      在现代通信系统中,扩频技术被广泛应用于数字通信链路中。扩频技术通过将要传输的信息序列与一个宽带的伪随机码序列进行卷积,将原始信号转换成一个具有更大带宽的扩频信号。在接收端......
  • 字典dict转字符串
    在Python中,可以使用不同的方法将字典转换为字符串。以下是几种常用的方法:使用str()函数:emy_dict={'key1':'value1','key2':'value2','key3':'value3'}dict_str=str(my_dict)print(dict_str)#输出:{'key1':'value......
  • mac配置初体验
    安装JDK进入azul下载对应版本、安装https://www.azul.com/downloads/查看安装路径/usr/libexec/java_home-V###输出以下内容MatchingJavaVirtualMachines(2):16.0.2(arm64)"AzulSystems,Inc."-"Zulu16.32.15"/Users/xiaoming/Library/Java/JavaVirt......
  • Zbox loves stack
    Zboxlovesstack题意有\(n\)个栈,\(q\)次操作,\(3\)种操作。1.\([l,r]\)之间的栈全部加入一个数\(k\)。2.\([l,r]\)之间的栈全部弹出栈顶。3.第\(s\)个栈中的第\(k\)个元素,栈顶为第一个元素,没有则输出Error。\(n\leq10^6,q\leq10^5\)题解考试的时候想到了......
  • m基于Costas环的QPSK载波同步matlab性能仿真,对比不同环路系数等对载波同步的影响
    1.算法仿真效果matlab2022a仿真结果如下:       2.算法涉及理论知识概要        在数字通信中,载波同步是保证正常数据传输的重要环节之一。Costas环是一种常用的基于相位差检测的载波同步方法,适用于QPSK调制信号的同步。本文将介绍基于Costas环的QPSK......
  • 欧姆龙CX系列PLC串口转以太网欧姆龙cp1hplc以太网连接电脑
    你是否还在为工厂设备信息采集困难而烦恼?捷米特JM-ETH-CX转以太网通讯处理器为你解决这个问题!捷米特JM-ETH-CX转以太网通讯处理器专门为满足工厂设备信息化需求而设计,可以用于欧姆龙多个系列PLC的太网数据采集,非常方便构建生产管理系统。而且,该处理器采用模块化设计,不占用PLC编程......
  • m基于Costas环的QPSK载波同步matlab性能仿真,对比不同环路系数等对载波同步的影响
    1.算法仿真效果matlab2022a仿真结果如下:2.算法涉及理论知识概要在数字通信中,载波同步是保证正常数据传输的重要环节之一。Costas环是一种常用的基于相位差检测的载波同步方法,适用于QPSK调制信号的同步。本文将介绍基于Costas环的QPSK载波同步方法,并比较不同环路系数对载波同步......