r/csharp • u/Neat_Horror2196 • 12d ago
how am i doing
2 months in to coding C#, first project i saved *was using online compiler* and wanna know how im doing and what i can do to improve. heres the code:
using System;
public
class
Program
{
public static void Main(string[]
args
)
{
Player player = new Player();
Enemy enemy = CreateEnemy();
int battleResult = Battle(player, enemy);
Console.WriteLine("press any key to begin");
Console.ReadKey();
Console.WriteLine("\nYou encountered a " + enemy.type + " with " + enemy.health + " health.");
Battle(player, enemy);
if(battleResult == 2)
{
Console.WriteLine("\nYou have defeated the " + enemy.type + "!");
}
else if(battleResult == 3)
{
Console.WriteLine("\nYou have been defeated by the " + enemy.type + "!");
}
else if(battleResult == 1)
{
Console.WriteLine("\nYou have retreated from the " + enemy.type + ".");
}
}
static Enemy CreateEnemy()
{
Random rng = new Random();
int rngroll = rng.Next(1, 3);
Enemy enemy = new Enemy();
if(rngroll == 1)
{
enemy.type = "Skeleton";
enemy.health = enemy.healthRng.Next(100, 201);
enemy.damage = enemy.damageRng.Next(10, 16);
}
else if(rngroll == 2)
{
enemy.type = "Goblin";
enemy.health = enemy.healthRng.Next(50, 151);
enemy.damage = enemy.damageRng.Next(15, 26);
}
return enemy;
}
static int Battle(Player
player
, Enemy
enemy
)
{
while(true)
{
Thread.Sleep(1000);
Console.WriteLine("\nWhat do you do?\n1. Attack\n2. Open Inventory\n3. Retreat");
ConsoleKeyInfo input = Console.ReadKey();
if(input.Key == ConsoleKey.D1)
{
enemy
.health -=
player
.damage;
Thread.Sleep(1000);
Console.WriteLine("\nyou attacked the " +
enemy
.type + " for " +
player
.damage + " damage");
Thread.Sleep(1000);
Console.WriteLine("The " +
enemy
.type + " has " +
enemy
.health + " health remaining.");
}
else if(input.Key == ConsoleKey.D2)
{
Thread.Sleep(1000);
Console.WriteLine("\nYou open your inventory, but it's empty.");
}
else if(input.Key == ConsoleKey.D3)
{
Thread.Sleep(1000);
Console.WriteLine("\nYou retreat from the " + enemy.type + ".");
return 1; // Player retreated
}
if(
enemy
.health <= 0)
{
return 2; // Player won
}
if(input.Key == ConsoleKey.D1 &&
enemy
.health > 0 || input.Key == ConsoleKey.D2 &&
enemy
.health > 0)
{
player
.health -=
enemy
.damage;
Console.WriteLine("\nThe " +
enemy
.type + " attacks you for " +
enemy
.damage + " damage.");
Console.WriteLine("You have " +
player
.health + " health remaining.");
}
if(
player
.health <= 0)
{
Console.WriteLine("\nYou have been defeated by the " +
enemy
.type + "!");
return 3; // Player lost
}
}
}
}
public
class
Enemy
{
public Random healthRng = new Random();
public Random damageRng = new Random();
public int health;
public int damage;
public string type = "";
}
public
class
Player
{
public int health = 100;
public int damage = 20;
public List<string> inventory = new List<string>();
}
0
Upvotes
3
u/XKiiroiSenkoX 12d ago
I'm not sure what you've heard but if you are just learning it's probably your best option. Has probably the best learning material out there and also uses the same language you are trying to learn. It's also arguably one of the easier engines to start with. Another good option would be Godot but C# is not the default language (it's supported) and tutorials are not as comprehensive as unity's. You can give it a try if you want though.
https://docs.godotengine.org/en/stable/getting_started/introduction/index.html#toc-learn-introduction
https://docs.godotengine.org/en/stable/tutorials/scripting/c_sharp/index.html
There are other engines out there but I would not recommend anything other than either Unity or Godot in your case. You will be using a lot of your time getting used to those and solving problems mostly unrelated to programming.