Easier way to write if else statement

Hello,

and welcome to the forum! Nice to have you here!

The command if() can be replaced with switch() (in this case).

Approach with a For-loop

In this case: You could also use a for-loop

for(int i=0; i<=lives; i++) 
    image(heart, (heartX1-15) + i*(heartWH+12) , heartY , heartWH, heartWH);

This for-loop

  • uses lives as upper bound (i<=lives), so that the number of loops is determined by the variable lives (how many hearts we see)
  • uses i to calculate the x position for each heart: (heartX1-15) + i*(heartWH+12). This can be simplified.

Remark

I would recommend to use image only with 3, not with 5 parameters. This slows things down. Avoid this everywhere in your game!

Instead apply resize() command once to “heart” in setup().

Chrisir

3 Likes