Here's a function you could copy into a project and use to draw a customisable health/stamina/mana bar! I did something similar in a game "Subs 2" @SteveZX81 and I worked on. It even has an easy colour changing effect as the bar changes size, which is really easy to customise too!
function drawBar(x, y, width, height, value, maxValue, colourEmpty, colourFull)
w = width/maxValue * value
t = w/width
col = interpolate(ease_in_out, colourEmpty, colourFull, t)
box(x, y, w, height, col, false)
return void
With this in your program somewhere (make sure it's outside your main loop, this is a function definition and must be outside other loops/function definitions), you can simply call it and draw a hud bar anywhere on screen. We just need to pass it the values we want to use. For example, here it is in a simple program:
// Some health variables to use for the length of the bar
maxHealth = 20
health = maxHealth
loop
clear()
// Call our function, draw a bar at screen location (100, 100), with a maximum width of 300 pixels, height of 50 pixels:
drawBar(100, 100, 300, 50, health, maxHealth, red, lime)
// Decrease the health variable to show the bar reducing and changing coour
health -= 0.1
// Limit the variable so it stops at 0
health = max(health, 0)
update()
repeat
You could call that function with any set of variables, doesn't have to be health! If you had maxStamina and stamina variables, they would also work and you could use different colours, for example:
// Draw a bar at screen location (50, 50) with max width of 250 pixels, height of 50 pixels colour shifting from yellow to gold (very subtle, looks quite nice!)
drawBar(50, 50, 250, 50, stamina, maxStamina, gold, yellow)
I hope these examples help you out!