In this part of my C# tutorial series we will make a fun little game. I’m going to show how to create 2 warriors that will fight to the death in a simulated battle. More complex video games follow many of the techniques we cover in this video. It also will show how awesome object oriented programming is in that it gives you the ability to model just about any real world object, or interaction.
For best results print the code and then write your own programs while referring to the cheat sheet.
If you like videos like this consider donating a $1 on Patreon.
[googleplusone]
Code & Transcript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
// ---------- Warrior.cs ---------- using System; namespace CSharpTutA.cs { class Warrior { // Define the Warriors properties public string Name { get; set; } = "Warrior"; public double Health { get; set; } = 0; public double AttkMax { get; set; } = 0; public double BlockMax { get; set; } = 0; // Always create a single Random instance and reuse // it or you will get the same value over and over Random rnd = new Random(); // Constructor initializes the warrior public Warrior(string name = "Warrior", double health = 0, double attkMax = 0, double blockMax = 0) { Name = name; Health = health; AttkMax = attkMax; BlockMax = blockMax; } // Generate a random atack value from 1 // to the warriors maximum attack value public double Attack() { return rnd.Next(1, (int)AttkMax); } // Generate a random block value from // 1 to the warriors maximum block public double Block() { return rnd.Next(1, (int)BlockMax); } } } // ---------- Battle.cs ---------- using System; namespace CSharpTutA.cs { class Battle { // This is a utility class so it makes sense // to have just static methods // Recieve both Warrior objects public static void StartFight(Warrior warrior1, Warrior warrior2) { // Loop giving each Warrior a chance to attack // and block each turn until 1 dies while (true) { if (GetAttackResult(warrior1, warrior2) == "Game Over") { Console.WriteLine("Game Over"); break; } if (GetAttackResult(warrior2, warrior1) == "Game Over") { Console.WriteLine("Game Over"); break; } } } // Accept 2 Warriors public static string GetAttackResult(Warrior warriorA, Warrior warriorB) { // Calculate one Warriors attack and the others block double warAAttkAmt = warriorA.Attack(); double warBBlkAmt = warriorB.Block(); // Subtract block from attack double dmg2WarB = warAAttkAmt - warBBlkAmt; // If there was damage subtract that from the health if (dmg2WarB > 0) { warriorB.Health = warriorB.Health - dmg2WarB; } else dmg2WarB = 0; // Print out info on who attacked who and for how // much damage Console.WriteLine("{0} Attacks {1} and Deals {2} Damage", warriorA.Name, warriorB.Name, dmg2WarB); // Provide output on the change to health Console.WriteLine("{0} Has {1} Health\n", warriorB.Name, warriorB.Health); // Check if the warriors health fell below // zero and if so print a message and send // a response that will end the loop if (warriorB.Health <= 0) { Console.WriteLine("{0} has Died and {1} is Victorious\n", warriorB.Name, warriorA.Name); return "Game Over"; } else return "Fight Again"; } } } // ---------- Program.cs ---------- using System; namespace CSharpTutA.cs { class Program { /* Bob Attacks Maximus and Deals 74 Damage Maximus Has 69 Health Maximus Attacks Bob and Deals 6 Damage Bob Has 6 Health Bob Attacks Maximus and Deals 48 Damage Maximus Has 21 Health Maximus Attacks Bob and Deals 48 Damage Bob Has -42 Health Bob has Died and Maximus is Victorious Game Over */ static void Main(string[] args) { Warrior maximus = new Warrior("Maximus", 1000, 120, 40); Warrior bob = new Warrior("Bob", 1000, 120, 40); Battle.StartFight(maximus, bob); Console.ReadLine(); } } } |
Leave a Reply