I made a health bar function that can change the bar's color based on your health. I'm going to use it so I thought I'd post it here. Maybe it's useful for an RPG or RTS?
Here's the main function
-First 4 parameters work like the box command.
-"Health" is the amount of health that whoever the health bar is for has.
-"MaxHealth" is the maximum health that they can have.
-HighHealthColor,MediumHealthColor,LowHealthColor,AlmostDeadHealthColor:
The color of the health bar when that thing is the case. For example, HighHealthColor would say what color the health bar is when you have high health. I may make this green.
function MakeHealthBar(Left,Top,Right,Bottom,Health as float,MaxHealth as float,HighHealthColor as dword,MediumHealthColor as dword,LowHealthColor as dword,AlmostDeadHealthColor)
HPLength as float
HTMH as float
TheRight as float
HPLength=Right-Left
HTMH=Health/MaxHEalth
TheRight=Left+HPLength*HTMH
box Left,Top,Right,Bottom,rgb(155,155,55),0,rgb(155,155,155),0
if Health>=MaxHealth-(MaxHealth/4) then Color=HighHEalthColor
if Health>=MaxHealth-2*(MaxHealth/4) and Health<=MaxHealth-(MaxHealth/4) then Color=MediumHEalthColor
if Health>=MaxHealth-3*(MaxHealth/4) and Health<=MaxHealth-2*(MaxHealth/4) then Color=LowHEalthColor
if Health>=MaxHealth-4*(MaxHealth/4) and Health<=MaxHealth-3*(MaxHealth/4) then Color=AlmostDeadHEalthColor
box Left,Top,TheRight,bottom,COLOR,0,COLOR,0
endfunction
If you include the main function in your code, you can use a simpler one that takes only one color.
Simpler:
function SimpleHealthBar(zLeft,zTop,zRight,zBottom,Health as float,MaxHealth as float,COLOR as dword)
MakeHealthBar(zLeft,zTop,zRight,zBottom,Health,MaxHealth,COLOR,COLOR,COLOR,COLOR)
endfunction
Here's an example:
sync on
sync rate 60
autocam off
set text size 14
set text font "verdana"
HP=100
MaxHP=200
Increase=2
rem Make shere
Make object sphere 1,40
rem Make grid
Make matrix 1,10000,10000,20,20
position matrix 1,-5000,0,-5000
rem Place camera
position camera 0,100,-500
do
text 0,0,"UP/DOWN ARROW KEYS CHANGE HEALTH. WASD MOVES THE SPHERE."
rem Change health
inc HP,Increase*( upkey()-downkey() )
if HP<0 then HP=0
if HP>MaxHP then HP=MaxHP
rem Get object screen coords
X=OBject screen x(1)
Y=Object screen y(1)-10
rem small health bar
SimpleHealthBar(X,Y,X+30,Y+10,HP,MaxHP,rgb(0,255,0))
rem Print HP.
Center Text X,Y+5,str$(HP)+" / "+str$(MaxHP)
rem Big health bar
SimpleHealthBar(0,440,640,470,HP,MaxHP,rgb(0,255,0))
rem Control the sphere
move object 1,3*(KEystate(17)-KEystate(31))
turn object left 1, keystate(30)-Keystate(32)
rem Point the camera at the sphere
point camera object position x(1),OBject position y(1),object position z(1)
sync
loop
function SimpleHealthBar(zLeft,zTop,zRight,zBottom,Health as float,MaxHealth as float,COLOR as dword)
MakeHealthBar(zLeft,zTop,zRight,zBottom,Health,MaxHealth,COLOR,COLOR,COLOR,COLOR)
endfunction
function MakeHealthBar(Left,Top,Right,Bottom,Health as float,MaxHealth as float,HighHealthColor as dword,MediumHealthColor as dword,LowHealthColor as dword,AlmostDeadHealthColor)
HPLength as float
HTMH as float
TheRight as float
HPLength=Right-Left
HTMH=Health/MaxHEalth
TheRight=Left+HPLength*HTMH
box Left,Top,Right,Bottom,rgb(155,155,55),0,rgb(155,155,155),0
if Health>=MaxHealth-(MaxHealth/4) then Color=HighHEalthColor
if Health>=MaxHealth-2*(MaxHealth/4) and Health<=MaxHealth-(MaxHealth/4) then Color=MediumHEalthColor
if Health>=MaxHealth-3*(MaxHealth/4) and Health<=MaxHealth-2*(MaxHealth/4) then Color=LowHEalthColor
if Health>=MaxHealth-4*(MaxHealth/4) and Health<=MaxHealth-3*(MaxHealth/4) then Color=AlmostDeadHEalthColor
box Left,Top,TheRight,bottom,COLOR,0,COLOR,0
endfunction
Ummm...