Skip to content

Instantly share code, notes, and snippets.

@JamesC01
Created September 22, 2017 23:54
Show Gist options
  • Select an option

  • Save JamesC01/79560842b2d21f628adf73730636abf3 to your computer and use it in GitHub Desktop.

Select an option

Save JamesC01/79560842b2d21f628adf73730636abf3 to your computer and use it in GitHub Desktop.
Pokemon-style Battle Game
class Program
{
// This is a Pokemon style text-based battle game.
static void Main(string[] args)
{
bool playing = true;
Console.Write("Enter your battle monster's name: ");
string playerName = Console.ReadLine();
// Instantiate Player objects.
Player player = new Player(100, 100, playerName);
Player enemy = new Player(100, 100, "Opponent");
// Game loop
while (playing)
{
Console.Clear();
Console.WriteLine("{0}'s HP: {1}/{2}", player.Name, player.health, player.MaxHealth);
Console.WriteLine("{0}'s HP: {1}/{2}", enemy.Name, enemy.health, enemy.MaxHealth);
Console.WriteLine();
Console.WriteLine("You would you like to do?");
Console.WriteLine("a - Attack opposing monster");
Console.WriteLine("f - Flee the fight");
Console.Write("move>");
string move = Console.ReadLine();
if (move == "a")
{
player.Attack(enemy);
}
else if (move == "f")
{
playing = false;
}
else
{
Console.WriteLine("Enter a valid option!");
}
}
Console.WriteLine("Thanks for playing! Game by James Czekaj.");
Console.ReadLine();
}
}
class Player
{
public int MaxHealth { get; set; }
public int health { get; set; }
public string Name { get; set; }
public Player(int maxHealth, int currentHealth, string name)
{
MaxHealth = maxHealth;
health = currentHealth;
Name = name;
}
public void Attack(Player opponent)
{
// Generate a random number and assign it to attack variable.
Random randomNum = new Random();
int attack = randomNum.Next(10, 20);
// TODO Figure out a way to exit game loop once a player has 0 health.
if (opponent.health - attack > 0)
{
opponent.health -= attack;
}
else if (opponent.health - attack < 0) // Set health back to 0 if it goes minus.
{
opponent.health = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment