# Need help with a health system in a game i'm making

please format code with </> button * homework policy * asking questions Making a game of my computer studies class and i’m struggling to make a health system with just variables and if statement (that 's all were allowed to use).

code looks kinda like this…

float Ehp = 200; //enemy health

``````fill (0); //enemy health
rect (575, 500, 200, 40);
fill (0, 255, 0);
rect (575, 500, Ehp, 40);

if (y > 55 && y < 220)//yellow (does 50 damage)
{
Ehp = 200 - 50;
}

if (y > 55 && y < 220 && Ehp == 150)//yellow - yellow
{
Ehp = 200 - 100;
}

if (y > 90 && y < 187)//orange (does 100 damage)
{
Ehp = 200 - 100;
}

if (y > 120 && y < 159)//red (does 150 damage)
{
Ehp = 200 - 150;
}

if (y > 135 && y < 145)//black (does 200 damage)
{
Ehp = 200 - 200;
}
``````

Everything works except for when I want to shoot the enemy again yellow - yellow one won’t work because as soon as enemy’s health = 150, it instantly changes it to 100. I only want it to change to 100 after another shot is fired.

1 Like

From what I can see, it is because you have two IF statements one after another.
If the first one is triggered, it fulfills the conditions to trigger the second, and it reduces Ehp again.
You could solve that by either placing the second IF statement first OR add an ELSE IF after the first statement.

A simple example:

``````int a = 5;
if(a == 5) a --;
if(a == 4) a --;
println(a); //outputs 3.
a = 5;
if(a == 5) a --;
else if(a==4) a --;
println(a); //outputs 4

a = 5;
if(a==4) a --;
if(a==5) a --;
println(a); //outputs 4
``````
2 Likes

thanks. I’ll give it a try.

1 Like

It didn’t seem to work. Any other ways?

Can you share the changes you made?

I figured it out. thanks

1 Like