LoginLogin
Might make SBS readonly: thread

Help with a complex heart system

Root / Programming Questions / [.]

Z_E_R_OCreated:
Just looking for advice on a simple heart system It may seem simple but you can find and buy more hearts in game and I wanted to set animations to the hearts I've tried using for statements but my problem is I cant Figure out how to do it correctly

A heart system like in LoZ games? What are trying to do specifically? I.e increase max hearts when you find one, heal, take damage etc.

A heart system like in LoZ games? What are trying to do specifically? I.e increase max hearts when you find one, heal, take damage etc.
Yeah! Something like that like I want to be able to animate the heart that is active and honestly all the above I've always struggled with arrays and Strings Sorry I really didnt know how to word the question

Normally I wouldn’t go out of my way to hand over the answer, but this was something I also had on m “to-do” list. I have published a demo that demonstrates a heart system. You can take damage, heal and increase max hearts in this demo. It isn’t perfect, but I hope it points you in the right direction. Key: 4839Y23NJ (SB4)

Here is my take, I guess it was answered before I could finish my demo. I spent a fair amount of time on it, so you get it anyway. Let me know if you find any bugs. This one is SmileBasic 3DS, shouldn't be many changes to allow it to run on the Switch version.
OPTION STRICT

DIM HEART_ID[3]
HEART_ID[0] = 220 'EMPTY
HEART_ID[1] = 221 'HALF
HEART_ID[2] = 222 'FULL

VAR PAGE_SPRITE = 4

VAR SCREEN_W = 400
VAR SCREEN_H = 240

VAR POINTS_PER_HEART = 4
VAR MAX_POINTS = 256

'MEASURE FIRST HEART SPRITE
VAR U, V, HEART_W, HEART_H
SPDEF HEART_ID[0] OUT U, V, HEART_W, HEART_H

VAR B, POINTS = POINTS_PER_HEART * 3 'START WITH 3 HEARTS
VAR POINT_STEP = .5

ACLS
B = BUTTON()
'LEFT AND RIGHT TO ADD/SUBTRACT POINTS
'B TO EXIT
WHILE (B AND #B) == 0
 GCLS
 DRAW_HEARS POINTS
 B = BUTTON()
 IF (B AND #LEFT) != 0 THEN INC POINTS, POINT_STEP
 IF (B AND #RIGHT) != 0 THEN DEC POINTS, POINT_STEP
 'KEEP BETWEEN 0 AND MAX
 POINTS = MAX(0, MIN(MAX_POINTS, POINTS))
 'SHOW POINTS AS A NUMBER
 LOCATION 0, 28
 PRINT POINTS;"   ";
WEND

END

DEF DRAW_HEARTS POINTS
 VAR REMAINING
 VAR X = SCREEN_W - HEART_W
 VAR Y = 0

 'EXIT IF NOTHING TO DRAW
 IF POINTS <= 0 THEN RETURN

 REMAINING = POINTS
 WHILE REMAINING >= POINTS_PER_HEART
  'WRAP TO NEXT LINE IF NEEDED
  IF X < 0 THEN
   X = SCREEN_W - HEART_W
   Y = Y + HEART_W
  ENDIF

  'SUBTRACT POINTS FOR THIS HEART
  REMAINING = REMAINING - POINTS_PER_HEART
  'DRAW A FULL HEART
  DRAW_HEART HEART_ID[LEN(HEART_ID) - 1], X, Y
  'MOVE A STEP BACK FOR THE NEXT ONE
  X = X - HEART_W
 WEND

 IF REMAINING > 0 THEN
  'DRAW LAST HEART WITH PARTIAL HEALTH
  IF X < 0 THEN
   'AGAIN WRAP IF NEEDED
   X = SCREEN_W - HEART_W
   Y = Y + HEART_H
  ENDIF
  DRAW_HEART HEART_ID[(REMAINING * (LEN(HEART_ID) - 1)) / POINTS_PER_HEART], X, Y
 ENDIF
END

DEF DRAW_HEART ID, X, Y
 VAR U, V, W, H
 'DRAW A HEART IMAGE COPIED FROM THE SPRITE PAGE
 SPDEF ID OUT U, V, W, H
 GCOPY PAGE_SPRITE, U, V, U+W, V+H, X, Y, FALSE
END

I really appreciate you both! I love this community! I've been having a tough time and programming is my hobby. Thanks a bunch! I'll try them when I get home