GLBasic forum

Codesnippets => Code Snippets => Topic started by: Synthetic on 2005-Dec-18

Title: Adjustable HP Bar
Post by: Synthetic on 2005-Dec-18
// --------------------------------- //
// Project: Health Bar
// Start: Sunday, December 18, 2005
// IDE Version: 2.50519


// This little function will make an adjustable bar good for health points in
// a game or just about anything else you can think of.
// The function breaks down as follows:
// HBhp - This is the current amount of hp your character has.
// HBhpmax - This is the max amount of hp your character has.
// HBspnum - This is the sprite number of your bar graphic loaded with LOADSPRITE.
// HBbartype - A setting of 0 makes a horizontal bar, 1 makes a vertical bar.
// HBbarsize - This is the max size of the bar that you want it to adjust to.
// HBspwidth - This is the width of your bar graphic.
// HBspheight - This is the height of your bar graphic.
// HBx - This is the x position on screen you wish the bar to appear at.
// HBy - This is the y position on screen you wish the bar to appear at.


LIMITFPS 60
LOADSPRITE "bar.bmp",1 //For this test, I just used a red 12x12 bmp.

//*******************************************************************
//Delcare variables
//*******************************************************************
a = 1
horizontalhp = 250
verticalhp = 100
hpmax = 500

//*******************************************************************
//Main Loop
//*******************************************************************
WHILE a = 1
//Setup arrow keys to control current amount of hp
IF KEY(205) = 1 THEN horizontalhp = horizontalhp + 5
IF KEY(203) = 1 THEN horizontalhp = horizontalhp - 5
IF KEY(200) = 1 THEN verticalhp = verticalhp - 5
IF KEY(208) = 1 THEN verticalhp = verticalhp + 5
IF horizontalhp > hpmax THEN horizontalhp = hpmax // Keep horizontal hp from going over max amount
IF verticalhp > hpmax THEN verticalhp = hpmax // Keep vertical hp from going over max amount
IF horizontalhp < 0 THEN horizontalhp = 0 // Keep horizontal hp from goin under 0
IF verticalhp < 0 THEN verticalhp = 0 // Keep vertical hp from going under 0
//Call health bar function
HealthBar(horizontalhp,hpmax,1,0,100,12,12,0,150) //Make horizontal bar
HealthBar(verticalhp,hpmax,1,1,100,12,12,150,150) //Make vertical bar
//Show current hp for both bars
PRINT horizontalhp,0,150
PRINT verticalhp,150,150
SHOWSCREEN
WEND


FUNCTION HealthBar: HBhp,HBhpmax,HBspnum,HBbartype,HBbarsize,HBspwidth,HBspheight,HBx,HBy
//Setup HP bar
HBhpbar = (HBhp*HBbarsize)/HBhpmax
HBhpbarx = HBhpbar-INTEGER(HBhpbar)
IF HBhpbarx <= .5
 HBhpbarresult = INTEGER(HBhpbar-1)
 ELSE
 HBhpbarresult = INTEGER(HBhpbar)
ENDIF
IF HBbartype = 0 THEN STRETCHSPRITE HBspnum,HBx,HBy,HBhpbarresult,HBspheight
IF HBbartype = 1 THEN STRETCHSPRITE HBspnum,HBx,HBy,HBspwidth,HBhpbarresult
ENDFUNCTION
Title: Adjustable HP Bar
Post by: Kitty Hello on 2005-Dec-19
Very nice. If you just use a red rect bitmap, try using FILLRECT instead. ;)