HealthBar (for games, etc)

Share your advanced PureBasic knowledge/code with the community.
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

HealthBar (for games, etc)

Post 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)
I like logic, hence I dislike humans but love computers.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Excellent, looks great! Also a good use for your earlier conquests in color morphing. Five stars.
BERESHEIT
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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....
oh... and have a nice day.
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

Scary - I'm much too far to the left.

Nice code.
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post 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.
I like logic, hence I dislike humans but love computers.
walker
Enthusiast
Enthusiast
Posts: 634
Joined: Wed May 05, 2004 4:04 pm
Location: Germany

Post 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
Brice Manuel

Post by Brice Manuel »

Gone into my snippets collection, thank you for sharing.
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

Absotivley Fabulos!!! It is burned in to my snippets collection as well... :D :!: :!:
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
Post Reply