面向对象的文字游戏
Players.cs
using System;namespace _7._21{class Player{//名称public string name = Game.givename;//public string Name { get; }//最大血量public int maxHp = Tools.Random(90, 110);//成长血量public int g_Hp = Tools.Random(90, 110);//当前血量private int currentHp = 0;//属性public int CurrentHp{set{currentHp = value < 0 ? 0 : value ;currentHp = value > maxHp ? maxHp : value;}get{return currentHp;}}//攻击力public int atk = Tools.Random(10, 15);//成长攻击力public int g_atk = Tools.Random(10, 15);//速度public int speed = Tools.Random(10, 15);//成长速度public int g_speed = Tools.Random(10, 15);//闪避public int miss = Tools.Random(5, 10);//暴击public int satk = Tools.Random(5, 10);//经验public int exp = 0;//等级 n*exppublic int level = 1;//钱public int money = 0;public Player(){CurrentHp = maxHp;Debug.Log(\"角色创建成功!\", ConsoleColor.Blue);}//显示信息public void ShowInfo(){string str = string.Format(\"{0}当前属性:\", name);Debug.Log(str, ConsoleColor.Green);str = string.Format(\"血量:{0}/{1},攻击:{2},速度:{3},暴击:{4},闪避:{5}\", currentHp, maxHp, atk, speed, satk, miss);Debug.Log(str, ConsoleColor.Green);str = string.Format(\"等级:{0},经验:{1}/{2},金钱:{3}\", level, exp, level * 100, money);Debug.Log(str, ConsoleColor.Green);}//增加经验public bool AddExp(int exp){this.exp += exp;bool isLevelUp = false;//判断升级while(this.exp>=100*level){this.exp -= 100 * level;level++;//增加属性maxHp += g_Hp;atk += g_atk;speed += g_speed;//血量回满currentHp = maxHp;//打印Debug.Log(\"恭喜等级提升为\" + level + \"级!\", ConsoleColor.Yellow, 0);ShowInfo();isLevelUp = true;}return isLevelUp;}//增加金钱public void AddMoney(int money){this.money += money;}//减少金钱public bool SubMoney(int money){if(this.money >=money){this.money -= money;return true;}return false;}public void AddAtk(int atk){this.atk += atk;}//攻击public bool Attack(Enemy enemy){return enemy.GetAttack(atk);}//受到攻击public bool GetAttack(Enemy enemy){//判断是否命中if(Tools.Random(0, 100) < miss){Debug.Log(\"你躲过了\" + enemy.name + \"的攻击\");return false;}//判断是否暴击int num = enemy.atk;if(Tools.Random(0, 100) < enemy.satk){num = enemy.atk * 2;}CurrentHp -= num;Debug.Log(\"你受到\" + enemy.name + num+\"点伤害。\");if (CurrentHp <= 0){//死亡Debug.Log(\"你被\" + enemy.name + \"无情的杀死了!\");return true;}return false;}}}
Enemy.cs
namespace _7._21{class Enemy{public int id = 0;public string name = \"I型崩坏兽\";public int hp = 30;public int atk = 20;public int speed = 10;public int satk = 5;public int miss = 5;public int exp = 50;public int money = 5;//攻击public bool Attack(){return Game.player.GetAttack(this);}//受到攻击public bool GetAttack(int damage){if(Tools .Random(0, 100) < miss){Debug.Log(name + \"躲过了你的攻击\");return false;}if(Tools.Random(0, 100) < Game.player.satk){damage *= 2;}hp -= damage;Debug.Log(\"你对\" + name + \"造成了\" + damage + \"点伤害\");bool res = false;if (hp <= 0){res = true;Debug.Log(\"你击败了\" + name + \"!经验+\" + exp + \",金钱+\" + money);Game.player.AddExp(exp);Game.player.AddMoney(money);}return res;}}}
Tools.cs
using System;namespace _7._21{class Tools{public static int Random(int min,int max){return new Random().Next(min, max + 1);}}}
Debug.cs
using System;using System.Threading;namespace _7._21{//输出调试类class Debug{//输出public static void Log(string text){Console.WriteLine(text);Thread.Sleep(1000);}public static void Log(int a){Console.WriteLine(a);Thread.Sleep(1000);}public static void Log(){for (int a = 0; a < 6; a++){Console.Write(\"·\");Thread.Sleep(1000);}Console.WriteLine();}//颜色输出public static void Log(string text,ConsoleColor color){Console.ForegroundColor = color;Console.WriteLine(text);Thread.Sleep(1000);Console.ForegroundColor = ConsoleColor.White;}//输出public static void Log(string text,int time){Console.WriteLine(text);Thread.Sleep(time);}//颜色输出public static void Log(string text, ConsoleColor color, int time){Console.ForegroundColor = color;Console.WriteLine(text);Thread.Sleep(time);Console.ForegroundColor = ConsoleColor.White;}}}
program.cs
namespace _7._21{class Program{static void Main(string[] args){//实例化GameGame game = new Game();//运行游戏game.Run();}}}
Game.cs
using System;namespace _7._21{class Game{//创建玩家public static Player player;public static string givename;public void Start(){while (true){//创建玩家Debug.Log(\"输入你的角色昵称:\", ConsoleColor.Blue);givename = Console.ReadLine();player = new Player();//执行评分内容PingFen pingfen = new PingFen();pingfen.Run();Debug.Log(\"1:开始游戏,2:重新创建\");string str = Console.ReadLine();if (str == \"1\"){break;}}Debug.Log(\"************************************\", ConsoleColor.Cyan);}//运行游戏public void Run(){//创建角色Start();//执行研究室内容YanJiuShi yjs = new YanJiuShi();yjs.Run();//执行迷宫内容MiGong migong = new MiGong();migong.Run();migong.Run2();//执行花海内容FlowerSea flowersea = new FlowerSea();flowersea.enemyStep = 5;flowersea.Run();}}}
PingFen.cs
namespace _7._21{class PingFen{public void Run(){int num1 = Game.player.maxHp + Game.player.atk + Game.player.speed + Game.player.satk + Game.player.miss;int num2 = Game.player.g_Hp + Game.player.g_atk + Game.player.g_speed;Debug.Log(\"您的角色初始属性评分为:\"+num1+\"(初始范围:120-160)\");Debug.Log(\"您的角色初始天赋评分为:\" + num2+\"(初始范围:110-140)\");}}}
YanJiuShi.cs
using System;namespace _7._21{class YanJiuShi{public static NPC npc = new NPC();//研究室public void Run(){Debug.Log(\"哈…………\");Debug.Log(\"这一觉睡得可真舒服呀,咦,我这是在哪儿?\");Debug.Log();Debug.Log(\"这里好像是一个研究室。\");while (true){Debug.Log(\"1:查看你的属性,2:导航精灵派蒙,3:走出研究室\");string s = Console.ReadLine();if (s == \"1\"){Game.player.ShowInfo();}else if (s == \"2\"){npc.Chat();}else{break;}}}}}
NPC.cs
namespace _7._21{class NPC{public string name = \"派蒙\";//是否对话过public bool isChat = false;//对话public void Chat(){if (isChat == false){isChat = true;Debug.Log(\"“你就是总部新派来的实习生吗?这里很危险,不要乱走动。”\");Debug.Log(\"导航精灵派蒙说道:“我的名字叫做派蒙,你初来乍到,这是20金币,你先拿着。”\");//给钱Debug.Log(\"你获得了20金币\");Game.player.AddMoney(20);Game.player.ShowInfo();}else{Debug.Log(\"派蒙:‘你又来了呀,我没有钱了哦。’\");}}}}
MiGong.cs
using System;namespace _7._21{class MiGong{public void Run(){Debug.Log(\"这的路怎么这么复杂呀,看来只能试试运气了。\");int x = 1, y = 1;//记录小人初始位置int[,] map = new int[20, 20]{{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},{1,2,1,0,0,0,0,1,0,0,0,1,0,1,0,0,0,1,0,1},{1,0,1,0,1,1,0,0,0,1,0,0,0,0,0,1,0,1,0,1},{1,0,1,0,1,0,0,1,0,1,0,1,1,0,1,0,0,0,0,1},{1,0,1,0,1,0,1,0,1,0,0,1,1,1,1,1,1,1,0,1},{1,0,1,0,0,0,0,0,1,0,1,1,0,0,0,0,0,0,0,1},{1,0,0,0,1,0,1,0,1,0,0,1,0,1,1,0,1,0,1,1},{1,1,1,1,1,0,1,0,1,0,1,1,0,1,0,0,0,0,1,1},{1,0,0,0,0,0,0,0,1,0,1,0,0,1,0,1,1,0,0,1},{1,1,1,1,1,1,1,1,1,0,1,0,0,1,0,1,1,1,0,1},{1,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,1,0,1},{1,0,1,0,1,1,1,1,1,1,0,0,0,1,1,0,1,1,0,1},{1,0,0,1,0,0,0,1,0,0,1,1,0,1,0,0,0,0,0,1},{1,0,1,1,0,1,0,1,0,1,0,1,0,1,1,1,1,0,1,1},{1,0,0,1,0,1,0,1,0,1,0,1,0,1,0,0,0,0,0,1},{1,0,1,0,0,0,0,0,0,1,0,1,0,1,0,1,0,1,0,1},{1,0,0,0,1,0,1,0,1,1,0,1,0,1,0,1,0,1,0,1},{1,0,1,1,0,0,0,0,0,0,1,1,0,1,0,0,1,1,1,1},{1,0,0,0,0,1,1,1,1,0,0,0,0,1,0,0,0,0,3,1},{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};//打印初始图for (int i = 0; i < 20; i++){for (int j = 0; j < 20; j++){if (map[i, j] == 0){Console.Write(\" \");}else if (map[i, j] == 1){Console.Write(\"■\");}else if (map[i, j] == 2){Console.Write(\"♀\");}else if (map[i, j] == 3){Console.Write(\"→\");}}Console.WriteLine();}//在键盘接受指令,对指令分析运算,输出数据while (true)//无数次执行循环体{ConsoleKeyInfo s = Console.ReadKey();//在键盘接受指令int t = map[x, y];if (s.Key.ToString() == \"RightArrow\")//若接受指令为“→”,{if (map[x, y + 1] != 1){map[x, y] = map[x, y + 1];map[x, y + 1] = t;y++;}}else if (s.Key.ToString() == \"LeftArrow\"){if (map[x, y - 1] != 1){map[x, y] = map[x, y - 1];map[x, y - 1] = t;y--;}}else if (s.Key.ToString() == \"UpArrow\"){if (map[x - 1, y] != 1){map[x, y] = map[x - 1, y];map[x - 1, y] = t;x--;}}else if (s.Key.ToString() == \"DownArrow\"){if (map[x + 1, y] != 1){map[x, y] = map[x + 1, y];map[x + 1, y] = t;x++;}}Console.Clear();for (int i = 0; i < 20; i++){for (int j = 0; j < 20; j++){if (map[i, j] == 0){Console.Write(\" \");}else if (map[i, j] == 1){Console.Write(\"■\");}else if (map[i, j] == 2){Console.Write(\"♀\");}else if (map[i, j] == 3){Console.Write(\"→\");}}Console.WriteLine();}if (map[18, 18] == 2){break;}}}public void Run2(){Debug.Log(\"终于走出迷宫了,哇,这里的景色真的好美!\");int[,] map = new int[20, 20]{{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};//打印初始图for (int i = 0; i < 20; i++){for (int j = 0; j < 20; j++){if (map[i, j] == 0){Console.Write(\"★\");}else if (map[i, j] == 1){Console.Write(\"■\");}else if (map[i, j] == 2){Console.Write(\"♀\");}}Console.WriteLine();}}}}
FlowerSea.cs
using System;namespace _7._21{class FlowerSea{public static NPC npc = new NPC();public int enemyStep;public int currentStep;public void Run(){if (enemyStep <= 0){while (true){Debug.Log(\"1:查看你的属性,2:导航精灵派蒙,3:走出花海\");string s = Console.ReadLine();if (s == \"1\"){Game.player.ShowInfo();}else if (s == \"2\"){npc.Chat();}else{break;}}}else{while (true){Debug.Log(\"按任意键移动\",ConsoleColor.Red);Console.ReadKey();currentStep++;if (currentStep >= Tools.Random(enemyStep - 2, enemyStep + 2)){currentStep = 0;//遇敌Debug.Log(\"你遇到了I型崩坏兽。\");Game.player.ShowInfo();Debug.Log(\"1:消耗5金币回满血量,2:跳过\");string str = Console.ReadLine();if (str == \"1\"&&Game.player.money>=5){Game.player.money -= 5;Game.player.CurrentHp = Game.player.maxHp;Game.player.ShowInfo();}Enemy enemy = new Enemy();bool isPlayerAtk = Game.player.speed > enemy.speed;while (true){if (isPlayerAtk){//如果玩家出手isPlayerAtk = false;bool res = Game.player.Attack(enemy);if (res == true){break;}}else{//僵尸出手isPlayerAtk = true;bool res = enemy.Attack();if (res == true){Debug.Log(\"游戏失败!\", ConsoleColor.Red);Environment.Exit(0);}}}}}}}}}