官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 通化市| 工布江达县| 辽阳市| 远安县| 灵台县| 枣强县| 花莲市| 肥乡县| 临安市| 土默特左旗| 海兴县| 花莲市| 十堰市| 奎屯市| 汽车| 磴口县| 长宁区| 定兴县| 广德县| 新绛县| 平陆县| 峨眉山市| 黔江区| 阳信县| 时尚| 新田县| 砀山县| 元谋县| 凉城县| 资源县| 涿鹿县| 绥江县| 棋牌| 沂水县| 河间市| 大足县| 班戈县| 望奎县| 桐梓县| 高雄市| 七台河市|