Building the player

We are going to start by building our player object. We have briefly described the design already, but we have not broken the design down into something that we can start creating. First, we should bullet point each feature and what it entails to ensure we have all the Variables and Events we will need.

  • Arrow keys will move the player around the play area
    • Must remain in play area
  • Spacebar will fire weapon
    • A single bullet fired with each button press
  • Colliding with bullets or enemies causes damage
    • Should be different values based on type

Setting up the player sprite

Let's create the player sprite and prepare it for the game:

  1. Create a new project and call it Chapter_03.
  2. Create a new Sprite and name it spr_Player.
  3. Click on Load Sprite and load Chapter 3/Sprites/Player.gif, with Remove Background checked. This .art file has a spaceship with transparency and several frames of animation.

    Next, we want to adjust the collision area of the spaceship. The default collision is a rectangle covering the entire area of the Sprite that has pixel data. This means that the ship will take damage even though it has not visually come into contact with anything. What we want is to have a really small collision area.

  4. Click on Modify Mask. This will open the Mask Properties editor as shown in the following screenshot:
    Setting up the player sprite

    In the Mask Properties editor, we are able to control the size, shape, and placement of the collision mask, the area of a sprite where collision detection will occur. Some games require pixel perfect collision, where collision is determined on an individual pixel basis. This is the most precise collision possible, but it is also computationally expensive. The majority of games, however, can get away with a much simpler shape, such as a rectangle. This method is much more efficient, but limits the visual accuracy of the collision. The choice of which one to choose is dependent on the game's design and how much control is necessary to achieve the desired results.

  5. We want full control of the collision area, so set the Bounding Box to Manual and leave Shape as Rectangle.
  6. There are two ways to adjust the parameters of the Bounding Box. We can either enter exact positions for the corners of the box, or we can draw the box directly on the image of the Sprite. Left mouse drag a small box roughly in the center of the spaceship as shown in the previous screenshot.
  7. Click on OK.

    We are now back in the Sprite Properties editor and we can see that Collision Checking now states that it has been Modified. The last thing we will do to this Sprite is to move the origin to the tip of the spaceship's gun. By doing this we won't have to worry about offsetting the bullets upon creation through code.

    Setting up the player sprite
  8. Set Origin to X: 28, Y: 24, and then click on OK.

Controlling the player object

Let's create the player object and get it moving around the world.

  1. Create a new Object and name it obj_Player.
  2. Assign spr_Player as its Sprite.
  3. We need to initialize a variable for the speed we want the player to move at. This will make it easier to change the value later and have all scripts in obj_Player refer to it. Create a new Script and name it scr_Player_Create.
    mySpeed = 8; 
  4. In obj_Player, add a Create event.
  5. Drag an Execute Script icon from Control into the Actions: area, and apply the scr_Player_Create to the script option. Click on OK.
  6. Create a new Script and name it scr_Player_Key_Left. This script will have the code for the left arrow key.
  7. While we want the player to be able to move left, we also want to prevent the player from going offscreen. Write the following code into the script:
    if ( x >= sprite_width )
    {
        x -= mySpeed;
    }

    We start with a conditional if statement that queries whether the player's current x position is greater than or equal to the width of the sprite. In this case, it would mean that the origin of the player is greater than the image's width of 48 pixels. If it is greater, we place the object eight pixels to the left of the current position.

    The method of movement we are using here is not movement in the traditional sense. There is no velocity being applied to the object, but rather we are teleporting the object from one position to the next. The benefit of using this method is that if the key isn't being pressed, the Object won't move. This is necessary in this game, because we cannot use a No Key event due to having to shoot weapons.

  8. In obj_Player, add a Left event under Keyboard.
  9. Drag an Execute Script icon from Control into the Actions: area, and apply the scr_Player_Key_Left to the Script option. Click on OK.

    Before we move on to all the other keys and their scripts, it is always good to check to see if the Object works as intended.

  10. Create a new Room.
  11. In the settings tab, change the name to TheGame and Width to 800. Making the Room wider will give the player more area to maneuver around and recognize enemies easier.
  12. In the objects tab, select obj_Player and place a single instance near the center of the room as seen in the following screenshot:
    Controlling the player object
  13. Run the game.

    If everything is set up properly, the player should move to the left only when the left arrow is pressed down and should remain in the play area. We can now move onto the other controls.

  14. Create a new Script and name it scr_Player_Key_Right. This will be for the right arrow key.
  15. The script will be similar to the left, except we need to also take into consideration the width of the room. Write the following code:
    if (x <= room_width - sprite_width)
    {
        x += mySpeed;
    }

    Here we are testing whether the player's current x position is less than the width of the room minus the width of the sprite. If it is less than that, we add mySpeed to the current location. This will ensure the player stays on-screen when moving to the right.

  16. In obj_Player, add a Right event under Keyboard.
  17. Drag an Execute Script icon from Control into the Actions: area, and apply scr_Player_Key_Right. Click on OK.

    We now have our horizontal controls and need to add the vertical movement. We will go over the code for the up key and down key scripts, but by now you should be able to implement them into the object.

  18. For the up arrow key, create a new Script and name it scr_Player_Key_Up, and write the following code:
    if (y >= sprite_height)
    {
        y -= mySpeed;
    }

    It is similar to the horizontal code except now we are looking at the y position and the height of the Sprite.

  19. For the down arrow key, create a new Script and name it scr_Player_Key_Down, and write the following code:
    if (y <= room_height - sprite_height)
    {
        y += mySpeed;
    }

    Again, here we are looking at the height of the room minus the height of the sprite as being the furthest point we can move downwards. The movement controls are now complete and the Object properties should look like the following screenshot:

    Controlling the player object
  20. Run the game.

The player should be able to move around the entire screen, but never off of it. The only remaining control we have left is the button for firing the gun. However, before we can implement that we need a bullet!

Building the bullet

Bullets are easy to make, as they generally just move along in a straight line once they have been fired.

  1. Create a new Sprite and name it spr_Bullet_Player.
  2. Click on Load Sprite and load Chapter 3/Sprites /Bullet_Player.gif.
  3. As we currently have the origin of the player object set to the tip of the gun, we will want Origin of the bullet to be at the front. This will help make the bullet appear to come out of the gun without having to code it directly. Set the values to X: 17, Y: 4.
  4. We can leave everything else as it is, so click on OK.
  5. Bullets should also make a sound when they are fired so let's bring in a sound. The first thing we need to do is to switch back to the Legacy Sound engine, so that we ensure the audio is heard in all browsers. Navigate to Resources | Change Global Game Settings and under the General tab, uncheck the box for Use New Audio Engine.
  6. Create a new Sound and name it snd_Bullet_Player.
  7. Click on Load Sound and load Chapter 3/Sounds/Bullet_Player.wav.
  8. Make sure Kind is set to Normal Sound. Then click on OK.
  9. Now it is time to make the bullet move on its own. Create a new Script and name it scr_Bullet_Player_Create.
  10. We want the bullet to move horizontally to the right. This is easy to do with the following code:
    hspeed = 16;
    sound_play(snd_Bullet_01); 

    Hspeed is a property representing the horizontal velocity of an object in GameMaker: Studio. We need to apply this code the moment the bullet is instantiated in the world. We also play the sound of the bullet a single time.

  11. Create a new Object and name it obj_Bullet_Player, and set the Sprite to spr_Bullet_Player.
  12. Add a Create event. The Create event is only ever executed once, upon creation.
  13. Apply the scr_Bullet_Player_Create and click on OK.
    Building the bullet

As shown in the preceding screenshot, the bullet is now complete and ready to be fired. Let's go back to the spaceship!

Firing the bullet

A bullet is only dangerous to enemies if it has been fired. The player ship will handle this code.

  1. Create a new Script and name it scr_Player_KeyPress_Space.
  2. Write the following code:
    instance_create(x, y, obj_Bullet_Player);

    With this code, we are simply creating an instance of a bullet where the current position of the player ship is, or more specifically, where the origin of the player ship Sprite is. This will make the bullet appear to be shot from the ship's gun.

  3. In obj_Player, add a Space event from Key Press and apply scr_Player_KeyPress_Space. The Key Press event checks if the indicated key has been pushed down. This will run once and requires the key to be released before being able to run again.
  4. Run the game.

If everything is working properly, we should be able to move around the screen and fire bullets as fast as we can hit the spacebar, as shown in the following screenshot. We are almost ready to start adding in gameplay, but before we do, we have a little bit of cleanup to do.

Firing the bullet

Note

If everything appears to be correct, but you are still unable to see the intended result, try refreshing your browser. Occasionally, browsers will keep the game in memory and won't load the updated version immediately.

Removing bullets from the world

Every time we create an instance of an object, it needs to be placed into the memory and the computer will need to keep a track of it. We have all these bullets that are going offscreen never to be seen again, but the computer sees them. This means that over time the computer could be trying to watch millions of wasted bullets, which in turn means that the game will start to slow down. As we don't want that to happen, we need to get rid of all these offscreen bullets.

  1. Create a new Script and name it scr_OffScreenRemoval. This Script can be applied to any object in the game that goes offscreen and that we want to get rid of.
  2. To remove an instance from the world, write the following code:
    instance_destroy();
  3. In obj_Bullet_Player, add an Outside Room event from Other and apply the script. The Outside Room event is a special event that checks if the entire sprite of an instanced object is completely outside the room.

There we go! We now have a spaceship that moves around the screen, shoots bullets, and we keep the memory usage low. Let's make some enemies!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.27.171