Drag And Drop Help with health bar and lives

Discussion in 'Programming' started by WingBat, May 3, 2017.

  1. WingBat

    WingBat Guest

    Hello all!

    I am very new to coding and by new I mean this is my first go at it. It's a hobby I have always wanted to peruse so I do apologize for my lack of knowledge but hey, we all have to start somewhere right?

    I am using Game Maker Studio 2 Drag and Drop.

    I completed the "My First Game" tutorial that YoYo Games has provided on their YouTube Page but I am trying to expand my knowledge. I want to add a health bar and lives counter for three lives and I can't figure out how to make the enemy spawn deplete my HP. I have been looking at their online docs to try and figure it out on my own but lets face it my knowledge is not there yet. Is there any sort of video or guide that someone can point me in the direction to, to help me better learn how to do this?

    Thank you all for taking the time to read. I really appreciate any and all advice you could give me.
     
  2. Tthecreator

    Tthecreator Your Creator!

    Joined:
    Jun 20, 2016
    Posts:
    757
    Alright, after a quick google search I came up with the following results:




    The bottom line is that you could have googled this yourself. However, some of these tutorials work exclusively with code so I'm going to make it simpler:

    Here are the steps that would need to be taken to make this:
    1.upon the creation of an object, give it a certain amount of health.
    2.whenever there is a collision with another object decrease the health by 1.
    3.draw the health somewhere. This can be done using draw health.

    The same thing can be roughly done for lives.

    All of the stuff you will actually need is inside of the score tab on the right and the only events you will need are create, step and draw.
    A few more things:
    The create event is the first thing executed on a object and is executed only once. (It's code is literally executed on it's creation)
    The collision event checks if there is a collision with a certain object
    The draw event is the only event in which things can be drawn and draws every frame. (by default there are 30 images per second drawn to the screen. The draw event with you code of what to draw will thus also execute 30 times a second.)
    One thing to note in the draw event is that when you do use it, the sprite assigned to the object disappears. This is due to the inner workings. Just use the "Draw Self" option under draw and you should be golden.

    I hope this helped.
     
  3. JackTurbo

    JackTurbo Member

    Joined:
    Oct 19, 2016
    Posts:
    824
    hp should simply be a instance variable in the object it relates to, then when ever an action occurs that should decrease it you subtract from the hp variable the required amount of damage. If this is happening in a collision you can use the "other" keyword to do this.

    For drawing health bars I'm currently using a technique where I have a healthbar sprite. This is a simple rectangle with sub images for the different colours. Frame 0 is red, frame 1 is yellow and frame 2 is green.

    Then in the draw event for each enemy I use draw_sprite_ext to create the health bar like so.
    Code:
    draw_sprite_ext( sHealthBar, (hp/maxHp) * 2, x, y + yOffset, (hp/maxHp), 1, 0, c_white, 1);
    Here I am drawing the health bar sprite at the enemy's x coordinates and at the enemy's y coordinates + a predefined offset (which I declare in the enemy's create event). This is so the healthbar will appear neatly above their head.
    I use an instance variable for the offset so that each enemy can inherit this behaviour but have a different offset defined incase they're taller or shorter.

    You'll also notice I'm defining the subimage as (hp/maxHp) * 2. Dividing their hp by their max hp gives their current hp as a decimal value of 0 to 1 (0 being dead, 1 being full health). Then multiplying this by 2 makes a value between 0 and 2, which matches up with the sprites number of subimages. Because the subimage automatically rounds up and down, this means when over 66% health the bar will be green, 33%-66% it'll be yellow and 1-33% it'll be red.

    I then use a similar technique for xscale. I define it as (hp/maxHp) which means that the sprite will be its full width when the hp is full and will have a width of 0 when the hp is 0.
     
  4. WingBat

    WingBat Guest

    Thank you both so much for your help.
     
  5. thank you for this, i also found this helpful!
     
  6. Colin Perkins

    Colin Perkins Member

    Joined:
    Nov 26, 2017
    Posts:
    2
    i got my game almost complete, my player has health but i can't get the health amount to appear, i need to be able to see my current health
     
  7. Colin Perkins

    Colin Perkins Member

    Joined:
    Nov 26, 2017
    Posts:
    2
    although i would also like my game to have multiple levels to play through too
     

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