c#第十三课面向对象的文字游戏2.0
2020-07-25 16:07
951 查看
A.cs
namespace _7._24 { class A:Player { public A() { name = "A"; maxHp = 100; g_Hp = 20; atk = 50; g_Atk = 10; speed = 100; g_Speed = 20; sAtk = 80; miss = 10; } } }
A1.cs
namespace _7._24 { class A1:Enemy { public A1() { Go("A1", 700, 10, 50, 10, 10, 10, 100); } } }
B.cs
namespace _7._24 { class B : Player { public B() { name = "B"; maxHp = 100; g_Hp = 20; atk = 50; g_Atk = 10; speed = 100; g_Speed = 20; sAtk = 80; miss = 10; } } }
B1.cs
namespace _7._24 { class B1: Enemy { public B1() { Go("B1", 800, 10, 50, 10, 10, 10, 100); } } }
BeforeFight.cs
using System; namespace _7._24 { class BeforeFight { public void Run() { Game.player.ShowInfo(); Debug.Log("是否消耗20金币回满血量?",ConsoleColor.Cyan); Debug.Log("1,是 2,跳过", ConsoleColor.Cyan); string str = Console.ReadLine(); if (str == "1" && Game.player.money >= 20) { Game.player.money -= 20; Game.player.currentHp = Game.player.maxHp; Game.player.ShowInfo(); } } } }
Debug.cs
using System; using System.Threading; namespace _7._24 { class Debug { public static void Log(int a) { Console.WriteLine(a); Thread.Sleep(1000); } public static void Log(string text) { Console.WriteLine(text); Thread.Sleep(1000); } public static void Log() { for(int i = 0; i < 12; i++) { Console.WriteLine("·"); Thread.Sleep(200); } 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,int time,ConsoleColor color) { Console.ForegroundColor = color; Console.WriteLine(text); Thread.Sleep(time); Console.ForegroundColor = ConsoleColor.White; } } }
Enemy.cs
using System; namespace _7._24 { enum EnemyType { A1, B1 } class Enemy { SkillType[] skillTypes = { SkillType.晴天霹雳, SkillType.火舞炫风 }; int[] skillP = { 30, 100 }; public string name; public int hp; public int atk; public int speed; public int satk; public int miss; public int money; public int exp; public void Go(string name, int hp, int atk, int speed, int satk, int miss, int money, int exp) { this.name = name; this.hp = hp; this.atk = atk; this.speed = speed; this.satk = satk; this.miss = miss; this.money = money; this.exp = exp; } public bool GetAttack() { if (Tools.Random() < miss) { Debug.Log(name + "躲开了你的一次攻击!", ConsoleColor.Blue); return false; } if (Tools.Random() < 50) { Skill skill = new Skill(); int n = Tools.Random(); for (int j = 0; j < skillTypes.Length; j++) { if (n <= skillP[j]) { switch (skillTypes[j]) { case SkillType.火舞炫风: skill = new Skill1(); break; case SkillType.晴天霹雳: skill = new Skill2(); break; } Debug.Log("你使出了" + skill.name+ ",对" + name + "造成了" + skill.atk + "点伤害!", ConsoleColor.Magenta); hp -= skill.atk; break; } } } else { int m = Tools.Random(); if (m <= Game.player.sAtk) { Debug.Log("你使用会心一击对" + name + "造成了" + Game.player.atk * 2 + "点暴击伤害!", ConsoleColor.Red); hp -= Game.player.atk * 2; } else { Debug.Log("你使用普攻对" + name + "造成了" + Game.player.atk + "点伤害!"); hp -= Game.player.atk; } } if (hp <= 0) { Debug.Log("你击败了" + name + ",获得了" + money + "金钱," + exp + "经验," + name + "的尸体。", ConsoleColor.Green); Game.player.Bag(name + "的尸体"); Game.player.AddMoney(money); Game.player.AddExp(exp); Game.player.ShowInfo(); return true; } return false; } public void ShowInfo() { Debug.Log($"{name}属性:", ConsoleColor.Magenta); Debug.Log($"血量:{hp},攻击:{atk},速度:{speed},暴击:{satk},闪避:{miss}", ConsoleColor.Magenta); Debug.Log($"杀死可获得经验:{exp},杀死可获得金钱:{money}", ConsoleColor.Magenta); } } }
Game.cs
using System; using System.Threading.Tasks; namespace _7._24 { class Game { public static Player player; public void Run() { Debug.Log("欢迎来到平安京!",ConsoleColor.Blue); player = Player.CreatePlayer(); Scene1_1 scene1_1 = new Scene1_1(); scene1_1.Run(); } } }
HuiTaiLang.cs
using System; using System.Threading.Tasks; namespace _7._24 { class HuiTaiLang:NPC { public HuiTaiLang() { name = "灰太狼"; } public override void Chat() { Debug.Log("一个B1的尸体换10点攻击的提升。"); Debug.Log("1:换,2:跳过"); string str = Console.ReadLine(); if (str == "1") { int num = 0; for (int i = Game.player.bag.Length - 1; i >= 0; i--) { if (Game.player.bag[i] == "B1的尸体") { num += 1; Debug.Log("兑换成功!", ConsoleColor.Blue); Game.player.bag[i] = null; Game.player.atk += 10; Game.player.ShowInfo(); break; } } if (num == 0) { Debug.Log("兑换失败,你的背包中没有B1的尸体。", ConsoleColor.Cyan); } } } } }
Jump.cs
namespace _7._24 { class Jump:NPC { public Jump() { name = "去地底世界"; } public override void Chat() { Scene2_1 scene2_1 = new Scene2_1(); scene2_1.Run(); } } }
NPC.cs
namespace _7._24 { class NPC { public string name = ""; public virtual void Chat() { } } }
Player.cs
using System; namespace _7._24 { class Player { public string name; public int maxHp; public int g_Hp; public int currentHp; public int atk; public int g_Atk; public int speed; public int g_Speed; public int miss; public int sAtk; public int exp; public int level = 1; public int money = 20; public void ShowInfo() { Debug.Log($"你的属性:", ConsoleColor.Green); Debug.Log($"等级:{level},经验:{exp}/{level * 100},金钱:{money}", ConsoleColor.Green); Debug.Log($"血量:{currentHp}/{maxHp},攻击:{atk},速度:{speed},物理暴击率:{sAtk},闪避:{miss}", ConsoleColor.Green); Debug.Log($"你施法概率为50%,你拥有技能:{SkillType.晴天霹雳},{SkillType.火舞炫风}",ConsoleColor.Green); Bag(null); } public void AddExp(int exp) { this.exp += exp; while (this.exp>=100*level) { this.exp -= 100 * level; level++; maxHp += g_Hp; currentHp = maxHp; atk += g_Atk; speed += g_Speed; Debug.Log("恭喜升级!", ConsoleColor.Yellow); } } 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 static Player CreatePlayer() { Debug.Log("请输入职业:1:A,2:B"); Player player = null; string str = Console.ReadLine(); if (str == "1") { player = new A(); Debug.Log("你选择了A"); } else { player = new B(); Debug.Log("你选择了B"); } player.currentHp = player.maxHp; player.ShowInfo(); return player; } public bool GetAttack(Enemy enemy) { if (Tools.Random() < miss) { Debug.Log("你躲开了" + enemy.name + "的一次攻击!", ConsoleColor.Blue); return false; } if (Tools.Random() < enemy.satk) { Debug.Log(enemy.name + "对你造成了" + enemy.atk * 2 + "点暴击伤害!", ConsoleColor.Red); currentHp -= enemy.atk * 2; } else { Debug.Log(enemy.name + "对你造成了" + enemy.atk + "点伤害!"); currentHp -= enemy.atk; } if (currentHp <= 0) { Debug.Log("你被" + enemy.name + "残忍地杀害了。游戏结束。", ConsoleColor.Red); return true; } return false; } public string[] bag = new string[20]; public void Bag(string text) { Debug.Log("你的背包里有:",ConsoleColor.Yellow); for(int i = 0; i < bag.Length; i++) { if (bag[i] == null) { bag[i] = text; break; } } int m = 0; foreach(string a in bag) { m++; if (a != null) { Console.ForegroundColor = ConsoleColor.Yellow; Console.Write(m + "," + a + " "); Console.ForegroundColor = ConsoleColor.White; } } Console.WriteLine(); } } }
Program.cs
namespace _7._24 { class Program { static void Main(string[] args) { Game game = new Game(); game.Run(); } } }
Scene.cs
namespace _7._24 { class Scene { public string name = ""; public virtual void Run() { Debug.Log("到达" + name + "!"); } } }
Scene1.cs
using System; namespace _7._24 { class Scene1:Scene { public NPC[] npcs; public override void Run() { base.Run(); while (true) { string str = "请选择:"; for (int i = 0; i < npcs.Length; i++) { NPC npc = npcs[i]; str = str + i + "." + npc.name + " "; } Debug.Log(str); string s = Console.ReadLine(); int n = Convert.ToInt32(s); n = n < 0 ? 0 : n; n = n >= npcs.Length ? npcs.Length - 1 : n; npcs[n].Chat(); } } } }
Scene1_1.cs
namespace _7._24 { class Scene1_1:Scene1 { public Scene1_1() { name = "青青草原"; NPC npc1 = new XiYangYang(); NPC npc2 = new HuiTaiLang(); NPC npc3 = new Jump(); npcs = new NPC[] { npc1, npc2, npc3 }; } } }
Scene2.cs
using System; namespace _7._24 { class Scene2 : Scene { EnemyType[] enemyTypes; int[] enemyP; int nextSceneStep; protected void init(EnemyType[] enemyTypes, int[] enemyP, int nextSceneStep) { this.enemyTypes = enemyTypes; this.enemyP = enemyP; this.nextSceneStep = nextSceneStep; } public void Attack(EnemyType type) { Enemy enemy = null; switch (type) { case EnemyType.A1: enemy = new A1(); break; case EnemyType.B1: enemy = new B1(); break; } Debug.Log("你遇到了" + enemy.name + "的袭击!"); enemy.ShowInfo(); BeforeFight before = new BeforeFight(); before.Run(); bool isPlayerAtk = Game.player.speed > enemy.speed; while (true) { if (isPlayerAtk) { isPlayerAtk = false; bool res = enemy.GetAttack(); if (res == true) { break; } } else { isPlayerAtk = true; bool res = Game.player.GetAttack(enemy); if (res == true) { Environment.Exit(0); } } } } public override void Run() { base.Run(); for (int i = 0; i < nextSceneStep; i++) { Debug.Log("按下回车键移动。"); Console.ReadLine(); int n = Tools.Random(); for (int j = 0; j < enemyTypes.Length; j++) { if (n < enemyP[j]) { Attack(enemyTypes[j]); break; } } } GoScene(); } protected virtual void GoScene() { } } }
Scene2_1.cs
namespace _7._24 { class Scene2_1:Scene2 { public Scene2_1() { name = "地底世界"; EnemyType[] enemyTypes = { EnemyType.A1, EnemyType.B1 }; int[] enemyP = { 20, 60 }; init(enemyTypes, enemyP, 2); } protected override void GoScene() { Scene1_1 scene1_1 = new Scene1_1(); scene1_1.Run(); } } }
Skill.cs
namespace _7._24 { enum SkillType { 火舞炫风, 晴天霹雳 } class Skill { public string name; public int atk; public void MoFa(string name,int atk) { this.name = name; this.atk = atk; } } }
Skill1.cs
namespace _7._24 { class Skill1:Skill { public Skill1() { MoFa("火舞炫风", Game.player.atk*3); } } }
Skill2.cs
namespace _7._24 { class Skill2:Skill { public Skill2() { MoFa("晴天霹雳", Game.player.atk*4); } } }
Tools.cs
using System; namespace _7._24 { class Tools { public static int Random() { return new Random().Next(0, 101); } } }
XiYangYang.cs
using System; namespace _7._24 { class XiYangYang:NPC { public XiYangYang() { name = "喜洋洋"; } public override void Chat() { Debug.Log("一个A1的尸体换20点生命上限的提升。"); Debug.Log("1:换,2:跳过"); string str = Console.ReadLine(); if (str == "1") { int num = 0; for(int i= Game.player.bag.Length-1; i >= 0; i--) { if (Game.player.bag[i] == "A1的尸体") { num += 1; Debug.Log("兑换成功!", ConsoleColor.Blue); Game.player.bag[i] = null; Game.player.maxHp += 20; Game.player.ShowInfo(); break; } } if (num == 0) { Debug.Log("兑换失败,你的背包中没有A1的尸体。", ConsoleColor.Cyan); } } } } }
相关文章推荐
- Silverlight C# 游戏开发:面向对象在游戏中的实例(二)
- 游戏人生Silverlight(3) - 打苍蝇[Silverlight 2.0(c#)]
- 上接游戏人生Silverlight(1) - 七彩俄罗斯方块[Silverlight 2.0(c#)]
- 游戏人生Silverlight(4) - 连连看[Silverlight 2.0(c#)]
- 【用C#写游戏-XNA游戏编程】坦克大战(三) 让面向对象的坦克在游戏画面上跑起来
- 游戏人生Silverlight(3) - 打苍蝇[Silverlight 2.0(c#)]
- 游戏人生Silverlight(1) - 七彩俄罗斯方块[Silverlight 2.0(c#)]
- 上接游戏人生Silverlight(4) - 连连看[Silverlight 2.0(c#)]
- 游戏人生Silverlight(2) - 趣味钢琴[Silverlight 2.0(c#)]
- 游戏人生Silverlight(4) - 连连看[Silverlight 2.0(c#)]
- 游戏人生Silverlight(1) - 七彩俄罗斯方块[Silverlight 2.0(c#)]
- 游戏人生Silverlight(3) - 打苍蝇[Silverlight 2.0(c#)]
- 游戏人生Silverlight(2) - 趣味钢琴[Silverlight 2.0(c#)]
- Silverlight C# 游戏开发:面向对象在游戏中的实例(二)
- 上接游戏人生Silverlight(2) - 趣味钢琴[Silverlight 2.0(c#)]
- 游戏人生Silverlight(2) - 趣味钢琴[Silverlight 2.0(c#)]
- 游戏人生Silverlight(4) - 连连看[Silverlight 2.0(c#)]
- 游戏人生Silverlight(2) - 趣味钢琴[Silverlight 2.0(c#)]
- Silverlight C# 游戏开发:面向对象在游戏中的实例(一)
- Silverlight C# 游戏开发:面向对象在游戏中的实例(一)