Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
GameMaker Cookbook

You're reading from   GameMaker Cookbook Over 50 hands-on recipes to help you build exhilarating games using the robust GameMaker system

Arrow left icon
Product type Paperback
Published in Dec 2015
Publisher
ISBN-13 9781784399849
Length 212 pages
Edition 1st Edition
Languages
Arrow right icon
Toc

Table of Contents (12) Chapters Close

Preface 1. Game Plan – Creating Basic Gameplay FREE CHAPTER 2. It's Under Control – Exploring Various Control Schemes 3. Let's Move It – Advanced Movement and Layout 4. Let's Get Physical – Using GameMaker's Physics System 5. Now Hear This! – Music and Sound Effects 6. It's All GUI! - Creating Graphical User Interface and Menus 7. Saving the Day – Saving Game Data 8. Light 'em up! – Enhancing Your Game with Lighting Techniques 9. Particle Man, Particle Man – Adding Polish to Your Game with Visual Effects and Particles 10. Hello, World – Creating New Dimensions of Play Through Networking Index

Setting up player health and lives

Any action game involving a player controlled character is likely to have some sort of "health" and "life" system. These are terms long used in video games that are used in conjunction with how your character is abstractly "alive" within the game itself. In reality, health and life in games are simply numbers whereby you lose a chance to play or even the game itself when they reach zero. GameMaker makes keeping track of these numbers quite easy when it comes to the drag and drop actions, so we're going to look at how we can add these elements to our player character.

Getting ready

Since this recipe involves making additions to your existing player character, you don't need to do much. You'll need a sprite to indicate your player's lives (call it spr_life) but other than that all you need to do now is open the obj_player Object Properties window.

How to do it...

  1. In the Create event, from the Score tab, drag and drop the Set Health action into the Actions box and set Health to 100.
  2. From the same tab, drag and drop Set Lives and set it to 3.
  3. Add a Set Variable to the Actions box and set hit to 0.
  4. Click Add Event, then Collision, and select obj_enemy_patrol from the menu.
  5. Add Test Variable to the Actions box and have it check for whether hit equals 0.
  6. Below, drag and drop Start and End Block, and within that block add Set Health with a relative value of -20.
  7. Add Set Variable, setting hit to 1.
  8. Add Set Alarm 1 and set it to 60 steps. Repeat these actions using obj_hazard_spike in place of your enemy.
  9. Add an event for Alarm 1 and drag Set Variable to the Actions box, setting hit to 0.
  10. Add the event Draw GUI and, from the score tab, drag Draw Health to the Actions box.
  11. Make sure Relative is checked and, under the appropriate headings, enter the following values:
    x1: -16
    y1: -24
    x2: 16
    y2: -34
    back color: black
    bar color: green to red
  12. Drag and drop Draw Life Images to the Actions box and enter the following values:
    x: room_width-64
    y: 64
    image: spr_life
  13. Create a Step event and add a Test Variable that checks if health is equal to 0.
  14. Below that, drag a Start and End Block and within that block add Set Variable lives to -1 relative, and Set Variable health to 100.

How it works...

Health and lives are very straightforward, with GameMaker doing a lot of the work for you when using the drag and drop actions. Essentially, GameMaker has created the variable health and you've set it to a value of 100. You've also created the variable hit, which you use to control when you can be injured. If hit is set to 0 (off) then you can be hit by an enemy or hazard. If you are hit by one of them you lose 20 health points and the variable hit is set to 1. This is to prevent contact with an enemy continuously draining your health, as it is being checked every step. An alarm is set to 60 steps and at the end of that countdown hit is set back to 0, meaning you can be injured once more.

The Step event is also constantly checking for when your health reaches 0. When it does, the lives variable is decreased by 1 and your health is reset to 100.

There's more...

In this case we set the health bar to a specific coordinate relative to the player's position. This was purely a design choice to show you that you have options. You can set the health bar to a static position on the screen, much like we did for the lives indicator. As for the lives indicator, using the Draw Lives action instead of Draw Life Images allows you to indicate the player's remaining lives with text and numbers.

You have been reading a chapter from
GameMaker Cookbook
Published in: Dec 2015
Publisher:
ISBN-13: 9781784399849
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at AU $24.99/month. Cancel anytime