Health and Damage?

Discussion in 'Programming' started by Mori Senpai, Jan 20, 2018.

  1. Mori Senpai

    Mori Senpai Guest

    I've been looking into health bars and I am VERY new to the gamemaking community. I have a deadline in 4 weeks and I'm trying to figure out how to implement damage and healthbars. I aimed to have the enemies and main character have health bars. None of the characters shoot projectiles. I have two sets of enemies and two bosses.

    The first enemy is a porcupine, and I wanted to make it where upon contact, the player takes damage. But I also wanted that to have a sort of delay, so multiple strikes could hit before damage comes again. Then I wanted to have them have 20 health.

    Then there are snakes, which I wanted to deal damage after a set animation.

    The main character I wanted to also deal damage after a set animation (I set a sprite when you press space)

    I tried putting a healthbar by drawing a rectangle to fit inside my healthbar, but when damage was dealt to my character, the entire thing went all the way to the left (since the health bar fills right and empties to the left) and then continued to move past the farthest x and y value before stopping WAY past the bar. This is my first game and I've been left to do the code on my own.

    I've been using dnd, but also writing out code through execute code.
  2. TheouAegis

    TheouAegis Member

    Jul 3, 2016
    The first two you could have Googled yourself. The health park going that far down, we would need to see what could you use. It is probably an issue with the math.
  3. Neptune

    Neptune Member

    Jun 21, 2016
    Just yesterday I implemented a nice health bar system, but it doesnt use built-in functions or DND, so I don't know how to help :\

    Here is basic health bar math:
    current_bar_width = total_bar_width*(attribute_current/attribute_max);
    Mori Senpai likes this.
  4. LoadingDevelopment

    LoadingDevelopment Member

    Sep 10, 2017
    This is off topic and maybe not good to hear from my mouth.

    The first time I joined GM was years ago and because it has DnD functions. It's one of the reasons I joined a informatics collage and decided I want to become a programmer, a game designer, w/e...
    I see the DnD system as a gateway to learning programming. The more you learn the DnD system, the more you will ask yourself why aren't you doing this by code.

    My advice is maybe a little harsh, but it's the one anyone would give you.
    Learn to program yourself, even at a basic level. You will be amazed what you can achieve with a basic level and a little help which often comes from this community.
    Mori Senpai likes this.
  5. Woochi

    Woochi Guest

    draw_healthbar() is your friend, in which you keep "amount" 0-100, representing a percentage. How to prevent it from going -15 or 236 before putting it in that function, is up to you but remember that clamp(value,0,100) is your buddy too. Namely,
    - clamp( 22837 , 0 , 100 ) returns 100
    - clamp( -3 , 0 , 100 ) returns 0
    - clamp( 55 , 0 , 100 ) returns 55

    unless youre absolutely sure that current and max HP never fall below 0, and that current never gets bigger than max. in that case you dont need clamp.

    if the current_HP and max_HP of a unit is known, you can do the following in the draw event for the unit (piece of code):
    var _percent = (current_HP / max_HP) * 100 ;
    draw_healthbar(x1, y1, x2, y2, amount, backcol, mincol, maxcol, direction, showback, showborder)
    • x1 and y1 are coordinates of top-left corner
    • x2 and y2 are coordinates of bottom-right corner
    • amount is percentage of health the bar represents (0-100)
    • backcol is just that-- color of the "empty" part of the healthbar
    • mincol and maxcol can be either the same, or different to have a smooth transition from one bar color to another depending on health.
    • direction is direction to which healthbar is oriented: 0 is to the right, so right = full health
    • showback set to true uses backcol where the healthbar is empty, false does not use the backcol, showing whatever is drawn beneath the healthbar at the empty part.
    • showborder does just that- show a border or not around the healthbar . true or false.
    there is no need to code your own healthbar. you didnt code your own game engine either (i hope). the math and geometry will bother you in time, when you have no other option, so dont worry about practicing it now.
    var _percent = (current_HP / max_HP) * 100 ;
    draw_healthbar(self.x - 50, self.y - 80, self.x + 50, self.y - 50, _percent , c_black, c_red, c_lime, 0, true, true)
    and youre good to go. Should show healthbar somewhere above (the origin of) the character, 100 pixels in width (x-50 to x+50) and 30 pixels in height (char x - 80 to char x -50)
    Last edited by a moderator: Jan 21, 2018
    Mori Senpai likes this.
  6. Mori Senpai

    Mori Senpai Guest

    I was using this code:

    draw_self ();

    if instance_exists (obj_player) exit;

    draw_rectangle(x+37, y+62, x+111, y+76, false);

    and it wasn't appearing at all like it would have before.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice