Adding Difficulty
Summary
In this tutorial, we enhance the challenge in our snake game by increasing the snake’s speed each time it eats food and introducing blocks that appear where the food was consumed, eventually filling the level.
Lesson 10 Transcript
So what I would like to do to add challenge to this is one; increase the speed of our snake each time we collect some food and two spawn a block inside of the level where we ate that food eventually filling up the level of so many blocks that it becomes impossible, just like the original snake.
So how would we go about doing this if we want to increase the speed of our snake? Well, we’ve already set that in the creative event. How do we add more?
Well, it’s just like the X and Y or direction that we’ve talked about. If we wanted to increase it, we could just say speed plus equal one. But where do we do that? That’s the question.
We can actually affect our snake object inside of our other objects. So let’s go to O B J food. And in the destroy event, when we die, we want to increase the speed of our snake.
(00:57)
And the way we do that is by typing the name of our object O B J Snake. You can see it turns red, indicating that it found it.
So now if we put the period, which is called dot notation, we will have access to all of the variables and all of the data we have used on O B J snake, and then we can set and get that data information.
So our speed is what we want to change here. So I’m going to say speed plus equals one. Now when we collect food, when it gets destroyed, it will increase the speed of our snake. Lemme press F5 and let’s try it out.
Okay, that works. We clearly start going a little faster each time. It’s a slow build, just like the original snake. It’ll get harder and harder as your score gets higher and higher, which is the fun part of the game.
(01:57)
Awesome. So we are increasing our speed, but now I want to spawn a block and here is where it gets a little tricky, and thinking like a clever programmer really helps.
So we want to spawn a block, but we cannot just spawn a block where we are the way it is right now. Well, we can, and let’s see what happens.
If I say instance create layer and I just use our X and y coordinate exactly where our food is currently at. We spawn it on instances and we create a block.
So if we do that and we run it, you’ll see that we run into a problem, the block gets spawned and the snake is immediately colliding with it, which means that we die and the game restarts over and over and over. So we can’t actually collect the food because the game just restarts indefinitely.
So what we want to do is make it so
(02:58)
That when we spawn it there, it doesn’t kill us for a little bit of time. And the way to do that is by altering the collision mask on our block.
Now, we haven’t talked about collision masks yet, so let’s do a quick overview of them and we’re going to dive way deeper into them in the next module where we make our space game.
So open up your S p R block or really any of your sprites. And over here on the left you have this option for a collision mask. So if you drop that down, this all becomes darker.
And the reason for that is that this Sprite, the entire Sprite, all 32 pixels of it are actually inside of its own collision mask. And so if an object touches this Sprite, this collision mask, then the collision event gets triggered.
If we open up our S P R food, you can see it a little more clearly.
(03:51)
Right now, even the spots that don’t have our Sprite in there will trigger the collision mask for some things. That’s okay. It doesn’t really matter if there are five or six pixels on each side where we can collect the food.
It’s making it a little bit easier for the player, not a big deal. You can come in here and you can change the collision mask. You can change the mode and the type.
So the mode is automatic full image or manual. Manual. You can come in here and actually set the collision mask. So if we did this, the only time that the collision event would trigger is when we were actually this far inside of the Sprite.
We don’t want to do that here, so let’s reset that. But what we want to do is actually take away the collision mask from our block. Unfortunately, there’s no way to just remove a collision mask entirely.
(04:47)
You can set it to something different, but it will still always have one. The way to remove it is to actually remove the Sprite itself.
So let’s open up our O B J block. And you can see over here that we can choose a Sprite and we can choose a collision mask. So if you ever wanted to have a Sprite with a unique collision mask, you can set that right here.
But what we want to do is actually remove the Sprite completely. Now, if you look in your room, it looks like it’s really, really messed up now, but it is still the same. It just changes the picture of what’s being displayed here and the stretching properties of that look different.
If we reset the Sprite, the room goes right back to the way it was, so you don’t need to worry about the way that looks. So let’s take it off again.
So what I want to do is spawn the block and have it set its Sprite back to itself, which will also reset the collision mask after a second or so so that we don’t run into it immediately.
So inside of our food, we’re actually still creating that block. That’s totally fine. So let’s keep that there and let’s go into our
(05:56)
block. And now when we get created, we are going to say we are going to set an alarm. And an alarm is just a ticker. It is something that will trigger after a little bit of time.
And the way you use an alarm is like this, the keyword alarm left bracket, which is not the one we’ve been using, the parenthetical marks that look like that.
This is a left bracket; it’s a square one right next to the P. So we’re going to open that up.
We’re going to say zero, and then we’re going to close it and we assign it with one equal sign to a number. And this is the number that will say how long until the alarm triggers.
So we’re just going to set it to be equal to 100. So this alarm will go off after 100 frames, not seconds, frames. So remember, our game runs at 60 frames a second.
(06:54)
So this is almost two seconds. Now the alarm will not go off if there is no code inside of it.
Let’s add an event alarm zero. It has to be that alarm because that’s the one we’ve set. We’re going to say reset Sprite.
The way we do this is by using a built-in property called Sprite Index. And this controls which Sprite this object has, which is really, really cool and very useful. So we’re just going to set it equal to S P R block.
And now when we run the game, our food will create a block. That block will then get set. And there we go, a hundred seconds, a hundred frames after that gets put there.
So you may have noticed though, that the walls weren’t there originally, which isn’t ideal. And these are also taking a really long time to come in. So let’s do two quick fixes.
(07:53)
The first one is in our room. We can actually double-click on this, and zoom in a little bit. And we can actually manually set our Sprite right here.
And the way we can do that is in our creation code. This code that happens when this instance, this specific instance, when it gets created, it runs right here.
So if we just say Sprite index equals s P R block, that will set this instance to that. So that’s awesome. So we’ll save that and we’ll go around and we’ll do it to each one of these. So creation code set creation code set.
Last one, creation code set. Now if I press F five in our game, the walls will be there immediately, which is awesome. Okay, that’s the first fix. And lemme exit out of these because they take up a lot of room
(08:54)
here.
The second fix that we want to do is maybe increase how fast this goes on, but we have to be really careful because our speed changes throughout the game. If we spawn it at 10 frames after we collected 10 food, that’s fine.
We’re moving so fast we won’t collide with it. But if we do that in the beginning, well we’ve got a problem. So what we need to do is just a little bit of math and all we’re going to do is just divide 100 by OBJ snake speed.
So the faster we get, the lower this number becomes. So the first time I’m going to run it and show you the first time we run into it, it’s going to take a while.
But the second time and the third time and the fourth time, it’s only going to start spawning faster and faster and faster. And now we have a game that’s coming together to be fun, challenging and to kind of resemble the original spirit of snake. And that is adding challenge and difficulty into our game.
What I want to talk about next is the design topic of difficulty. Is it right that we’re doing this? Should we make it easier? Should we add some options for difficulty?
As a designer, you have all the power in that area. So let’s talk about difficulty.
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