Jump to content


Photo
- - - - -

Cool Looking Healthbars


  • Please log in to reply
34 replies to this topic

#1 Shadowrend

Shadowrend

    Master of Shadows

  • GMC Member
  • 1572 posts
  • Version:GM8

Posted 21 March 2010 - 03:47 PM

  • Title: Cool Looking Healthbars
  • Description: a tutorial on how to make good looking health bars.
  • GM Version: GM7 or GM8
  • Registered: nah
  • File Type: .gmk
  • File Size: 9 kb
  • File Link: Download

Tired of lame healhbars?
Do you want them not to be one colored rectangles?
Do you want them to look cool?
Do you want them to be any shape?


Then!, simply read the following tutorial:

Hello. I decided to create a tutorial on how to make good looking health bars.
The things that you will need are:
  • Game Maker Pro (GM 7 or GM8)
  • An Health bar idea
  • Medium coding knowledge
  • Some time

By default GM draws a healthbar, using a rectangle that changes color. For some games that is good, but some games would rather like a healthbar that is actually and image or something else. Some one uses text to display health, or several sprites. This tutorial will throw all of those in the recycle bin, and add a new way of healthbars. One that's more healthier Posted Image ...

We will begin by starting ours Game Maker. For this tutorial I am using Game Maker 8, becaouse it supports alpha transparency, and other things that will make the healthbar look better.

STEP 1: Sprtiting
Create a new sprite and name it "spr_bar".
The tutorial bar will use several images in that sprite. These are:
  • The border
  • The color filling
  • Shadow
  • Highlight
  • Light Reflection
Here is a screenshot of my bar.
Posted Image
As you can see i used 5 images. The spriting is complete.

STEP 2: Coding
Create a new script and name it "draw_healthbar". Paste this code into it.
/*
Draw Healthbar Script

draw_hpbar(x,y,value);
x - x position to draw the bar at
y - y position to draw the bar at
w - previously initiated variable to define the width or height displayed in the bar.

*/

var xx, yy, w;
xx=argument0;
yy=argument1;
w=argument2;

for (i=1; i<5; i+=1)
{
    draw_sprite_part(spr_bar,i,0,0,0+w,sprite_height,xx,yy);
};
draw_sprite(spr_bar,0,xx,yy);
Before continuing let me explain what this does. The "var w;" will create a variable w, and xx and yy are for x and y coordinates. Dont forget to put the sign ";" after typing out your variable names. In the next line, we set the correct arguments. That way it will be easier later to change the valus. The "draw_sprite_part", will draw a part of the sprite. Let me explain it more theral:
"spr_bar" is the name of the sprite we used for the healthbar. 1, 2, 3, etc is the image index. 0,0 are the beging where to start the sprite and 0+w is the value the healtbar starts. "sprite_height" will set the sprite doesnt cuts on the y axis, but on the x only. x and y are the origins.
Save the code.

STEP 3: Object
Create a object and name it "obj_bar"
Paste this code.
//Create
width=16
//Step
if keyboard_check(vk_left){if width>0{width-=1}} //Make the value go down
if keyboard_check(vk_right){if width<sprite_width{width+=1}}  //Make the value go up
//Draw
draw_healthbar(width)
In the create event we have created the variable width and set it to 16.
In the step event, if we press left, the value will go down (health), and right will make it go up.
In the draw event we called the script "draw_healthbar" with the next arguments:
argument0 = X position
argument1 = Y position
argument2 = width

Here is the screenshot of the final product.
Posted Image


I hope that helped you on the way of creating decent looking healthbars. If you want the health can go up/down, simply changing the code into "draw_sprite_part(spr_bar,i,0,0,sprite_width,0
+w,xx,yy)" and adjusting other variables. The healthbar can be any shape, size color, alpha...


Thank you for your patience for reading.


You can download the example here: Download

/Shadowrend

Edited by Shadowrend, 12 January 2011 - 03:23 PM.

  • 3

#2 CaptainLepidus

CaptainLepidus

    GMC Member

  • GMC Member
  • 850 posts
  • Version:GM:HTML5

Posted 21 March 2010 - 05:58 PM

Interesting. I may use this some time in the future.
  • 0

#3 Tweak2

Tweak2

    GMC Member

  • New Member
  • 122 posts

Posted 21 March 2010 - 06:10 PM

I'll have to put these cool graphics into my circle healthbars!

*cough*click link in my sig*cough*
  • 0

#4 Shadowrend

Shadowrend

    Master of Shadows

  • GMC Member
  • 1572 posts
  • Version:GM8

Posted 21 March 2010 - 07:13 PM

Your tutorial was also good. But I don't see anywhere that I cant post a tutorial that someone has already posted more than a year ago, and I haven't used your code or anything. First time to see it tho...
  • 0

#5 carritohmc

carritohmc

    GMC Member

  • New Member
  • 18 posts

Posted 19 April 2010 - 07:22 AM

Nice, I'm two weeks into my game and just now realized I hadn't designed a health bar, so I went with the stock Game maker one and it all just looked so out of place, your example let's me get real creative and add my own, and the code is simple enough. I give it 5/5 stars. I'll post an example of what my health bar looks like once I get it all nice and done. If it's all cool with you I would like to credit you if I ever get my game done.
  • 0

#6 Shadowrend

Shadowrend

    Master of Shadows

  • GMC Member
  • 1572 posts
  • Version:GM8

Posted 22 April 2010 - 08:43 AM

Thanks.

Sure, credit me if you want to.
  • 0

#7 SuperEvilNick

SuperEvilNick

    GMC Member

  • New Member
  • 65 posts

Posted 24 June 2010 - 09:27 PM

I think that this example is just awesome!
  • 0

#8 Shadowrend

Shadowrend

    Master of Shadows

  • GMC Member
  • 1572 posts
  • Version:GM8

Posted 24 June 2010 - 09:28 PM

I think that this example is just awesome!


Posted Image Thanks!
  • 0

#9 Fira

Fira

    God Bless You All

  • GMC Member
  • 1399 posts

Posted 25 June 2010 - 01:56 AM

I think draw_sprite_part can be used in Lite Edition actually.
  • 0

#10 Shadowrend

Shadowrend

    Master of Shadows

  • GMC Member
  • 1572 posts
  • Version:GM8

Posted 25 June 2010 - 09:32 AM

I think draw_sprite_part can be used in Lite Edition actually.


I don't know... When I had the lite edition I didn't know much coding xD
Bought the GM 7 after a couple of months. So if someone could check...
  • 0

#11 treeco123

treeco123

    GMC Member

  • New Member
  • 4 posts

Posted 06 July 2010 - 09:46 PM

you don't need game maker pro, you can use game maker light! ;)
  • 0

#12 Shadowrend

Shadowrend

    Master of Shadows

  • GMC Member
  • 1572 posts
  • Version:GM8

Posted 07 July 2010 - 12:02 PM

you don't need game maker pro, you can use game maker light! ;)


:) Thx
  • 0

#13 Fish9

Fish9

    GMC Member

  • GMC Member
  • 16 posts

Posted 15 July 2010 - 01:17 AM

I really don't see why you need more than 2 sprites for this.
  • 0

#14 iluvfuz

iluvfuz

    GMC Member

  • GMC Member
  • 1452 posts
  • Version:GM:Studio

Posted 15 July 2010 - 10:14 PM

Indeed, combine the accents with the health bar (and possibly the back of the health bar, if it happens to not be a border) and it should work fine. And more optimizistically.

Besides that, this is very useful. I've used draw_sprite_part many times for a fancier health bar, and it works great :)
  • 0

#15 Guminuxas

Guminuxas

    GMC Member

  • New Member
  • 17 posts

Posted 17 July 2010 - 08:54 AM

Thanks good example and healthbar :) :lol:
  • 0

#16 Shadowrend

Shadowrend

    Master of Shadows

  • GMC Member
  • 1572 posts
  • Version:GM8

Posted 18 August 2010 - 09:17 AM

NP Posted Image
  • 0

#17 whoisit

whoisit

    Always Learning

  • New Member
  • 242 posts
  • Version:GM8

Posted 11 January 2011 - 12:16 PM

Hi
I'm playing around with this example but I'm having trouble, I've changed width to global.width.
I created a goodguy and a badguy
In the goodguy collision event with badguy I have the following.
if global.width>0(global.width-=8)
But nothing happens, I figure I have to redraw the health bar but how and where does the code belong?
  • 0

#18 Shadowrend

Shadowrend

    Master of Shadows

  • GMC Member
  • 1572 posts
  • Version:GM8

Posted 11 January 2011 - 12:26 PM

By your code, I see that you are using "( )" instead of " { }".
Your code should look something like this:
if global.width>0{global.width-=8};
The use of "( )" in Game Maker is only used for mathematical equations and for function and script arguments. Further than that it has no use.
When you want something to occur if an action is done in coding you should use something like:
if something_happens
{
   do_something;
}

  • 0

#19 whoisit

whoisit

    Always Learning

  • New Member
  • 242 posts
  • Version:GM8

Posted 11 January 2011 - 10:19 PM

Hi Shadow,
Thanks for the detailed explanation.
Changed the code to the correct syntax but the bar stills stay full,
should I be calling the draw event after the collision?
  • 0

#20 Shadowrend

Shadowrend

    Master of Shadows

  • GMC Member
  • 1572 posts
  • Version:GM8

Posted 11 January 2011 - 11:18 PM

Can you send me your code (PM me), I can't fix your code unless I see it :)
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users