Game maker enemy shooting code


















Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Helping communities build their own LTE networks. Podcast Making Agile work for data science. Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Related 1. Hot Network Questions. Question feed. So, add a Create Event now, and in the code window that pops up type:.

It is worth noting that GameMaker Studio 2 has a built in global scope variable "score" which can be used to keep track of the player's points. Now, because the score variable is global in scope it means that you can only ever have one score value for the entire game, which can be limiting and it's generally better to have unique variables for score in each instance for example, if we wanted to make a two player game then we couldn't use score and would have to make instance variables for each one, the same as we have here.

We are now going to add a Draw Event to our object. Click the Add Event button now and select the Draw Event category:. As you can see, there are multiple draw events to choose from, but for this we simply need the general draw event, which is the one at the top of the category list highlighted in the image above. The general draw event is the one that GameMaker Studio 2 uses when it default draws your instance sprite. What does this mean? Well, so far we haven't added any Draw Event to any of our instances, and yet they all draw their sprites to the screen when we run the project.

This is because when you don't add a general Draw Event to an object yourself, GameMaker Studio 2 will automatically assume you want to draw the sprite assigned to the instance along with any transforms like colour or alpha or scale that you have added.

Essentially, the general Draw Event defaults to using this line of code:. That function will also draw the assigned sprite along with any applied transforms, just like the default drawing for an instance when it has no Draw Event.

Now, when you add a general Draw Event to an object and in it add some code, you are telling GameMaker Studio 2 that you want to handle what is being drawn and GameMaker Studio 2 will no longer draw anything except what you have put in the event, so it won't draw the assigned sprite unless you tell it to and you can draw any sprite, it doesn't have to be the assigned one.

The general Draw Event and all other draw events will be performed once for every game frame, much like the Step Event will, and note that you can have unrelated code or functions in the draw event as well as draw functions but it is recommended that you keep this to a minimum and use the Step Events instead where possible.

However, all drawing must be done in a draw event , and placing draw functions in any other event will not work. Just before we continue, it's worth noting that the Draw Events other than the general drawing, will not affect the instance default drawing, so that you can, for example, have no general Draw Event but have a Draw End Event and the instance will still default draw the sprite and whatever you have added into the Draw End Event.

If you want to know more about the different events in the Draw Event category, you can press to open the manual and read up on them in the section on the Object Editor. In our draw event we want to draw the score text on the screen at the same position at all times.

This means that we can't use an absolute room position since as the camera moves about, the text will be lost off of one side or the other of the camera view. So what we need to do is draw the score text relative to the view camera. To make this easier we are going to use a couple of local temporary variables. A local variable is simply defined as one that is "local" to the script or event that is using it. This means that it is created when you use the keyword var and then discarded again at the end of the event or script that created it.

This is useful for many, many things, not least of which is storing one-off values from calculations and saving memory resources.

In this case we want to use local variables to store function return values, so add the following to our Draw Event :.

In this case we use view port[0] since that is the one we set up in the room editor in the last section. As you type this code in, note that the bottom of the Code Editor will display "argument help" for the function, showing what value is expected for the function to work.

We could just as easily have written a string in there too. You could drop this instance into the game room, but all you'd see would be a "0" on the screen since we don't actually add to the score value yet, so let's do that now.

We currently have the following code in that event:. We need the enemy to add a value to the score object variable "thescore" and so for that we will use the "with" command. We covered "with" in the Enemies Section where we used the "other" keyword to refer to the other instance in the collision.

However "other" in that context only works for the collision event, and we are going to use "with" in the Step event, so we need to give it an object index or an instance ID to work with.

Change the code now so it reads:. Note that when using an object index like this, if there are more than one instance of the object in the room when the code is run it will run for all instances of the object.

Our code will now add 5 onto the score variable every time an enemy instance is destroyed, and we can now add the score object into our game room. Since it doesn't have a sprite it will be shown with a question mark icon. You can play the game now and you will see the score displayed at the top, and if you shoot and destroy the enemy it will go up by five. You might have to squint a bit though, as it's written rather small and isn't very easy to read, so lets fix that now. We need to style the text on the screen to make it more readable and nicer to look at and for that we'll start by adding a new Font Resource.

If you right click on the Font resource and select Create it will create a new font resource and open the Font Editor :. You can then go ahead and select a font from your machine to use as the game font and then in the editor set the style to bold to make it stand out that much more if the font you select permits it , and set the size to 24 to make it bigger.

Note that in the Font Editor you have a preview window that by default shows "Hello World". You can actually delete this and type in any text to get a preview of how it will look in the game.

The other options in the Font Editor are outside of the scope of this tutorial, but you can read up on them by pressing to open the manual and going to the section on the Font Editor. You can close the font editor now, as the next bit of styling we need to do is done through code. At the moment the text is being drawn black which helps make it harder to see for the player, so we are going to fix that.

As the function name implies, this will set the font for drawing any text. Note that this will set it for the whole game and all subsequent text will be drawn using this font even if you don't use this function anywhere else, so if you want to use various fonts in a game you must call this function before every item that needs drawing, but if your game only needs one font then you can call this function in a controller at the start of the game once only.

You can now run the game again and you should see that the score is much more visible, maintains it's position and goes up when an enemy is destroyed:. In this section we spent some time showing how to add text your game and increment the score, showing the player how they are doing as they they play. The main points you should have picked up while working through this section are as follows:.

That might not seem like much, but with this knowledge you can start to set up more complex displays for your player to include any number of written details, and format it to follow the view camera at the position you want.

It's worth taking a moment to look at all the different draw functions for text that are available to you press to open the manual as they permit you to scale the text, or limit it to a specific string width number of characters and many other things. In fact, you can save this project file and then save it again under a different name and experiment a bit with the different functions to see what they do you can load the original saved tutorial later to continue.

This section we are going to dedicate to adding some sound to our game. Basic sound is incredibly easy to do and so this section will be a short one, but what you'll learn can be used anywhere in your own projects. At the moment the game is looking good, but it has no sound effects, and sound is an essential part of any gaming experience so let's go ahead and rectify that In this tutorial we will simply be adding a single sound effect when you destroy the enemy instances, so you can use any sound effect that you have on your hard-drive or you can use the one that we have supplied along with the tutorial, which will be found when you open the file explorer in the TutorialResources folder, in the "Sound" sub-folder if you have any issues, you can also find the sound here.

Creating a sound resource is exactly the same as creating any other resource. Simply go to the Resource Tree and right click on the Sound resource and click Create. This will open up the Sound Editor :. If you have any issues, you can also find the sound here. Once you have named the resource and added the file, you can use the audio preview buttons in the editor to listen to the sound and set its volume:.

The rest of the options here we can leave at their default values, but if you want to find out more about them then hit to open the manual and go to the section on the Sound Editor. You can close the Sound Editor now as we are finished with the resource and are ready to add it to our game.

Playing a sound is really simple and requires a single line of code. This line plays the given sound with a given priority. For small projects you don't need to worry too much about priority, but for larger projects it is important. When too many sounds are playing at once for the hardware to handle, this value will be used to decide which sounds are stopped to let the others be heard.

So, sounds with a higher priority will have more chance of being kept playing while lower priority sounds will be stopped. Music for example would generally have a high priority so that it is never stopped, while small effects like shooting noises can have a lower priority.

Since we aren't concerned with this right now we can leave it at zero. Note that we set the last argument to false too. This is the "loop" argument and if you set it to to true then the sound wll loop continuously until the game ends or we call a function to stop it. Generally this is what you want for music or ambient effects like a wind or waterfall sample. When you play the game you should here the sound being played every time the enemy is destroyed, however the sound will quickly become tiring to the ear of those that play the game.

We need to "spice it up" a bit and one of the easiest way to do this is to simply change the pitch of the sound. When you use a sound effect or music in GameMaker Studio 2 it is played with a pitch value of 1. If you lower the value of the pitch to say 0.

This is a great method to add more life to your games and can be applied to almost any repetitive sound effect to make it more interesting and realistic. Note that if you set the pitch anywhere in code then the referenced sound will always play at that pitch unless set again, which is why we call this function not once at the start of the game, but every time the sound is going to played. In this short section we have added a simple sound effect to the game and made it a bit more interesting by changing its pitch every time it plays.

It's not much for such an important aspect of any game, but surprisingly those two functions are about all you'll need for adding sound effects into your project. If you want to, you can experiment some more now and add some extra sounds into the game for when the player shoots and moves or when he dies. Just remember that before you start experimenting, save this project file and then save it again under a different name and experiment a bit with the renamed file before loading the original to continue with the tutorial.

When you start a game, you aren't normally just dropped straight into the action, but instead you are usually presented with some kind of title screen. In this section , we are going to show you how to set up a basic title screen for your game using a new room and with some extra graphics. To start with we'll need a couple of sprites. This sprite will hold the title screen text graphic, so open the tutorial assets folder and load the sprite "titlescreen.

With this sprite we need to change the origin. We've covered this previously, but just to remind you, simply click on the Origin drop down menu and select Middle Center to position the origin in the exact center of the sprite.

At the moment, our game just starts in the main game room, but we want it to show a title screen and have the player do something like press Enter or click anywhere to actually start the game. To achieve this we are going to use another Room Resource , so right click on the Room resource folder and select Create Room to make one.

The new room will be added after the current one:. There is a minor problem that we have to fix now, though GameMaker Studio 2 will run rooms in consecutive order, meaning the room at the start of the room resource list is the one that will be shown initially when we start the game.

This means that we now have to re-order the rooms so that the title room is placed before the game room. Notice that when you reorder the rooms there are actually two different places where you can drop the room when you click and drag. The first is dropping it on another rom the room being dropped on is highlighted , which means that you want the selected room to become a "child" of the other room, and the other is dropping it over a room where a bar is highlighted above the room , which will place it in the room order.

By default the room will have been created with a Background layer, so select that now. This layer will be used to show the background tile sprite that we added previously.

This is done from the background Layer Properties window:. You'll see that the image now appears in the top left corner of our room as a background, but that's not exactly what we want We could have created a tilemap layer for this and added the background as single tile, but with the background layer it's a lot simpler: just tick the Vertical tile and Horizontal Tile check-boxes:.

Before continuing let's just look at a couple of the other options for backgrounds. You'll see that we have the option to Stretch the background image too. All this will do is stretch the background image used to fill the whole room area, which with a small image like the one we are using doesn't look right, which is why we tile it instead. You can also set the Horizontal Speed and Vertical Speed for the background layer.

This will scroll the layer by the number of pixels given every game frame, so setting a horizontal speed of, for example, -2 will scroll the background from right to left at a rate of 2 pixels every game frame. If you set this in the room editor you won't see anything happen, but in your game it will scroll. However sometimes you want to preview a change like this without having to compile the whole project to see how it looks, which is where the Animate feature of the room editor comes in very handy.

In the main editor window, you have a bunch of tools in the top right corner:. The one we are interested in here is the Play Animation button. Clicking this will animate the whole room such that any backgrounds that are set to move, or tile sets that are set to animate, or any sprite assets that have sub-images will be shown as they would appear in the game:.

With the horizontal background speed now set to -2, this will add interest to our title screen, we can continue on to make our title object. To move from one room - the title room - to the next one - the game room - we will need a new object, so go ahead and make one now Right click on the object resource folder and select Create.

Now add a Create Event :. We are going to have our object draw its sprite fading in gradually, just to create a slightly nicer looking title screen. All instances of objects have a number of built in variables that are related to how the assigned sprite will be drawn. In the Scoring Section we explained how GameMaker Studio 2 default draws the sprite assigned to an instance of an object, and most of the objects in this game have no Draw Event.

However letting GameMaker Studio 2 default draw the sprite does not mean you cannot change how it is drawn, as you can using the different built in variables.

You can change things like the scale, the rotation, the colour and the alpha, all through using the built in variables. In this case we are going to change the alpha to make the sprite "invisible" at the start of the room. For this we add the following code into the Create Event :. We also want to make sure that the sprite will be drawn in the exact centre of the screen, so we also add the next two lines:. In this way we can ensure that the image alpha will increment until it reaches 1 and go no higher this is important, as while you can set the image alpha value to more than 1 - or even negative numbers - this will have different effects on different platforms and the recommended value for this variable is always between 0 and 1.

We can now drop this object into the title room and test the game open the room editor, click on the object in the Resource Tree, then drag an instance of it into the room editor and release the mouse. You don't even have to worry about placing it properly because we've taken care of that in the Create Event of the object. The title text sprite says "Press Enter To Start", so we now need to have our instance detect that key and go to the next room to start the game.

For that we need to add a Key Up Event. The Key Up Event detects when a key has been released and won't trigger until that happens. We use this event just to give the user a moment to get ready to play, as the other two key events would change the room instantly. In this event we want to add a single line of code:. There are a number of functions available for moving between rooms, but we only need this one since it simply goes to the next room in the Resource Tree, which is our game room.

Care must be taken when using this function, though, as if there is no room after the current one ie: the room using this code is the last one in the Resource Tree we will get an error which will cause the game to close. Note that if we want to go to a specific room without following the order of the resource tree, then we would use:.

You can press to open the manual and see what all the different room functions are. If you run the game now, you'll get the title screen and pressing Enter will take you to the main game room to play.

After completing this section we now have a nicer introduction to the game. While setting this up you should have learned the following:. With that we are almost finished with this tutorial, but we have one final task before us. Currently the game has a limited number of enemies to destroy, which limits the gameplay greatly, so in the next section we'll look at "spawning" more enemies as you play Our project is shaping up quite nicely, but we have one final thing to fix before we can really call it a game Currently, if you destroy the enemies without getting hit, then the game doesn't do anything and the user has to close the game window.

Home Game Design Make your player shoot lazers or bullets. Leave a Comment Cancel Reply Your email address will not be published. This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Close Privacy Overview This website uses cookies to improve your experience while you navigate through the website.

Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website.

Install Steam. GameMaker: Studio. Global Achievements. So I have a procedural level setup where the game spawns in a certain amount of enemies and then the player. I have written some code to get the enemies to shoot at the player once he moves within a certain distance of the enemy and if they have line of sight. The problem I am having is that only one enemy in the level will shoot at the player and it uses whichever instance is closest to the player so it can be shooting at the player from halfway across the level.



0コメント

  • 1000 / 1000