Skip to content

Instantly share code, notes, and snippets.

@kenpower
Created November 9, 2023 10:57
Show Gist options
  • Save kenpower/c62d54bce83f611b31d88b20d2818575 to your computer and use it in GitHub Desktop.
Save kenpower/c62d54bce83f611b31d88b20d2818575 to your computer and use it in GitHub Desktop.
class Health{
private:
int health;
Player& player;
public:
Health(Player& player) : player(player) {}
void change(int amount){
health += amount;
if (health > 100)
{
health = 100;
}
if (health < 0)
{
health = 0;
}
if (health < 25)
player.speed = SLOW;
}
};
//...
Health health;
void takePotion(int potionStrength)
{
health.change(potionStrength);
}
void takeDamage(int damage)
{
health.change(-potionStrength);
}
void fall()
{
health.change(-5);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment