Page 1 of 1

HealthBar (for games, etc)

Posted: Fri Apr 13, 2007 7:44 pm
by Joakim Christiansen
I guess it's not hard to make one yourself, but someone might like it, feel free to improve!

Code: Select all

#DarkGray = 3158064

Procedure DrawBar(X,Y,Width.f,Height,Value.f,MaxValue,Text$,FullColor,EmptyColor)
  Protected Color
  If Value > MaxValue: Value = MaxValue: EndIf
  Color = RGB((Red(FullColor)-Red(EmptyColor))/MaxValue*Value+Red(EmptyColor), (Green(FullColor)-Green(EmptyColor))/MaxValue*Value+Green(EmptyColor), (Blue(FullColor)-Blue(EmptyColor))/MaxValue*Value+Blue(EmptyColor))
  Box(X,Y,Width,Height,#Gray) ;Border
  Box(X+1,Y+1,Width-2,Height-2,#DarkGray) ;Background
  Box(X+1,Y+1,(Width-2) / MaxValue*Value,Height-2,Color) ;Bar
  DrawText(X+Width/2-TextWidth(Text$)/2,Y+Height/2-TextHeight(Text$)/2,Text$,#White) ;Text
EndProcedure

Define Health = 200

InitKeyboard():InitSprite():OpenScreen(640,480,32,"Test")
Delay(1000)
Repeat
  Delay(40): If Health: Health - 1: EndIf
  FlipBuffers():ClearScreen(#Black)
  If StartDrawing(ScreenOutput())
    DrawingMode(#PB_2DDrawing_Transparent)
    DrawBar(10,10,200,20,Health,200,"Health",#Green,#Red)
    StopDrawing()
  EndIf
  ExamineKeyboard()
Until KeyboardPushed(#PB_Key_Escape)

Posted: Fri Apr 13, 2007 8:21 pm
by netmaestro
Excellent, looks great! Also a good use for your earlier conquests in color morphing. Five stars.

Posted: Fri Apr 13, 2007 8:25 pm
by Kaeru Gaman
yo, looks nice, but I think it's far from performance....

if you meant it as example how to morph the color: great.

but the approach for displaying must be different to make it useful....

Posted: Fri Apr 13, 2007 8:25 pm
by rsts
Scary - I'm much too far to the left.

Nice code.

Posted: Fri Apr 13, 2007 8:57 pm
by Joakim Christiansen
netmaestro wrote:Also a good use for your earlier conquests in color morphing.
I knew that would come in handy some day :P
And thanks for you nice comment!
the approach for displaying must be different to make it useful....
It's useful for me, but feel free to share your ideas or improve it.

Posted: Sat Apr 14, 2007 8:38 pm
by walker
very nice example...
simply add

Code: Select all

CompilerIf #PB_Compiler_OS = #PB_OS_Linux
#gray=$787878
#white = $FFFFFF
#black = 0
#green=$00FF00
#red=$0000FF
CompilerEndIf
at the top of the source and it will work on Linux too :D

Posted: Sun Apr 15, 2007 4:42 pm
by Brice Manuel
Gone into my snippets collection, thank you for sharing.

Posted: Sat Apr 21, 2007 4:28 am
by Rook Zimbabwe
Absotivley Fabulos!!! It is burned in to my snippets collection as well... :D :!: :!: