效果圖:
代碼如下:
1 class Program 2 { 3 struct Soldier//戰士 4 { 5 private string name;//用戶名 6 private int attack;//攻擊力 7 private int defense;//防御力 8 private int hP;//血量 9 10 public string Name { get => name; set => name = value; } 11 public int Attack { get => attack; set => attack = value; } 12 public int Defense { get => defense; set => defense = value; } 13 public int HP { get => hP; set => hP = value; } 14 } 15 struct Weapon//武器 16 { 17 private string name;//武器名 18 private int attack;//攻擊力 19 20 public string Name { get => name; set => name = value; } 21 public int Attack { get => attack; set => attack = value; } 22 } 23 struct Skill//技能 24 { 25 private string name;//技能名 26 private int attack;//攻擊力 27 28 public string Name { get => name; set => name = value; } 29 public int Attack { get => attack; set => attack = value; } 30 } 31 32 static void Main(string[] args) 33 { 34 Random r = new Random(); 35 36 #region 武器庫 37 List<Weapon> Weapons = new List<Weapon>(); 38 Weapon w1 = new Weapon(); 39 w1.Name = "樹枝"; 40 w1.Attack = 20; 41 Weapons.Add(w1); 42 43 Weapon w2 = new Weapon(); 44 w2.Name = "木棍"; 45 w2.Attack = 40; 46 Weapons.Add(w2); 47 48 Weapon w3 = new Weapon(); 49 w3.Name = "棒球棍"; 50 w3.Attack = 60; 51 Weapons.Add(w3); 52 53 Weapon w4 = new Weapon(); 54 w4.Name = "板磚"; 55 w4.Attack = 100; 56 Weapons.Add(w4); 57 #endregion 58 59 #region 技能庫 60 List<Skill> Skills = new List<Skill>(); 61 Skill sk1 = new Skill(); 62 sk1.Name = "普通攻擊"; 63 sk1.Attack = 20; 64 Skills.Add(sk1); 65 66 Skill sk2 = new Skill(); 67 sk2.Name = "認真一拳"; 68 sk2.Attack = 50; 69 Skills.Add(sk2); 70 71 Skill sk3 = new Skill(); 72 sk3.Name = "飛起一腳"; 73 sk3.Attack = 100; 74 Skills.Add(sk3); 75 76 Skill sk4 = new Skill(); 77 sk4.Name = "死亡注視"; 78 sk4.Attack = 200; 79 Skills.Add(sk4); 80 #endregion 81 82 #region 輸入倆戰士名字,戰士初始化 83 Soldier s1 = new Soldier(); 84 Console.Write("請輸入第一個戰士的名字:"); 85 s1.Name = Console.ReadLine(); 86 s1.Attack = 100; 87 s1.Defense = 20; 88 s1.HP = 1000; 89 90 Soldier s2 = new Soldier(); 91 Console.Write("請輸入第二個戰士的名字:"); 92 s2.Name = Console.ReadLine(); 93 s2.Attack = 100; 94 s2.Defense = 20; 95 s2.HP = 1000; 96 #endregion 97 98 #region 游戲開始,玩家武器刷新 99 Console.WriteLine(); 100 Console.WriteLine("===============游戲開始==============="); 101 Console.WriteLine(); 102 int s1_w = r.Next(0, Weapons.Count);//為戰士一隨機獲取一個武器 103 s1.Attack += Weapons[s1_w].Attack;//戰士一的攻擊力更新 104 Console.WriteLine(s1.Name + "獲得的武器是:" + Weapons[s1_w].Name+"!攻擊力成長為:"+s1.Attack); 105 106 int s2_w = r.Next(0, Weapons.Count);//為戰士二隨機獲取一個武器 107 s2.Attack += Weapons[s2_w].Attack;//戰士二的攻擊力更新 108 Console.WriteLine(s2.Name + "獲得的武器是:" + Weapons[s2_w].Name + "!攻擊力成長為:" + s2.Attack); 109 Console.WriteLine(); 110 #endregion 111 112 #region 循環攻擊開始 113 int i = 0; 114 while (true) 115 { 116 Console.WriteLine("===============第"+(++i)+"回合==============="); 117 Console.WriteLine(); 118 int s1_s = r.Next(0,Skills.Count);//戰士一此回合獲取的技能 119 int s1_a = s1.Attack + Skills[s1_s].Attack + r.Next(0,101);//戰士一此回合的攻擊力(戰士一的攻擊力+技能攻擊力+0到100的攻擊力浮動) 120 s2.HP -= (s1_a - s2.Defense);//戰士二剩余血量 121 if (s1_s == 0)//輸出顏色隨技能改變 122 Console.ForegroundColor = ConsoleColor.Blue; 123 else if (s1_s == 1) 124 Console.ForegroundColor = ConsoleColor.Yellow; 125 else if (s1_s == 2) 126 Console.ForegroundColor = ConsoleColor.Green; 127 else if(s1_s==3) 128 Console.ForegroundColor = ConsoleColor.Cyan; 129 Console.WriteLine("【"+s1.Name+"】對【"+s2.Name+"】發動【"+Skills[s1_s].Name+"】,造成【"+ s1_a + "】傷害,【" + s2.Name + "】的血量還有"+s2.HP); 130 Console.ForegroundColor = ConsoleColor.White; 131 132 int s2_s = r.Next(0, Skills.Count);//戰士一此回合獲取的技能 133 int s2_a = s2.Attack + Skills[s2_s].Attack + r.Next(0, 101);//戰士一此回合的攻擊力(戰士一的攻擊力+技能攻擊力+0到100的攻擊力浮動) 134 s1.HP -= (s2_a - s1.Defense);//戰士二剩余血量 135 if (s2_s == 0) 136 Console.ForegroundColor = ConsoleColor.Blue; 137 else if (s2_s == 1) 138 Console.ForegroundColor = ConsoleColor.Yellow; 139 else if (s2_s == 2) 140 Console.ForegroundColor = ConsoleColor.Green; 141 else if (s2_s == 3) 142 Console.ForegroundColor = ConsoleColor.Cyan; 143 Console.WriteLine("【" + s2.Name + "】對【" + s1.Name + "】發動【" + Skills[s2_s].Name + "】,造成【" + s2_a + "】傷害,【" + s1.Name + "】的血量還有" + s1.HP); 144 Console.ForegroundColor = ConsoleColor.White; 145 Console.WriteLine(); 146 //判斷戰斗結果 147 if (s1.HP <= 0 && s2.HP <= 0) 148 { 149 Console.ForegroundColor = ConsoleColor.Red; 150 Console.WriteLine("同歸於盡!"); 151 break; 152 } 153 else if (s1.HP <= 0) 154 { 155 Console.ForegroundColor = ConsoleColor.Red; 156 Console.WriteLine(s2.Name + "戰勝!"); 157 break; 158 } 159 else if (s2.HP <= 0) 160 { 161 Console.ForegroundColor = ConsoleColor.Red; 162 Console.WriteLine(s1.Name + "戰勝!"); 163 break; 164 } 165 System.Threading.Thread.Sleep(1000); 166 } 167 #endregion 168 169 Console.ReadLine(); 170 } 171 }