Advertisement
Dosk3n

KillNinjaC101

Mar 14th, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ClassesKillTheNinja
  8.    
  9. {
  10.     /// <summary>
  11.     /// A mini game created for Coding 101 - Twit.tv/code - by Dean T (twitter @dosk3n)
  12.     /// to show a working example of classes being used in a real (but basic) scenario.
  13.     /// Be aware I am using public variables which is not recommended but used for ease
  14.     /// in this example code.
  15.     /// </summary>
  16.     class Program
  17.     {
  18.  
  19.  
  20.         // ### CLASSES ###
  21.         // I have put the classes at the top since classes are this weeks topic.
  22.         //
  23.         // A class is a blueprint of a model you want to use in your code. For example
  24.         // if you built a house from a blueprint you would get the same house every time
  25.         // but once you had built a few different houses you would then add house
  26.         // numbers to distinguish them from each other. This is the same with a class.
  27.         // each house on the blue print would have x amount of windows and x amount of doors
  28.         // so in the same way you could create a house class that had 4 windows and 2 doors.
  29.         // For example int windows = 4 and int doors = 2 would be in the class code.
  30.         //
  31.         // Since in a video game use you would create a class for an enemy character for example
  32.         // in a zombie game you may have 50 zombies coming at you that are all of the same level
  33.         // and just used to get your score up. Instead of coding each zombie on its own you would
  34.         // create a class of zombie (a blueprint that listed its health, speed, damage etc) and
  35.         // just call that class multiple times in your code which then each time it is called
  36.         // already has the data set for it, rather than having to set each zombie individually.
  37.         //
  38.         // In my code here, I am creating the blueprint (class) for 2 playable characters and
  39.         // 2 bad guys. An evil ninja and the boss.
  40.  
  41.         public class Ninja // In a similar fashion to functions we have to create the name
  42.                            // so that we can call it up later. Here is is called Ninja
  43.         {
  44.             public int healthPoints = 1000; // Sets the default health for our Ninja character
  45.             public int powerPoints = 300;   // Sets the power / strength of our character
  46.         }
  47.  
  48.         // I do the same thing for each of my other classes as they all have the same parameters
  49.         // so that I can compare them in a fight but each one has different base stats
  50.  
  51.         public class Human
  52.         {
  53.             public int healthPoints = 1;
  54.             public int powerPoints = 1;
  55.         }
  56.  
  57.         public class EvilNinja
  58.         {
  59.             public int healthPoints = 900;
  60.             public int powerPoints = 250;
  61.         }
  62.  
  63.         public class EvilBoss
  64.         {
  65.             public int healthPoints = 3900;
  66.             public int powerPoints = 440;
  67.         }
  68.  
  69.  
  70.         // ### PROGRAM STARTS HERE ###############################################################
  71.  
  72.         static void Main(string[] args)
  73.         {
  74.             PrintWelcome(); // This starts the PrintWelcome function to print our welcome.
  75.            
  76.             // Lets ask the player to choose a character to play as.
  77.             Console.ForegroundColor = ConsoleColor.White;
  78.             Console.WriteLine("WHO WOULD YOU LIKE TO PLAY AS?\n");
  79.             Console.WriteLine("-THE POWERFUL NINJA WITH A KEEN FIGHTING ABILITY (1)");
  80.             Console.WriteLine("-THE REGULAR HUMAN WITH NO ABILITY WHAT SO EVER (2)");
  81.             Console.Write("\nPLEASE SELECT YOUR CHARACTER (1 or 2):");
  82.             int choice = Int32.Parse(Console.ReadLine());
  83.             int playerHealth; // declare empty ints for use
  84.             int playerPower;
  85.  
  86.             ////////////////////////////////////////
  87.             // This is where we first call one of our class'. So if our player chooses 1
  88.             // then he will get to play as the Ninja. Try to imagine classes and the way
  89.             // they can be called are similar to variables. For example int num = 3
  90.             // instead of wanting an intiger in this case we want to use Ninja so we
  91.             // create a new Ninja which is called player (like before the int was called
  92.             // num). This then gives player the base stats that we declared in out class
  93.             // blueprint.
  94.             if (choice == 1)
  95.             {
  96.                 Ninja player = new Ninja();
  97.                 playerHealth = player.healthPoints; // this sets the value of the int
  98.                                                     // playerHealth using the value stored
  99.                                                 // in player (remember its a copy of our
  100.                                                 // ninja class) healthPoints int, which we
  101.                                                 // set in out class for Ninja.
  102.  
  103.                 playerPower = player.powerPoints; // This is the same but for powerPoints.
  104.             }
  105.             else // If any other choice is selected it defaults to our Human class that was
  106.                 // set up above with our other classes. As you should be able to see it
  107.                 // does the same thing as described above but by using the Human class
  108.                 // rather than the Ninja class it gets the different base stats that we set.
  109.             {
  110.                 Human player = new Human();
  111.                 playerHealth = player.healthPoints;
  112.                 playerPower = player.powerPoints;
  113.             }
  114.  
  115.            
  116.             while (true) // Starts the game loop
  117.             {
  118.                 Console.WriteLine();
  119.                 Console.WriteLine("WHAT WOULD YOU LIKE TO DO?");
  120.                 Console.WriteLine("FIGHT AN EVIL NINJA (1)");
  121.                 Console.WriteLine("FIGHT THE POWERFUL BOSS (2)");
  122.                 Console.WriteLine("QUIT (3)\n");
  123.                 Console.Write("PLEASE CHOOSE CAREFULLY (1, 2 OR 3):" );
  124.                 int fightChoice = Int32.Parse(Console.ReadLine());
  125.            
  126.                 // Make a choice of which bad guy to fight
  127.                 if (fightChoice == 1)
  128.                     // If choice 1 for the Ninja is selected send our health and power to
  129.                     // the function for fighting the Ninja which will decide if we can win
  130.                     // or not and then return a true or false value for it we won or not
  131.                     // and save that value to outcome.
  132.                 {
  133.                     bool outcome = FightEvilNinja(playerHealth, playerPower); // send our stats to
  134.                                                                     // to our function to return a
  135.                                                                 // bool value after it has run/
  136.                                                                 // Check out the function for
  137.                                                                 // more info.
  138.                     if (outcome == true) // If we win
  139.                     {
  140.                         playerHealth = playerHealth + 1000; // increase our health
  141.                         playerPower = playerPower + 50; // increase our power
  142.                         Console.ForegroundColor = ConsoleColor.Red;
  143.                         Console.WriteLine("\nYOU WON! YOUR HEALTH IS NOW {0}"
  144.                         + " AND YOUR POWER IS NOW {1}!", playerHealth, playerPower);
  145.                         Console.ForegroundColor = ConsoleColor.White;
  146.                     }
  147.                     else // If we lose
  148.                     {
  149.                         Console.WriteLine();
  150.                         Console.ForegroundColor = ConsoleColor.Red;
  151.                         Console.WriteLine("YOU WERE DEFEATED. PRESS ENTER TO CONTINUE.");
  152.                         Console.ForegroundColor = ConsoleColor.White;
  153.                         Console.ReadLine();
  154.                     }
  155.                 }
  156.                 if (fightChoice == 2) // Same as above but boss selected so sent to boss Function
  157.                 {
  158.                     bool outcome = FightBoss(playerHealth, playerPower);
  159.                     if (outcome == true)
  160.                     {
  161.                         playerHealth = playerHealth + 1000;
  162.                         playerPower = playerPower + 50;
  163.                         Console.ForegroundColor = ConsoleColor.Red;
  164.                         Console.WriteLine("\nYOU WON! THE BOSS IS NO MORE!");
  165.                         Console.ForegroundColor = ConsoleColor.White;
  166.                         Console.WriteLine();
  167.                         Console.WriteLine("Thank you for playing. Press enter or any key to exit the game.");
  168.                         Console.ReadLine();
  169.                         break;
  170.                     }
  171.                     else
  172.                     {
  173.                         Console.WriteLine();
  174.                         Console.ForegroundColor = ConsoleColor.Red;
  175.                         Console.WriteLine("YOU WERE DEFEATED. PRESS ENTER TO CONTINUE.");
  176.                         Console.ForegroundColor = ConsoleColor.White;
  177.                         Console.ReadLine();
  178.                     }
  179.                 }
  180.                 if (fightChoice == 3) // if we chose 3 we can quit
  181.                 {
  182.                     break;
  183.                 }
  184.                
  185.             }
  186.         }
  187.  
  188.  
  189.         // ### FUNCTIONS ################################################
  190.         // Because this code is designed to show the use of classes the majority of
  191.         // notes here will be on classes
  192.         static void PrintWelcome() // Basic welcome function
  193.         {
  194.             Console.WriteLine("#######################################################################");
  195.             Console.WriteLine("##########                                                   ##########");
  196.             Console.WriteLine("##########                   KILL THE NINJA                  ##########");
  197.             Console.WriteLine("##########                                                   ##########");
  198.             Console.WriteLine("##########    Example code for Coding 101 - Twit.tv/code     ##########");
  199.             Console.WriteLine("##########           by Dean T (twitter: @Dosk3n)            ##########");
  200.             Console.WriteLine("#######################################################################");
  201.             Console.WriteLine();
  202.         }
  203.  
  204.         // Here we fight the evil Ninja! We sent our health and power (which is different depending
  205.         // if you choose to be a Ninja or regular human) to this function in the code above.
  206.         // This then creates an evil ninja using our blueprint (class) we made at the start and
  207.         // names it evilninja. It then compares our health to the healthpoints we gave our evil
  208.         // ninja from our blueprint and if our health is lower it sends back faulse as we lost
  209.         // or it sends back true meaning we won. You could change this part to run a calculation
  210.         // determine the winner but I kept it simple as that wasnt the aim of this code.
  211.         static bool FightEvilNinja(int playerHealth, int playerPower)
  212.         {
  213.             EvilNinja evilninja = new EvilNinja();
  214.             if (playerHealth < evilninja.healthPoints & playerPower < evilninja.powerPoints)
  215.             {
  216.                 Console.WriteLine("\nYOU STAND FACE TO FACE AGAINST YOUR OPPONENT...\n");
  217.                 Console.WriteLine("YOUR HEALTH LEVEL IS: {0}", playerHealth);
  218.                 Console.WriteLine("YOUR POWER LEVEL IS: {0}", playerPower);
  219.                 Console.WriteLine("YOUR OPPONENTS HEALTH LEVEL IS {0}", evilninja.healthPoints);
  220.                 Console.WriteLine("YOUR OPPONENTS POWER LEVEL IS {0}", evilninja.powerPoints);
  221.                 Console.Write("Press Enter to start the fight...");
  222.                 Console.ReadLine();
  223.                 return false;  // We lost to return false
  224.             }
  225.             else
  226.             {
  227.                 Console.WriteLine("\nYOU STAND FACE TO FACE AGAINST YOUR OPPONENT...\n");
  228.                 Console.WriteLine("YOUR HEALTH LEVEL IS: {0}", playerHealth);
  229.                 Console.WriteLine("YOUR POWER LEVEL IS: {0}", playerPower);
  230.                 Console.WriteLine("YOUR OPPONENTS HEALTH LEVEL IS {0}", evilninja.healthPoints);
  231.                 Console.WriteLine("YOUR OPPONENTS POWER LEVEL IS {0}", evilninja.powerPoints);
  232.                 Console.Write("Press Enter to start the fight...");
  233.                 Console.ReadLine();
  234.                 return true; // We won so return true
  235.             }
  236.            
  237.         }
  238.  
  239.  
  240.         // This does the exact same thing as the fight above but uses out boss class rather than
  241.         // the evil ninja class which means the stats are different based off the blueprint
  242.         static bool FightBoss(int playerHealth, int playerPower)
  243.         {
  244.             EvilBoss evilboss = new EvilBoss();
  245.             if (playerHealth < evilboss.healthPoints & playerPower < evilboss.powerPoints)
  246.             {
  247.                 Console.WriteLine("\nYOU STAND FACE TO FACE AGAINST YOUR OPPONENT...\n");
  248.                 Console.WriteLine("YOUR HEALTH LEVEL IS: {0}", playerHealth);
  249.                 Console.WriteLine("YOUR POWER LEVEL IS: {0}", playerPower);
  250.                 Console.WriteLine("YOUR OPPONENTS HEALTH LEVEL IS {0}", evilboss.healthPoints);
  251.                 Console.WriteLine("YOUR OPPONENTS POWER LEVEL IS {0}", evilboss.powerPoints);
  252.                 Console.Write("Press Enter to start the fight...");
  253.                 Console.ReadLine();
  254.                 return false;
  255.             }
  256.             else
  257.             {
  258.                 Console.WriteLine("\nYOU STAND FACE TO FACE AGAINST YOUR OPPONENT...\n");
  259.                 Console.WriteLine("YOUR HEALTH LEVEL IS: {0}", playerHealth);
  260.                 Console.WriteLine("YOUR POWER LEVEL IS: {0}", playerPower);
  261.                 Console.WriteLine("YOUR OPPONENTS HEALTH LEVEL IS {0}", evilboss.healthPoints);
  262.                 Console.WriteLine("YOUR OPPONENTS POWER LEVEL IS {0}", evilboss.powerPoints);
  263.                 Console.Write("Press Enter to start the fight...");
  264.                 Console.ReadLine();
  265.                 return true;
  266.             }
  267.         }
  268.  
  269.  
  270.        
  271.  
  272.     }
  273.  
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement