Gw Temp

Menu

Tutorial - 'Energy Bars' by yugi

An item about RPGMaker 2000 posted on

Blurb

Creating a simple Energy/Health bar in Gamemaker!

Body

Well I'm bored so why not do something. Yes I know there are hundreds of energy bar tutorials for GM and GM 5 has an energy bar function but it sucks.
So I'm doing this.
Well first make a script and call it "draw_bar".
This'll be what is used to draw the energy bar.
Well first we need to do the maths so do this.
{
num = round(argument1/argument2*argument5);
}
Don't ask why I've used those arguments (it'll be explained later).
Basically what that'll do is all the maths for drawing the bar. argument1 is the energy (lets say it's 78). argument2 is the maximum energy (lets say it's 150) and argument5 is the maximum length of the energy bar in pixels (lets say it's 200) so what that does is simple.
78/150*200 = 104
It'll basically creates a variable which is used later on as the length of the bar (which will decrease and increase with the energy). So the length will always be proportional to the maximum length of the bar.
Now we want to draw the bar. To do that add this.
draw_rectangle(argument3,argument4,argument3+argument5,argument4 + argument6);
draw_rectangle(argument3,argument4,argument3 + num,argument4 + argument6);
That will draw 2 bars in the same place (the back bar and the front one).
Ok. Now we need to pretty it up a little bit. Add this above the code you've just done.
pen_color = argument8;
pen_size = argument7;
brush_color = argument9;
That basically chooses the outline colour of the bar and in inner colour.
Now you might want the bars to be 2 diffrent colours so do this inbetween the code for drawing the 2 rectangles.
brush_color = argument10;
Now your code should look like this.
{
num = round(argument1/argument2*argument5);
pen_color = argument8;
pen_size = argument7;
brush_color = argument9;
draw_rectangle(argument3,argument4,argument3+argument5,argument4 + argument6);
brush_color = argument10
draw_rectangle(argument3,argument4,argument3 + num,argument4 + argument6);

}
Ok. If your code doesn't look like that then just copy and paste that.
You'll notice there are a few things missing. Firstly you might want the bar to be round not square so add this above the rest of the code.
type = argument0;
Then you'll need to replace the draw_rectangle with this.
if (type == 1) {draw_rectangle(argument3,argument4,argument3+argument5,argument4 + argument6);
brush_color = argument10;
draw_rectangle(argument3,argument4,argument3 + num,argument4 + argument6);}
if(type == 2){draw_roundrect(argument3,argument4,argument3+argument5,argument4 + argument6);
brush_color = argument10;
draw_roundrect(argument3,argument4,argument3 + num,argument4 + argument6);}
if(type == 3){draw_circle(argument3,argument4,argument5);
brush_color = argument10;
draw_circle(argument3,argument4,num);}
So what that does is simple. If type is 1 it will draw a rectangle. If type is 2 it will draw a rectangle with round corners and if type is 3 it will draw a circle? Yor probably think WTF? but you could use the circles for something like a Zelda game (replacing the hearts with circles). Then use multipul circles and energy then you'll have it working.
Ok now your code should look like this.
{
type = argument0;
num = round(argument1 / argument2 * argument5)
pen_color = argument8;
pen_size = argument7;
brush_color = argument9;
if (type == 1) {draw_rectangle(argument3,argument4,argument3+argument5,argument4 + argument6);
brush_color = argument10;
draw_rectangle(argument3,argument4,argument3 + num,argument4 + argument6);}
if(type == 2){draw_roundrect(argument3,argument4,argument3+argument5,argument4 + argument6);
brush_color = argument10;
draw_roundrect(argument3,argument4,argument3 + num,argument4 + argument6);}
if(type == 3){draw_circle(argument3,argument4,argument5);
brush_color = argument10;
draw_circle(argument3,argument4,num);}
}
If it looks like that then pat your self on the back. If not copy and paste it. Now that'll be fully functional. Now how to use it.
Well it's simple to use just place this in the draw event of an object
draw_bar(0,var,maxvar,0,0,0,0,0,colour,colour,colour);
It'll explain each part.
The first argument (0) is the type of bar it is. You can use 1,2 or 3.
1 = square box
2 = box with round corners
3 = circle
The next argument (var) is the energy. This has to be a variable for it to be changed.
The third argument is the maximum energy possible. This has to be a variable as well (diffrent to the energy one).
The fourth argument is the x position to the bar. You can use a number, variable or view_left[0]+0 if views are used.
The fith argument is the y position of the bar. Again you can use a number, variable or view_top[0]+0.
The sixth argument is the length of the bar. You can use a number or variable.
The seventh argument is the height of the bar. You can use a number or variable.
The eight argument is the size of the border for the bar. You can use a number of variable (0 is no border).
The ninth argument is the colour of the border. You can use c_red ETC. of make_color(0,0,0).
The tenth argument is the colour of the back bar. Again you can use c_red ETC. or make_color(0,0,0)
Finally the eleventh argument is the colour of the front bar. Again you can use c_red ETC. or make_color(0,0,0).
Few. This bar doesn't have to be used for energy it can be used as a timer, a power bar or anything along those lines. This bar will only work horizontally. If you want a vertical bar you this instead of the above code.
{
type = argument0;
num = round(argument1 / argument2 * argument5)
pen_color = argument8;
pen_size = argument7;
brush_color = argument9;
if (type == 1) {draw_rectangle(argument3,argument4,argument3+argument6,argument4 - argument5);
brush_color = argument10;
draw_rectangle(argument3,argument4 - num,argument3+argument6,argument4);}
if(type == 2){draw_roundrect(argument3,argument4,argument3+argument6,argument4 - argument5);
brush_color = argument10;
draw_roundrect(argument3,argument4 - num,argument3+argument6,argument4);}
if(type == 3){draw_circle(argument3,argument4,argument5);
brush_color = argument10;
draw_circle(argument3,argument4,num);}
}
It works exactly the same but the sixth argument is the height of the bar and the seventh is the width.
Well thats it. If there are any bugs or something you don't understand contact me.