1 namespace test 2 3 { 4 5 #region 武器接口和对每个武器的实现 6 7 /// <summary> 8 9 /// 武器攻击的通用接口 10 11 /// </summary> 12 13 internal interface IAttack 14 15 { 16 17 /// <summary> 18 19 /// 攻击,参数是被攻击的对象 20 21 /// </summary> 22 23 /// <param name="monster"></param> 24 25 void AttackTarget(Enemy enemy); 26 27 } 28 29 30 31 /// <summary> 32 33 /// 武器-木剑 34 35 /// </summary> 36 37 internal sealed class WoodSword : IAttack 38 39 { 40 41 public void AttackTarget(Enemy enemy) 42 43 { 44 45 enemy.Notify(20); 46 47 } 48 49 } 50 51 52 53 /// <summary> 54 55 /// 武器-金属剑 56 57 /// </summary> 58 59 internal sealed class IronSword : IAttack 60 61 { 62 63 public void AttackTarget(Enemy enemy) 64 65 { 66 67 enemy.Notify(50); 68 69 } 70 71 } 72 73 /// <summary> 74 75 /// 武器-魔剑 76 77 /// </summary> 78 79 internal sealed class MagicSword : IAttack 80 81 { 82 83 private Random _random = new Random(); 84 85 86 87 public void AttackTarget(Enemy enemy) 88 89 { 90 91 Int32 loss = (_random.NextDouble() < 0.5) ? 100 : 200; 92 93 if (200 == loss) 94 95 { 96 97 Console.WriteLine("出现暴击!!!"); 98 99 } 100 101 enemy.Notify(loss); 102 103 } 104 105 } 106 107 #endregion 108 109 110 111 #region 被攻击的对象,武器操作的对象 112 113 /// <summary> 114 115 /// 怪兽 116 117 /// </summary> 118 119 internal sealed class Enemy 120 121 { 122 123 /// <summary> 124 125 /// 怪物的名字 126 127 /// </summary> 128 129 public String Name { get; set; } 130 131 132 133 /// <summary> 134 135 /// 怪物的生命值 136 137 /// </summary> 138 139 private Int32 HP { get; set; } 140 141 142 143 public Enemy(String name, Int32 hp) 144 145 { 146 147 this.Name = name; 148 149 this.HP = hp; 150 151 } 152 153 154 155 /// <summary> 156 157 /// 怪物被攻击时,被调用的方法,用来处理被攻击后的状态更改 158 159 /// </summary> 160 161 /// <param name="loss">此次攻击损失的HP</param> 162 163 public void Notify(Int32 loss) 164 165 { 166 167 if (this.HP <= 0) 168 169 { 170 171 Console.WriteLine("此怪物已死"); 172 173 return; 174 175 } 176 177 178 179 this.HP -= loss; 180 181 if (this.HP <= 0) 182 183 { 184 185 Console.WriteLine("怪物" + this.Name + "被打死"); 186 187 } 188 189 else 190 191 { 192 193 Console.WriteLine("怪物" + this.Name + "损失" + loss + "HP"); 194 195 } 196 197 } 198 199 } 200 201 #endregion 202 203 204 205 #region 游戏主人公,武器的发起者 206 207 /// <summary> 208 209 /// 角色 210 211 /// </summary> 212 213 internal sealed class Role 214 215 { 216 217 /// <summary> 218 219 /// 表示角色目前所持武器 220 221 /// </summary> 222 223 public IAttack Weapon { get; set; } 224 225 226 227 /// <summary> 228 229 /// 攻击怪物 230 231 /// </summary> 232 233 /// <param name="monster">被攻击的怪物</param> 234 235 public void Attack(Enemy enemy) 236 237 { 238 239 this.Weapon.AttackTarget(enemy); 240 241 } 242 243 } 244 245 #endregion 246 247 248 249 }
主程序中去调用它:
1 #region 通过一个游戏学习设计模块 2 3 Enemy enemy1 = new Enemy("小怪A", 50); 4 5 Enemy enemy2 = new Enemy("小怪B", 50); 6 7 Enemy enemy3 = new Enemy("关主", 200); 8 9 Enemy enemy4 = new Enemy("最终Boss", 1000); 10 11 Role role = new Role(); 12 13 role.Weapon = new WoodSword(); 14 15 role.Attack(enemy1); 16 17 role.Attack(enemy1); 18 19 role.Attack(enemy1); 20 21 #endregion