Lesson 13: Adding A Score
In this tutorial, we add a scoring system to our snake game, utilizing GameMaker’s built-in score variable and learning how to manually draw elements on the screen to display the score.
Lesson 13 Transcript
What I want to do now is add a score to our game, a visualization of how well the player is doing.
There is a built-in variable that we’re going to use called score, and then we’re going to look at drawing stuff to the screen manually because underneath the hood, out of all of the things that we’re doing already, anything that shows up on our level is being drawn.
What we want to do is take a little bit of that power and draw things exactly where we want and how we want to do it. All right, so let’s jump into that.
Now, inside of the food, once again, when it gets destroyed, this is the best place to upgrade our score. So I’m going to go ahead and say plus plus score. So score is a built-in variable and it just keeps track of a number.
You can use that number for really anything you want. Just because it says score doesn’t mean you have to, but it is the score that we’re going to use.
So it starts at zero when you begin the game, and we’re just going to increase it every single time we gain food. Now, if we ran this, we wouldn’t see anything because the score, it doesn’t have any visual component yet, so we need to add something.
So let’s open up our snake and I’m going to add a new event, and this is going to be a draw event. So inside here we’re going to draw score.
And the first thing to know about draw events is you must, if your object has a Sprite, you must say draw self, because otherwise your Sprite will not show up at all.
Your object is there, but the draw event is inherently being called in every object that is in the game, and the first thing that it does is it draws itself to the level.
Now, if we override the draw event like we just did, then we must say, Hey, don’t forget to draw yourself on the level.
So if I say take that out and I run it, our object is still there. There goes the food, and now we are somewhere, but we have no idea where.
So we’re not trying to make the game that hard, so add and draw self, and then when we run it again, we will appear.
I have made that mistake plenty of times. So be sure that you draw self if you ever put in a draw vent, assuming you have a Sprite, if you don’t and you try to draw self, I’ll show you what happens. You’ll get an error because you cannot draw something that doesn’t exist. That’s a philosophical statement.
I’m not going to worry too much about the implications of it, but if you do not have a Sprite, then you cannot draw it. So s p r Snake, there we go. Drawing self. Now there is a whole host of drawing functions.
We are going to stick to the simplest one, which is going to be a draw underscore text. Now we pass in where we want to draw it, and the string we want to draw. So I’m
Going to just draw it in the top left corner over here. I’m just going to say right around here. And so I’m going to put in 32. 32. That’s going to be our X and our Y and the string that we want to draw. We want to draw the score.
Now, draw text is a special function in that it will take whatever is passed in here and convert it to a string. We haven’t had any in-depth conversation on data types because I don’t want to get bogged down in that, but it is really important to understand that there are different types of data.
This is an integer. It’s just a whole number. That’s really all that means. And score is also an integer and it’s expecting a string. And so normally when you try and do something with a data type that’s not allowed, you get an error, which just makes sense because if you’re looking for the letter in a number, well, it can’t do that.
It tries, it fails, it blows up. So data types are very important, but there are times that the functions will let you get away with doing other things. So putting our score in here, it’s going to just show the number, even though it says string.
So I’m going to press F5 and it should show up right up here. There’s our score. So every time we collect food, that will increase by one.
Of course, just having a number there, although fairly self-explanatory, we should probably put in what that number actually means. So I’m going to say player score, colon plus score.
Now we are going to run into an error doing this, so I’m going to press F5 and show you the error that we get. This says do add execution error. This is an extremely frustrating error if you have not seen it before and you’re not familiar with what that means.
An execution error essentially means you’ve done something fundamentally wrong like dividing by zero or trying to say Player score plus zero. GameMaker cannot do that. It doesn’t understand what you want.
So what we have to do is put these two data types in a way that they can be added together. So we can’t convert this string to a number that doesn’t make any sense, but we can convert a number to a string and there’s a function for that called string, and you just pass in whatever value you want.
Make sure you put it in those parenthetical marks, and then it will understand that what you actually want to do is not add two strings together to get a value of 500 of adding up all the asky values or something strange like that.
No, it will understand that you just want to put
This on the end of that. So when we run it again, we have an explanation for what that number is and voila, a basic introduction to data types and what you can do with them and what you cannot do with them.
So that actually concludes our first game. You have made a clone, not a pure clone, but a heart clone of snake. It’s a really cool game. It’s something fun, and now I encourage you to go and kind of make it your own.
So the art we did was two blocks and an ellipse. Nothing too special there, but if you’re artistic and you want to try your hand at that, go and make some sprites, maybe increase the size of them. Play around with the room, the size of the room, and move around it.
There are a lot of things you can do even with just such a simple game. There are still a million variations that can be done in this game and probably will continue to be done in this game because it’s just a classic game.
But that concludes all the code we need to put into it. What I want to tackle next is how to export and build your game to be able to share it with a friend. That’s just a file they can click on and run it on their computer, and then they can play the game wherever they are.
I’m going to show you how to do that next.
Lesson 14: Sharing Your Game
In this tutorial, we conclude the module with a walk-through on how to export your game using GameMaker, highlighting that this feature is only available to paid users.
We demonstrate the process step-by-step, showcasing how to create an executable file that can be shared with others, and emphasizing the importance of packaging it as a zip file for easy sharing via email.
Lesson 14 Transcript
So if you’re using the free 30-day trial version, you’re not going to be able to do this because it limits the functionality of what you can do. Pretty much only in this feature, you’re not going to be allowed to export.
But if you purchase GameMaker in the lowest form, I think it’s $39 for a year’s license, then you can build and export your game perfectly fine. The way you do that is by first you choose where you want it to go.
If you remember, there are lots of different versions of GameMaker, and so it can export to Windows, it can export to HTML, to the Switch, to the Xbox, to anywhere that you can think of. You can make a game that plays on there.
And up here in the top right corner, Targets is where you choose where that goes.
So right now I’m able to export to Windows, Mac, OS, and Ubuntu. I’m just going to choose Windows because that’s what I’m on and that is where it’s going to go.
So now up here near the top left, if I click on Build, I can go to Create Executable. Then you’ve got two options, package as installer or package as a zip.
Now the installer, both of them are going to look exactly the same, but an installer is like a program that you click on and then it actually installs itself into your computer.
So it’s going to go through the whole process of choosing where to put it, installing it, and doing all of that in a finished game. That’s what you would want for a game like this, that you just want them to be able to run.
You want to do a package as a zip, and you come up and you save it.
I’m going to save it on my desktop, and then it goes through the whole process of compiling it, and then it brings it up for you over here, and now it comes up as a zip file.
So you will have to extract it, and then it opens up again, and here’s our game. And if I click on it, our game is now running. We don’t have to install anything. It goes really quickly. It just runs immediately, which is really, really cool.
And then you can share this with anybody you want. It’s in the zip file, so you can send it over email. You cannot, just so you know, send an exe just by itself over email.
Clients just won’t allow it because exes can contain viruses. And so they will not accept you to just send it, but you can build it that way.
Choose your platform and then share it with your friends and family. So what we did in this module was make a game.
We created a snake-like Clone Game where we discussed what the essence of a game was. We learned how to dissect a game. We created it. You learned how to code; you learned how to use functions. We’ve done so much and it’s been so awesome.
Now I want to take a little bit of time to dive deeper into the conversation of designing versus programming because in this game you’ve done both.
We’ve kind of blended them together, but I want to take some time to separate them and talk about the responsibilities of a designer versus a programmer and kind of what that life is like in the industry.
And we’re going to do that in the next module.