Space shooter tutorial game maker


















Essentially, the higher the depth, the "nearer" the camera it is and the lower the depth the further away, so a layer at depth will draw under a layer with depth , for example. As mentioned previously, you can use the layer name in "" as a string to tell GameMaker Studio 2 what layer to use, and now if you test the game again, the bullets will be created below the player object.

At the moment, our bullets are created 1 per game frame every time the game loops round and performs the Step Event again , which is a bit too fast for what we require.

So now we need to slow the rate of fire down a bit. All we are doing here is preparing a variable called "cooldown" for use later on in the game - remember, the Create Event is only run once when the instance is first created, so this variable is being initialised to 0 once only.

We've seen built-in variables already, but this is one you are creating and it's called an instance variable. Instance variables are valid for any event in an instance and can be changed or read in other code blocks.

We are now going to use this variable in the Step Event of the player object to control how often the shooting occurs, like this:. We have also set the "cooldown" variable to 3 after we create the bullet instance, which means that the next game frame, the condition will fail and no bullet will be created because "cooldown" is not less than 1.

Finally we need to add the next line, after the current code, and outside of the "if" check:. This simply subtracts 1 from the "cooldown" variable every game frame, meaning that now we can shoot once every 3 frames instead of every single frame.

Your full codeblock should now look like this:. If you test the game again now, you can see that the bullets fire a bit slower, but for our purposes it still seems too fast. This is now an easy thing to fix, as it requires a simple change to the number of frames we wait between creating each new bullet instance.

So, change the "3" in the Step Event "if" code block to "10" and test again. That brings us to the end of Shooting section of the "My First Game" tutorial.

Let's just pass over the core concepts and things that you should know after completing this tutorial:. Our project is still not quite a game, as it's missing a few important things One of which is an enemy to shoot at! So, the next section in this series will see us creating a new object for the enemy and having it react to the player and the player bullets You've made it to the Enemies section of our "My First Game" series of tutorials, and you're doing well!

You should now have a player ship that can move and shoot, and bullets that are timed to come out at regular intervals. So, what next? Well, most games have some form of goal to achieve and more often than not this involves shooting something, so in this tutorial our goal is to add in some enemies for the player to shoot.

Before getting to the part where we add the enemies, however, we are going to take a moment to speed the game up and make it feel more responsive. We could do this by changing the amount of pixels that player moves when the arrow keys are pressed, along with changing the bullet speed and the timer instance variable we use As mentioned briefly in a previous tutorial, all games run at a speed which is defined by the number of times the game loop runs in a second.

By default this is set to 30 by GameMaker Studio 2 for any new project, which is fine for puzzle games, or games that don't require fast response times or even for mobile games, but for arcade style games like ours a preferred value would be As you can see from the image above, here you can change a few things that will affect how your project will run, including the game FPS value. So, set this to 60 now, then click on Apply and close the options then test the game again.

You'll see that everything is much faster, and smoother when playing. The workflow for creating the enemy object is the exact same as that which we used for the player and bullet objects, so we'll simply list the steps here, as you should be familiar with how it goes:.

We now add a Create Event for our enemy object. In this event, we will add the following:. Everytime we shoot the enemy, we will deduct 1 from this value until it reaches 0 and is removed from the game. Keep in mind that instance variables need to be initialised before use, and so that is why we have them in the Create Event , as that event is run for every instance the moment it is created in a room and the event only runs once so the variable is only set once at the start.

This variable will be used to define the movement speed of the instance. Note that you should not use the variable "speed", as that is one of the built-in variables that all objects have and we don't want to use that for this.

Why are we using the instance variable and not the built in one? Well, we don't have to, and we could just use the value 1. This can be very time consuming and error prone, so we use the instance variable instead to store the value.

This means that should we want to change it, we only need to change it in this one event, and all the rest of the code will "just work".

We now need to add a Step Event to the enemy object. This event runs every game frame and in it we are going to check for a player instance in the room, and if one is found we'll move towards it. The code looks like this:. We check to see if there is an instance of the player object first because later we access certain player variables. This is a bit of forward thinking on our part, as we will eventually have the player "die", removing its instance from the room, and if we try to access the variables of an instance that doesn't exist, then the game will error and crash.

Notice how we tell the enemy where to move to The method shown is the "point" method for accessing variables in another instance. You've seen this line before in the other two objects, so you should know what we're doing here You should open the Room Editor now on our game room and add a few instances of this enemy object into it click and drag from the resource tree into the room area , and then test the game:.

At the moment, the enemies just follow the player around and don't actually do or react to anything else. We need to fix that, so to start we need to edit the Step Event again. At the moment it looks like this:.

We are going to expand on this to include a check to see if the "hp" variable we initialised previously is less than or equal to 0, and if it is we are going to destroy the instance remove it from the game room. This is done using the following code, which you should add in after the current code:. The next thing to do is to make the "hp" variable actually go down, as currently that check will always return false since we only set the "hp" to 5 and nothing else.

Now, in this event we will need to affect the "hp" variable of the colliding instance, and we saw that we could do this using the "point" method previously. However that won't work in this case, as there are multiple enemy instances within the room and GameMaker Studio 2 doesn't know which one you actually want to affect. So we can't write:. Instead we need to write the code like this:. The important part here is the very first line where we introduce a new function - with - and a new keyword - other.

We use "with" to change the scope of the following code block, making it run from the instance or object that we indicate.

However we only want the code to run in the other instance involved in the collision so we use the keyword "other". As you might have guessed, other is a special reserved keyword that GameMaker Studio 2 uses in the collision event to reference the unique instance ID value of the other instance in the collision, in this case one of the enemy instances. After we take the point of off the "hp" variable, we then call a function to destroy the calling instance the bullet.

Test game now and see what happens While you've been testing your game, you may have noticed that the bullets don't often actually appear to hit the enemies when they disappear What's happening? To answer that we need to back to the Sprite Editor and explain another of its features - the ability to set up a collision mask.

So, open up the bullet sprite now and click the section labelled Collision Mask :. This section of the sprite editor permits you to define the area of the sprite that will be used to detect collisions, where a collision is defined as when two collision masks overlap at any point. By default, you are set to use a rectangular collision mask, and this collision mask will cover the whole area of the sprite.

However if you look at the bullet sprite, you can see we have a lot of "blank space" which is being used for collision detection. This is something we need to change You can edit the collision mask by simply dragging the little box "handles" around in the sprite preview window:. And you can also set these values directly using the input boxes in the collision mask section. What we want is to have the collision mask cover only the "head" of the bullet so that the rest won't register, something like this:.

You now need to open the other two sprites for the player and for the enemy and edit their collision masks too, as in the image shown below:. Note that we have left the player collision mask a fair bit smaller than the sprite itself. In most arcade games, the collision mask is kept smaller than the sprite to give the player a bit more room for error, and in this game we will do the same. Before we end this tutorial, let's quickly add in a collision for the player colliding with the enemy.

This won't be the final way we do this, but for testing and to give a feel for how the game will pan out, it is fine. All this code does is as you might imagine restart the game the moment an enemy instance "touches" the player instance, meaning that we now have to not just shoot the enemies, but dodge them too. This is a very basic mechanic and we'll refine it more in later tutorials, but for now it's enough for us to test and get a feel for how everything plays.

You should run the game again now, and you'll see that the experience is quite different to what it was before. The collisions are solid and look better, and there is a certain skill required by the player to avoid the enemies while shooting. In this short section we have added yet another object to our game, the enemy, and got things feeling a bit more like how an arcade game should feel.

The main points you should have learned from this are:. That might not seem like much, but the core concepts here are probably amongst the most important you can learn. Being able to access variables in other instances is incredibly useful and very powerful, and it's important to know how and when it can be done.

The same for the collisions mask, as having the wrong collision mask in your game will seriously affect the gameplay and the fun of the player. In the next section , we are going to look at making the visual aspect of the game nicer, in particular adding backgrounds to the arena, as well as making the arena bigger and having a "camera" follow the player around This is the Tiles and Views section of our "My First Game" tutorial, and if you've done the first three sections then you've already got the beginnings of a small arena shooter game.

Your player can move and shoot, and you have enemies that attack the player. However, it's not very pretty, since everything happens on a plain black background. It's also not a very big play area for the player to move around in, making the game feel cramped. In this section we will change that by making it more attractive to the player using tiles and also expand the play area using cameras.

This section is a bit different to previous ones in that we won't be using any GML code. Everything we want to do can be achieved using only the Room Editor , so to get started double click the room "room0" in the Resource Tree to open the Room Editor workspace.

To add a background to our game we want to use a Tile Set resource. Tile Sets are all based on sprites , and although so far we have only used sprites for game entities like the player or the bullets, we can also use sprites for backgrounds and tile sets, which can also be animated and do other interesting things.

First thing to do is create a new Sprite resource right click on the Sprite resource folder and select Create. Then import a tile set image. We are going to use the following image:. You can get the above tile set from the TutorialResources folder that the tutorial made automatically, in the "Images" sub-folder, when you click the Import button if you have any issues, you can also find the images here.

The tiles we are going to use for the game are x pixels each, all placed on the same image in a grid. Tile Sets are always comprised of a single sprite image, and must always be based on a grid, although the grid does not have to be square, just regular, ie: you can have 24x96 tiles, or 32x32, etc Note that the top left corner of the tile set sprite is empty.

GameMaker Studio 2 will always use the first tile of a tile set as a "blank" tile, which is what is placed by default as an empty tile. When you add a tilemap layer in the room editor, which we'll cover in a moment, it will be empty and you won't see anything, but it's actualy filled with these "blank" tiles, and when you start to paint tiles onto the tilemap layer, if you delete a tile, it's not actually being removed, but instead it's being changed for this "empty" tile too.

Note that even if the image you supply has non-transparent pixels in this first grid cell, they will not be drawn , so when making tile sets keep this in mind and just leave this first tile blank. We now need to make a new Tile Set resource, which is done the same way as for sprites and objects, etc This will open the Tile Set Editor :.

The tile set window is comprised of two parts to start with: the main Tile Set Editor window which has the Tile Set Properties window chained to it. You can assign the sprite by either clicking the Select Sprite button in the Properties window, or the button marked No Sprite in the Editor window. When you import the image, you'll see that it is covered in a grid that is 16x16px per cell.

This grid shows the way that the image will be split to create the final tile set cells, and if you have used the tutorial image you can probably tell that the current settings are way to small for the image we are using. In the above image you can see we have marked the first two sections of the tile set properties.

These control the tile set cell width and height and setting these will dictate how the tile set image will be split up and how it will be displayed when painting it into a room tilemap layer.

For this tile set you need to set each of these values to px. The rest of the values can be left at their default settings, as they are used for creating "offsets" and "split distances" between individual tiles in a tile set image. Basically, some tile set images may be created with "empty" areas around each tile and so you can set the pixels or cells between each individual part of the image here.

However our tile set has all the tiles packed right beside each other, so we don't need to worry about this. We need to go back to the Room Editor workspace now, as we want to create a new layer called the Tilemap Layer.

We currently have two instance layers one for the player and the enemies, one for the bullets and a background layer. We want to add a tilemap layer between the background layer and the bullet layer so click the Add Tilemap button to add the layer first, then click on it and drag it to position it between those two. As with almost everything in GameMaker Studio 2 , you can give this new layer a name like " TilesLayer " or something, and then you can go ahead and assign the tile set that we made previously to it.

This is done by going to the Layer Properties window - which is opened by default on the left of the room editor when you select any layer - and then clicking the button that says " No Tile Set " which will open a window to let you select the tile set to use:.

The tile set selected will now open on the right of the room editor workspace and you can click on any of the tiles to select it for "painting" into the room:. We now need to paint the tiles in a way that makes sense for the room and the tile set chosen, so select the appropriate tiles to make the final tilemap layer look like this:. You can test the game now and it should play exactly the same as it did before, only now we have a nicer background for the action to happen on. In a later tutorial in this series we'll look at creating a "wall" around the arena so the player can't leave the room area, but for now we have what we wanted.

The play area for the game is a bit small so the next thing we are going to do is make the roomsize a bit bigger. This is easily done by simply changing the width and height of the room from the Room Properties , which by default can be found at the bottom left of the room editor window:. You can see that the default width and height here are x, but that's too small so let's just double the width value to by clicking on the input box and changing it.

If you run the game at this point, you will find that you now get a massive game window that is way to big for most people to actually play in, so we need to sort that out using cameras. However before we get to that, you should fix the tilemap layer so that it covers the whole room:. To prevent the issue with the huge window we need to tell GameMaker Studio 2 to only show a portion of the game room using a camera view.

We can set this up from the room editor too, from the section titled Views in the Room Properties. Clicking on that section will expand the different properties:. The first thing to do here is to check the box beside Enable Views. No matter how you set things up, if you don't enable views then nothing will change, so this is very important! Next you need to expand the section on View 0. You can have multiple camera views in a room, and they can all be enabled and displayed at different positions permitting, for example, a two player split screen game, with a camera view for each player , however for our game we only need one, and that's the camera View 0.

External links may contain affiliate links, meaning we get a commission if you decide to make a purchase. Read our disclosure. Check Out These Related Coupons. About Coupon Scorpion. This category only includes cookies that ensures basic functionalities and security features of the website.

These cookies do not store any personal information. Non-necessary Non-necessary. Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website. Facebook messenger. Hacker News. Set the number to "1" and press OK.

Next, add the "Restart Room" action. Save your game and play it! If you want to create a second level now, you can. Remember though, that it's often easier to duplicate an existing room and adjust it, rather than starting from scratch.

Step 9 - Losing the game It's not fun if you can't actually lose, right? First, let's give the player 5 lives. Open the playerShip object. Add a new event for "Game Start" it's under the button labeled "other". Find and add the "Set Lives" action, in the "Score" tab. Enter a value of 3 or whatever you want and press ok. Now that the player has lives, we can start taking them away! Add a "Collsion" event for colliding with the playerShip.

Add an action to destroy the enemyShip object self. Add an action to destroy the playerShip object other. Now open the playerShip object. Add the "Destroy" event to the playerShip object. By putting actions in here, we can have them processed regardless of how the ship was actually destroyed! For now, we just want to remove 1 life from the player when the ship is destroyed.

Add the "Set Lives" action from the "Score" tab. Set the value to -1 relative. Can you figure out how to subtract one life when an enemy ship goes off screen? Step 10 - Giving the player feedback It's important to let the player know when they're doing well, and when they're doing poorly.

I suggest drawing the score at the top-left corner 0, 0 , but this is up to you. Add the "Draw the lives as image" action. I suggest drawing the lives at the bottom-left corner 0, , but this is up to you. You'll need to determine when to award the player points and how many points to give them! Step 11 - Making the enemies move To make the game more exciting, we'll make the enemies move towards the player. Add an event for "Create".



0コメント

  • 1000 / 1000