Creating New Food
Summary
In this lesson, we enhance our snake game by introducing a feature where a new piece of food appears at a random location every time the snake consumes the existing food.
We delve into the concepts of instances and objects and learn how to use the “instance_create” function with random ranges to spawn food at various positions within specified boundaries.
Lesson 6 Transcript
We have the ability to run into our food and make it disappear, but that’s not much of a game.
We’re going to take this game and every time we run into that food, we want to create a new one. We are destroying this food when we touch the snake, which means that we can add a new event called Destroy.
So when this gets destroyed, we want a new one. So create new food.
Now the way this works is using a function called instance underscore create, and then we have two options, depth and layer. Now if we open up our room, we can actually take a look at what both of those mean.
These are layers. You can see it says right here, layers for room zero. You have instances which is our objects, and we’ve been throwing this word around a lot and I’m going to continue to use it.
(01:03)
But I want to provide a little bit of description between the difference of instances and objects. An object is what we’ve created over here. An instance is the specific thing in the level that is there.
So if I add another food inside of here, we only have one object, but now we have two instances. We have two specific creations of O B J food and we have to know them apart. We have to be able to tell which one is which because they each run their own event.
Otherwise, if we ran into one food, then all of them would disappear and that’s not what we want. So an instance is the specific object in the level objects are over here, which are kind of like the blueprint for how something works.
Alright? So whenever we do something with an object, we’re really doing it with an instance.
(02:06)
So we have an instances layer and a background layer. And both of these have something called depth.
Now it is locked on the instances layer and we don’t really want to do anything with the depth, but understand that the depth is basically how close we are to the screen.
So if I moved this background above instances, its depth is now zero and the instances depth is now 100, which means that the background is actually closer to the screen than the objects we have in the level.
So we don’t want that. So we can create something at a specific depth or we can put something on a specific layer. What we want to do is put it on a layer.
So now we have to give it a coordinate, an X and a Y, which is on our grid system, which we understand, but then we also have to give it a layer ID or name.
(03:00)
So the easiest way is to give it this name right here, and it has to be spelled exactly the way this is. If it’s not, then you have a problem.
So make sure that it’s spelled exactly like these capitals and everything. And last, we tell it which object we want to spawn in.
So where do we want to create this new food? Well, we don’t know, right? We want it to be random. We don’t want it to just spawn at our current X and Y because then it would just always stay exactly where it is.
And we can’t just put in a number like 25 and 400 because then it would always spawn at 25 and 400 and that would be a pretty boring game if the food was just always in the exact same spot.
So what we need is a random set of numbers, but we don’t have any yet.
(03:58)
So let’s go ahead and create a few. So we are going to say random x equals, and now we want to choose a random number. We’re going to say random underscore range.
The reason we’re doing range is it allows us to choose between two sets of numbers, whereas random says it’s from zero to the number you put. This one says it’s from the first number to the second number.
This is important because if we didn’t use the range, then our food could easily end up at zero, which would be right there. And we’re eventually going to add some borders to the walls here.
And if the food spawned over here, it would be impossible to get. So we have to make sure that it at least spawns over here so that we have access to it.
Okay, so let’s put in the range between 32, and let’s come in here and take a look at where this range would be at. So we double-click on this, it’s at the furthest, and that’s 992.
(05:11)
Let’s put that in there. So that is the random x, so we can pass that in right here.
Now we need a random Y and that’s going to be equal to the random range again. So let’s come up here. I think it’s going to be 32.
Once again, yeah, the Y is equal to 32. So we’ll set 32 and that is going to be coming down here, 736. So let’s put that in there.
And you might be wondering where all these numbers are, what’s controlling, how big or how small these numbers can get. Well, in our room over here, which we haven’t talked about yet, and we will get too soon, we have a room property.
So if I actually minimize some of these, you can see there are room settings and there’s width and there’s height. That’s actually controlling how big our room is.
If we were to come in here and make the width of the room 2000, all of a sudden it’s now huge.
(06:20)
So this is what’s actually controlling the width and the height of our room. We’re not going to mess with that right now because this is a really good width and height. But we’re going to come in and talk about rooms at a later point.
So we have our X and our Y. So let’s go ahead and put that in. Now we need to pass in the layer name as a string. So we’re going to say instances. And if this is spelled wrong, I’ll show you exactly what happens. We’ll get to that in just a second. And the name of the object is O B J food.
So now we’re going to create this and it’s going to be put somewhere random every single time, which is pretty cool. So let’s bring our food back right next to us. So we can easily test this out and run it one more time.
(07:07)
We’re going to run it, seeing it correctly here. There we go. It created a new one down there. So if we come down and we touch this one, we should probably increase the speed.
This is really slow. Boom, it makes a new one over there. Awesome. So now we are trying to catch this food over and over and over again. So it’s turning into a game.
Now, if we didn’t spell this correctly, let’s say we left out an in. If we try to do this, we’re not actually going to get an error when we run the game.
We’ve gotten those errors before, but this is only going to occur when we collide with it. It’s going to say it can’t find this layer; it doesn’t exist.
So we have to make sure it’s spelled correctly. And it looks like you can actually leave this as a lowercase letter, and it’s perfectly fine.
(07:52)
I would still encourage you to follow the naming convention exactly with uppercase, but GameMaker Studio is very lenient when it comes to programming, so it will let you get away with that. And that is creating a new piece of food every single time we run into it.
One last thing I want to mention is that if we wanted to have our game be truly random in the sense that it always picks different numbers, then we need to call one function. And that is a randomized function.
So if we just call this one-time inside of our game, randomize, you can spell it with a Z or an Ss. They both do the same thing.
You call this and then these numbers are actually going to be different if you don’t call that, I’m sure you’ve already seen if you’ve played your game a couple of times, it actually is always in the exact same spot right there and they’ll always be in the same spot.
But if we call randomized just one time, then it’ll always be in a new spot.
(08:55)
So randomize sets the seed and we’re not going to get into what a seed is or anything like that just right now, but it sets the seed to a random number.
If you don’t call this, then it’s always the exact same seed, which means your game will run at the exact same time, it’ll pick the same random numbers every time.
And for what we’re going to be doing in this course, we’re not going to reuse, or randomize because a lot of the time we want to debug or find out or test something, and we always want it to be the same.
That way it’s easy to test if we wanted to test collecting our food or if something specific happens when we did A or B and it was truly random, it makes it a lot harder to test.
But once you actually make your game, you probably want to throw randomize inside of it. It makes it much more interesting and much more fun.
But I wanted to add that there so that you know about it, and you can change that in the future if you so desire. Alright, we’re going to take a quick break from coding and have a design talk about randomness in games.
GameMaker Studio 2 – Module 3: Attack of the Snake
- Lesson 1 – Breaking Down Snake
- Lesson 2 – Creating Our Snake
- Lesson 3 – Comments
- Lesson 4 – Snake Food
- Lesson 5 – What Are Functions
- Lesson 6 – Creating New Food
- Lesson 7 – Randomness
- Lesson 8 – What Is A Game
- Lesson 9 – Losing The Game
- Lesson 10 – Adding Difficulty
- Lesson 11 – Game Difficulty
- Lesson 12 – Game Expectations
- Lesson 13 – Adding A Score
- Lesson 14 – Sharing Your Game
Leave a Reply