Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ClassesKillTheNinja
- {
- /// <summary>
- /// A mini game created for Coding 101 - Twit.tv/code - by Dean T (twitter @dosk3n)
- /// to show a working example of classes being used in a real (but basic) scenario.
- /// Be aware I am using public variables which is not recommended but used for ease
- /// in this example code.
- /// </summary>
- class Program
- {
- // ### CLASSES ###
- // I have put the classes at the top since classes are this weeks topic.
- //
- // A class is a blueprint of a model you want to use in your code. For example
- // if you built a house from a blueprint you would get the same house every time
- // but once you had built a few different houses you would then add house
- // numbers to distinguish them from each other. This is the same with a class.
- // each house on the blue print would have x amount of windows and x amount of doors
- // so in the same way you could create a house class that had 4 windows and 2 doors.
- // For example int windows = 4 and int doors = 2 would be in the class code.
- //
- // Since in a video game use you would create a class for an enemy character for example
- // in a zombie game you may have 50 zombies coming at you that are all of the same level
- // and just used to get your score up. Instead of coding each zombie on its own you would
- // create a class of zombie (a blueprint that listed its health, speed, damage etc) and
- // just call that class multiple times in your code which then each time it is called
- // already has the data set for it, rather than having to set each zombie individually.
- //
- // In my code here, I am creating the blueprint (class) for 2 playable characters and
- // 2 bad guys. An evil ninja and the boss.
- public class Ninja // In a similar fashion to functions we have to create the name
- // so that we can call it up later. Here is is called Ninja
- {
- public int healthPoints = 1000; // Sets the default health for our Ninja character
- public int powerPoints = 300; // Sets the power / strength of our character
- }
- // I do the same thing for each of my other classes as they all have the same parameters
- // so that I can compare them in a fight but each one has different base stats
- public class Human
- {
- public int healthPoints = 1;
- public int powerPoints = 1;
- }
- public class EvilNinja
- {
- public int healthPoints = 900;
- public int powerPoints = 250;
- }
- public class EvilBoss
- {
- public int healthPoints = 3900;
- public int powerPoints = 440;
- }
- // ### PROGRAM STARTS HERE ###############################################################
- static void Main(string[] args)
- {
- PrintWelcome(); // This starts the PrintWelcome function to print our welcome.
- // Lets ask the player to choose a character to play as.
- Console.ForegroundColor = ConsoleColor.White;
- Console.WriteLine("WHO WOULD YOU LIKE TO PLAY AS?\n");
- Console.WriteLine("-THE POWERFUL NINJA WITH A KEEN FIGHTING ABILITY (1)");
- Console.WriteLine("-THE REGULAR HUMAN WITH NO ABILITY WHAT SO EVER (2)");
- Console.Write("\nPLEASE SELECT YOUR CHARACTER (1 or 2):");
- int choice = Int32.Parse(Console.ReadLine());
- int playerHealth; // declare empty ints for use
- int playerPower;
- ////////////////////////////////////////
- // This is where we first call one of our class'. So if our player chooses 1
- // then he will get to play as the Ninja. Try to imagine classes and the way
- // they can be called are similar to variables. For example int num = 3
- // instead of wanting an intiger in this case we want to use Ninja so we
- // create a new Ninja which is called player (like before the int was called
- // num). This then gives player the base stats that we declared in out class
- // blueprint.
- if (choice == 1)
- {
- Ninja player = new Ninja();
- playerHealth = player.healthPoints; // this sets the value of the int
- // playerHealth using the value stored
- // in player (remember its a copy of our
- // ninja class) healthPoints int, which we
- // set in out class for Ninja.
- playerPower = player.powerPoints; // This is the same but for powerPoints.
- }
- else // If any other choice is selected it defaults to our Human class that was
- // set up above with our other classes. As you should be able to see it
- // does the same thing as described above but by using the Human class
- // rather than the Ninja class it gets the different base stats that we set.
- {
- Human player = new Human();
- playerHealth = player.healthPoints;
- playerPower = player.powerPoints;
- }
- while (true) // Starts the game loop
- {
- Console.WriteLine();
- Console.WriteLine("WHAT WOULD YOU LIKE TO DO?");
- Console.WriteLine("FIGHT AN EVIL NINJA (1)");
- Console.WriteLine("FIGHT THE POWERFUL BOSS (2)");
- Console.WriteLine("QUIT (3)\n");
- Console.Write("PLEASE CHOOSE CAREFULLY (1, 2 OR 3):" );
- int fightChoice = Int32.Parse(Console.ReadLine());
- // Make a choice of which bad guy to fight
- if (fightChoice == 1)
- // If choice 1 for the Ninja is selected send our health and power to
- // the function for fighting the Ninja which will decide if we can win
- // or not and then return a true or false value for it we won or not
- // and save that value to outcome.
- {
- bool outcome = FightEvilNinja(playerHealth, playerPower); // send our stats to
- // to our function to return a
- // bool value after it has run/
- // Check out the function for
- // more info.
- if (outcome == true) // If we win
- {
- playerHealth = playerHealth + 1000; // increase our health
- playerPower = playerPower + 50; // increase our power
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("\nYOU WON! YOUR HEALTH IS NOW {0}"
- + " AND YOUR POWER IS NOW {1}!", playerHealth, playerPower);
- Console.ForegroundColor = ConsoleColor.White;
- }
- else // If we lose
- {
- Console.WriteLine();
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("YOU WERE DEFEATED. PRESS ENTER TO CONTINUE.");
- Console.ForegroundColor = ConsoleColor.White;
- Console.ReadLine();
- }
- }
- if (fightChoice == 2) // Same as above but boss selected so sent to boss Function
- {
- bool outcome = FightBoss(playerHealth, playerPower);
- if (outcome == true)
- {
- playerHealth = playerHealth + 1000;
- playerPower = playerPower + 50;
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("\nYOU WON! THE BOSS IS NO MORE!");
- Console.ForegroundColor = ConsoleColor.White;
- Console.WriteLine();
- Console.WriteLine("Thank you for playing. Press enter or any key to exit the game.");
- Console.ReadLine();
- break;
- }
- else
- {
- Console.WriteLine();
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("YOU WERE DEFEATED. PRESS ENTER TO CONTINUE.");
- Console.ForegroundColor = ConsoleColor.White;
- Console.ReadLine();
- }
- }
- if (fightChoice == 3) // if we chose 3 we can quit
- {
- break;
- }
- }
- }
- // ### FUNCTIONS ################################################
- // Because this code is designed to show the use of classes the majority of
- // notes here will be on classes
- static void PrintWelcome() // Basic welcome function
- {
- Console.WriteLine("#######################################################################");
- Console.WriteLine("########## ##########");
- Console.WriteLine("########## KILL THE NINJA ##########");
- Console.WriteLine("########## ##########");
- Console.WriteLine("########## Example code for Coding 101 - Twit.tv/code ##########");
- Console.WriteLine("########## by Dean T (twitter: @Dosk3n) ##########");
- Console.WriteLine("#######################################################################");
- Console.WriteLine();
- }
- // Here we fight the evil Ninja! We sent our health and power (which is different depending
- // if you choose to be a Ninja or regular human) to this function in the code above.
- // This then creates an evil ninja using our blueprint (class) we made at the start and
- // names it evilninja. It then compares our health to the healthpoints we gave our evil
- // ninja from our blueprint and if our health is lower it sends back faulse as we lost
- // or it sends back true meaning we won. You could change this part to run a calculation
- // determine the winner but I kept it simple as that wasnt the aim of this code.
- static bool FightEvilNinja(int playerHealth, int playerPower)
- {
- EvilNinja evilninja = new EvilNinja();
- if (playerHealth < evilninja.healthPoints & playerPower < evilninja.powerPoints)
- {
- Console.WriteLine("\nYOU STAND FACE TO FACE AGAINST YOUR OPPONENT...\n");
- Console.WriteLine("YOUR HEALTH LEVEL IS: {0}", playerHealth);
- Console.WriteLine("YOUR POWER LEVEL IS: {0}", playerPower);
- Console.WriteLine("YOUR OPPONENTS HEALTH LEVEL IS {0}", evilninja.healthPoints);
- Console.WriteLine("YOUR OPPONENTS POWER LEVEL IS {0}", evilninja.powerPoints);
- Console.Write("Press Enter to start the fight...");
- Console.ReadLine();
- return false; // We lost to return false
- }
- else
- {
- Console.WriteLine("\nYOU STAND FACE TO FACE AGAINST YOUR OPPONENT...\n");
- Console.WriteLine("YOUR HEALTH LEVEL IS: {0}", playerHealth);
- Console.WriteLine("YOUR POWER LEVEL IS: {0}", playerPower);
- Console.WriteLine("YOUR OPPONENTS HEALTH LEVEL IS {0}", evilninja.healthPoints);
- Console.WriteLine("YOUR OPPONENTS POWER LEVEL IS {0}", evilninja.powerPoints);
- Console.Write("Press Enter to start the fight...");
- Console.ReadLine();
- return true; // We won so return true
- }
- }
- // This does the exact same thing as the fight above but uses out boss class rather than
- // the evil ninja class which means the stats are different based off the blueprint
- static bool FightBoss(int playerHealth, int playerPower)
- {
- EvilBoss evilboss = new EvilBoss();
- if (playerHealth < evilboss.healthPoints & playerPower < evilboss.powerPoints)
- {
- Console.WriteLine("\nYOU STAND FACE TO FACE AGAINST YOUR OPPONENT...\n");
- Console.WriteLine("YOUR HEALTH LEVEL IS: {0}", playerHealth);
- Console.WriteLine("YOUR POWER LEVEL IS: {0}", playerPower);
- Console.WriteLine("YOUR OPPONENTS HEALTH LEVEL IS {0}", evilboss.healthPoints);
- Console.WriteLine("YOUR OPPONENTS POWER LEVEL IS {0}", evilboss.powerPoints);
- Console.Write("Press Enter to start the fight...");
- Console.ReadLine();
- return false;
- }
- else
- {
- Console.WriteLine("\nYOU STAND FACE TO FACE AGAINST YOUR OPPONENT...\n");
- Console.WriteLine("YOUR HEALTH LEVEL IS: {0}", playerHealth);
- Console.WriteLine("YOUR POWER LEVEL IS: {0}", playerPower);
- Console.WriteLine("YOUR OPPONENTS HEALTH LEVEL IS {0}", evilboss.healthPoints);
- Console.WriteLine("YOUR OPPONENTS POWER LEVEL IS {0}", evilboss.powerPoints);
- Console.Write("Press Enter to start the fight...");
- Console.ReadLine();
- return true;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement