You are on page 1of 2

Player health system

To use the code, I suggest using Visual Studio Code

using System;

public class Player

private int maxHealth;

private int currentHealth;

public Player(int maxHealth)

this.maxHealth = maxHealth;

this.currentHealth = maxHealth;

public void TakeDamage(int damageAmount)

currentHealth -= damageAmount;

if (currentHealth <= 0)

currentHealth = 0;

Die();

Console.WriteLine("Player took " + damageAmount + " damage. Current Health: " + currentHealth);

public void Heal(int healAmount)

currentHealth += healAmount;

if (currentHealth > maxHealth)

currentHealth = maxHealth;

}
Console.WriteLine("Player healed for " + healAmount + ". Current Health: " + currentHealth);

private void Die()

Console.WriteLine("Player has died!");

// Additional logic for handling player death can be added here

public int GetCurrentHealth()

return currentHealth;

class Program

static void Main(string[] args)

Player player = new Player(100); // create a player with maximum health of 100

// Simulate taking damage and healing

player.TakeDamage(20);

player.Heal(10);

player.TakeDamage(50);

player.TakeDamage(50); // this should result in player death

Console.ReadLine(); // Keep the console window open

You might also like