Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Core
- {
- using System;
- using System.IO;
- public enum PlayerTypes
- {
- Name,
- XP,
- Level,
- Marks,
- Perception
- }
- public class PlayerStats
- {
- private string name;
- private int xp;
- private int level;
- private int marks;
- private int perception;
- static void WriteColor(string message, ConsoleColor color, string lineEnding)
- {
- Console.ForegroundColor = color;
- Console.WriteLine(message);
- Console.ResetColor();
- Console.Write(lineEnding);
- }
- public PlayerStats()
- {
- name = null;
- xp = 0;
- level = 0;
- marks = 0;
- perception = 0;
- }
- public string Name
- {
- get { return name; }
- }
- public int XP
- {
- get { return xp; }
- }
- public int Level
- {
- get { return level; }
- }
- public int Marks
- {
- get { return marks; }
- }
- public int Perception
- {
- get { return perception; }
- }
- public void SetStat(PlayerTypes statType, object value)
- {
- switch (statType)
- {
- case PlayerTypes.Name:
- name = (string)value;
- break;
- case PlayerTypes.XP:
- xp = (int)value;
- break;
- case PlayerTypes.Level:
- level = (int)value;
- break;
- case PlayerTypes.Marks:
- marks = (int)value;
- break;
- case PlayerTypes.Perception:
- perception = (int)value;
- break;
- default:
- string errorMessage02 = "Invalid stats type.";
- WriteColor(errorMessage02, ConsoleColor.Red, Environment.NewLine);
- throw new ArgumentException(errorMessage02);
- }
- }
- }
- public class Enemy
- {
- public int health { get; set; }
- public int damage { get; set; }
- public Enemy(int health, int damage)
- {
- this.health = health;
- this.damage = damage;
- }
- }
- class CoreGame
- {
- static void Main(string[] args)
- {
- Random rand = new Random();
- var PlayerStats = new PlayerStats();
- Enemy enemy = new Enemy(100, rand.Next(50, 91));
- bool gameOver = false;
- bool inCombat = false;
- string gameStatsSave = Path.Join(AppDomain.CurrentDomain.BaseDirectory, "stats.txt");
- int enemyStealth = rand.Next(1, 16);
- string? playerName = null;
- int playerHealth = 100;
- int playerXp = 0;
- int playerLevel = 0;
- int playerMarks = 0;
- int playerPerception = 0;
- static void WriteColor(string message, ConsoleColor color, string lineEnding)
- {
- Console.ForegroundColor = color;
- Console.WriteLine(message);
- Console.ResetColor();
- Console.Write(lineEnding);
- }
- if (File.Exists(gameStatsSave))
- {
- using (StreamReader reader = new StreamReader(gameStatsSave))
- {
- string? line = null;
- while ((line = reader.ReadLine()) != null)
- {
- string[] parts = line.Split(':');
- if (Enum.TryParse(parts[0], out PlayerTypes statType))
- {
- switch (statType)
- {
- case PlayerTypes.Name:
- playerName = parts[1].Trim();
- break;
- case PlayerTypes.XP:
- int.TryParse(parts[1], out playerXp);
- break;
- case PlayerTypes.Level:
- int.TryParse(parts[1], out playerLevel);
- break;
- case PlayerTypes.Marks:
- int.TryParse(parts[1], out playerMarks);
- break;
- case PlayerTypes.Perception:
- int.TryParse(parts[1], out playerPerception);
- break;
- default:
- string errorMessage01 = "Failed to load game. Corrupt or incomplete data detected. Please avoid modifying the game files.";
- WriteColor(errorMessage01, ConsoleColor.Red, Environment.NewLine);
- throw new ArgumentException(errorMessage01);
- }
- }
- }
- }
- Console.ForegroundColor = ConsoleColor.Green;
- Console.Write("Game successfully loaded from ");
- Console.Write(gameStatsSave);
- Console.Write(".");
- Console.ResetColor();
- Console.Write(Environment.NewLine);
- }
- if (playerName == null)
- {
- Console.WriteLine("Please enter your name:");
- playerName = Console.ReadLine();
- Console.Write("Welcome, ");
- Console.ForegroundColor = ConsoleColor.Magenta;
- Console.Write(playerName);
- Console.ResetColor();
- Console.Write("!");
- Console.Write(Environment.NewLine);
- }
- else
- {
- Console.Write("Welcome back, ");
- Console.ForegroundColor = ConsoleColor.Magenta;
- Console.Write(playerName);
- Console.ResetColor();
- Console.Write("!");
- Console.Write(Environment.NewLine);
- }
- while (!gameOver && !inCombat)
- {
- Console.WriteLine("##############################");
- Console.WriteLine("What do you want to do?" + Environment.NewLine);
- Console.WriteLine("1. Attempt to search for enemies.");
- Console.WriteLine("2. Use a first aid kit.");
- Console.WriteLine("3. Check your stats.");
- Console.WriteLine("4. Check your skills.");
- Console.WriteLine("9. Reset your stats.");
- Console.WriteLine("0. Quit the game.");
- Console.WriteLine("##############################");
- string? playerAction = Console.ReadLine();
- switch (playerAction)
- {
- case "1":
- Console.WriteLine("You attempt to search for enemies...");
- // Random chance from 1 to 10
- int spotChance = rand.Next(1, 11);
- // Random chance from 1 to 50 to gain level in pereception
- int perceptionGain = rand.Next(1, 51);
- if (perceptionGain == 1)
- {
- playerPerception++;
- Console.Write("You have gained a level in ");
- Console.ForegroundColor = ConsoleColor.Cyan;
- Console.Write("Perception");
- Console.ResetColor();
- Console.Write("!");
- Console.Write(Environment.NewLine);
- }
- // If player's perception skill is higher than 5
- if (playerPerception > 5)
- {
- spotChance += playerPerception - 5; // Add the difference to success chance
- }
- // Player spots a enemy
- if (spotChance >= enemyStealth)
- {
- Console.WriteLine("You spot an enemy nerby!" + Environment.NewLine);
- FoundAEnemy();
- }
- else // Player fails to spot a enemy
- {
- Console.WriteLine("You fail to spot any enemies." + Environment.NewLine);
- }
- break;
- case "2":
- Console.WriteLine("You use a first aid kit!" + Environment.NewLine);
- playerHealth += 10;
- Console.Write("Your now have ");
- Console.ForegroundColor = ConsoleColor.Green;
- Console.Write(playerHealth);
- Console.ResetColor();
- Console.Write(" health.");
- Console.Write(Environment.NewLine);
- Console.Write(Environment.NewLine);
- break;
- case "3":
- Console.Write(Environment.NewLine);
- Console.Write("Your current stats ## Name: ");
- Console.ForegroundColor = ConsoleColor.Magenta;
- Console.Write(playerName);
- Console.ResetColor();
- Console.Write(", XP: ");
- Console.ForegroundColor = ConsoleColor.Blue;
- Console.Write(playerXp);
- Console.ResetColor();
- Console.Write(", Level: ");
- Console.ForegroundColor = ConsoleColor.Green;
- Console.Write(playerLevel);
- Console.ResetColor();
- Console.Write(", Marks: ");
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.Write(playerMarks);
- Console.ResetColor();
- Console.Write(Environment.NewLine);
- Console.Write("Your current health ## Health: ");
- Console.ForegroundColor = ConsoleColor.Green;
- Console.Write(playerHealth);
- Console.ResetColor();
- Console.Write(Environment.NewLine);
- Console.Write(Environment.NewLine);
- break;
- case "4":
- Console.Write(Environment.NewLine);
- Console.Write("Your current skills ## Perception: ");
- Console.ForegroundColor = ConsoleColor.Cyan;
- Console.Write(playerPerception);
- Console.ResetColor();
- Console.Write(Environment.NewLine);
- Console.Write(Environment.NewLine);
- break;
- case "9":
- WriteColor("Are you sure you want to reset your stats? This will restart your game.(Y/N)", ConsoleColor.Red, Environment.NewLine);
- string? reset = Console.ReadLine();
- if (reset?.ToLower() == "y" || reset?.ToLower() == "yes")
- {
- // Reset the stats
- playerName = null;
- playerHealth = 100;
- playerXp = 0;
- playerLevel = 0;
- playerMarks = 0;
- playerPerception = 0;
- // Delete the save and restart game
- if (File.Exists(gameStatsSave))
- {
- File.Delete(gameStatsSave);
- WriteColor("Stats reset. Save file deleted.", ConsoleColor.Red, Environment.NewLine);
- RestartGame();
- }
- else
- {
- WriteColor("Stats reset. No save file found.", ConsoleColor.Red, Environment.NewLine);
- RestartGame();
- }
- }
- break;
- case "0":
- WriteColor("Are you sure you want to quit the game.(Y/N)", ConsoleColor.Red, Environment.NewLine);
- string? quit = Console.ReadLine();
- if (quit?.ToLower() == "y" || quit?.ToLower() == "yes")
- {
- // Exit the game and save
- using (StreamWriter writer = new StreamWriter(gameStatsSave))
- {
- writer.WriteLine("Name: " + playerName);
- writer.WriteLine("XP: " + playerXp);
- writer.WriteLine("Level: " + playerLevel);
- writer.WriteLine("Marks: " + playerMarks);
- writer.WriteLine("Perception: " + playerPerception);
- }
- Console.ForegroundColor = ConsoleColor.Green;
- Console.Write("Game saved to ");
- Console.Write(gameStatsSave);
- Console.Write(".");
- Console.ResetColor();
- Console.Write(Environment.NewLine);
- Console.ForegroundColor = ConsoleColor.Red;
- Console.Write("Quitting game...");
- Console.ResetColor();
- Environment.Exit(0);
- }
- break;
- default:
- WriteColor("Invaild user input.", ConsoleColor.Red, Environment.NewLine);
- break;
- }
- }
- void RestartGame()
- {
- // Close the current instance of the game
- Console.WriteLine("Restarting game...");
- Thread.Sleep(2000);
- System.Diagnostics.Process.GetCurrentProcess().Kill();
- // Start a new instance of the game
- string gameExecutablePath = System.Reflection.Assembly.GetEntryAssembly().Location;
- System.Diagnostics.Process.Start(gameExecutablePath);
- }
- void FoundAEnemy()
- {
- inCombat = true;
- Console.WriteLine("You open fire on the enemy!" + Environment.NewLine);
- while (!gameOver && inCombat)
- {
- // Damage to deal to the enemy
- int enemyDamage = rand.Next(50, 100);
- // Chance for enemy or player to hit ( 50/50 )
- int hitChance = rand.Next(0, 2);
- // Rewards for killing
- int gainXp = rand.Next(0, 20);
- int earnMarks = rand.Next(0, 20);
- if (hitChance == 1)
- {
- enemy.health -= enemyDamage;
- Console.Write("You manage to hit the enemy and deal ");
- Console.ForegroundColor = ConsoleColor.Red;
- Console.Write(enemyDamage);
- Console.ResetColor();
- Console.Write(" damage. ");
- Console.Write(Environment.NewLine);
- if (enemy.health <= 0)
- {
- inCombat = false;
- enemy.health = 100;
- playerXp += gainXp;
- playerMarks += earnMarks;
- Console.Write("You successfully kill the enemy!" + Environment.NewLine);
- Console.Write("You gain ");
- Console.ForegroundColor = ConsoleColor.Blue;
- Console.Write(gainXp);
- Console.ResetColor();
- Console.Write(" XP and ");
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.Write(earnMarks);
- Console.ResetColor();
- Console.Write(" marks.");
- Console.Write(Environment.NewLine);
- Console.Write(Environment.NewLine);
- }
- else
- {
- Console.WriteLine("##############################");
- Console.WriteLine("What do you want to do?" + Environment.NewLine);
- Console.WriteLine("1. Attempt to fire at the enemy again.");
- Console.WriteLine("2. Attempt Take cover.");
- Console.WriteLine("3. Attempt to retreat away..");
- Console.WriteLine("##############################");
- string? playerCombatAction = Console.ReadLine();
- switch (playerCombatAction)
- {
- case "1":
- Console.WriteLine("You open fire on the enemy!" + Environment.NewLine);
- break;
- case "2":
- Console.WriteLine("You attempt to take cover." + Environment.NewLine);
- break;
- case "3":
- Console.WriteLine("You attempt to retreat." + Environment.NewLine);
- inCombat = false;
- break;
- default:
- WriteColor("Invalid input.", ConsoleColor.Red, Environment.NewLine);
- break;
- }
- }
- }
- else
- {
- Console.Write("You fire at the enemy but miss." + Environment.NewLine);
- }
- if (hitChance == 0)
- {
- playerHealth -= enemy.damage;
- Console.Write("The enemy hits you and deals ");
- Console.ForegroundColor = ConsoleColor.Red;
- Console.Write(enemy.damage);
- Console.ResetColor();
- Console.Write(" damage.");
- Console.Write(Environment.NewLine);
- if (playerHealth <= 0)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.Write("Game over. You died." + Environment.NewLine);
- Console.ResetColor();
- gameOver = true;
- // Reset the stats
- playerName = null;
- playerHealth = 100;
- playerXp = 0;
- playerLevel = 0;
- playerMarks = 0;
- playerPerception = 0;
- // Delete save
- if (File.Exists(gameStatsSave))
- {
- File.Delete(gameStatsSave);
- }
- else
- {
- }
- }
- else
- {
- Console.WriteLine("##############################");
- Console.WriteLine("What do you want to do?" + Environment.NewLine);
- Console.WriteLine("1. Attempt to fire at the enemy again.");
- Console.WriteLine("2. Attempt Take cover.");
- Console.WriteLine("3. Attempt to retreat away..");
- Console.WriteLine("##############################");
- string? playerCombatAction = Console.ReadLine();
- switch (playerCombatAction)
- {
- case "1":
- Console.WriteLine("You open fire on the enemy!" + Environment.NewLine);
- break;
- case "2":
- Console.WriteLine("You attempt to take cover." + Environment.NewLine);
- break;
- case "3":
- Console.WriteLine("You attempt to retreat." + Environment.NewLine);
- inCombat = false;
- break;
- default:
- WriteColor("Invaild user input.", ConsoleColor.Red, Environment.NewLine);
- break;
- }
- }
- }
- if (playerXp >= 100)
- {
- playerLevel++;
- playerXp = 0;
- Console.Write("You have leveled up to level: ");
- Console.ForegroundColor = ConsoleColor.Green;
- Console.Write(playerLevel);
- Console.ResetColor();
- Console.Write("!");
- Console.Write(Environment.NewLine);
- Console.Write(Environment.NewLine);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement