Check out a free preview of the full Get Kids into Coding with Scratch course
The "Snake Game" Lesson is part of the full, Get Kids into Coding with Scratch course featured in this preview video. Here's what you'd learn in this lesson:
Steve discusses utilizing the Pen extension to create infinite and finite versions of a snake game. The video sensing tool is also discussed in this segment.
Transcript from the "Snake Game" Lesson
[00:00:00]
>> As well, and so these are kind of here as well. And then there's one point I make, possibly the most dangerous game of Etch A Sketch, as I lost immediately. This snake will never clean up after itself. So it's basically, you might wanna keep a score, there's a timer function as well, to see how long you can survive before it goes back on itself.
[00:00:25]
And we've got actually how to make a realistic game of Snake. We're not gonna build this whole way through cuz it's actually a little bit involved. But we'll talk through it cuz I think it's super interesting, again, you can explore it as well. If you go to this one in particular, I've gone through and commented on all the code as well.
[00:00:42]
So it's kinda go step by step about what each piece is doing. And I'll kinda explain it to you while we're here together. But let's kinda just take a tour of it, and you can kind of follow along. Let me move my comments out of the way from when I cleaned up my code.
[00:00:57]
Whee, cool, so we've got a few things that happen. I've got when the green flag is checked we have this set up, cuz I didn't want to see that all over my thing, right? Which is delete everything, start it in the right place, erase the page, put everything back to normal.
[00:01:17]
Our controls are the controls you've seen 1,000 times at this point. It's like the Pac-Man rules, which is that we always move forward. Touching the color purple, you're done, cuz the snake is purple. If touching the apple, get longer is really the only thing, and move the apple.
[00:01:39]
The interesting part is that basically the way this works is, if you look at it, there are two lists happening here. I'm keeping two lists before it gets restarted, which is for every segment of the snake, where those go, right? So what happens is, when the snake moves forward, it stores its last position, very similar to the platformer.
[00:02:07]
It moves one forward, right, draw all those previous positions, I just go through the list of the x and y. Remember when we did that, going through the catchphrases? It's the same idea, go through the list, use the pen tool to draw those segments, then pop the last one off until you eat another apple, right?
[00:02:27]
If you have more segments than the number of apples eaten, shorten it again. And then, you'll always have a number of trailing ones, Caleb was talking about this earlier. A number of trailing snake segments where they were previously losing the last one every time, but adding the new head of the snake onto the end as well, and so we draw the snake behind it each time.
[00:02:46]
It uses the same thing we saw in the platformer with that, I'll show you, where is the, draw a snake? Right, does it run without screen refresh as well? Right, so the snake is getting drawn but you don't see it getting drawn, you can turn it off and kinda see it, I'll show you.
[00:03:09]
Let's hide those, [LAUGH] this list is taking up a lot of space. So that's what happens if you have the screen refresh turned on. It is literally drawing those pieces of snake behind it every time, that's obnoxious. So you don't wanna see that, all you wanna see is the end result of figuring out where I should have put the pen down each time.
[00:03:29]
It's actually using stamp, which is basically, again, I'm using everything you understand about a pen, that motion, to make a stamp of the sprite. So unlike infinite snake that just goes on forever, this one will draw all the pieces right behind it for whatever the length should be.
[00:03:46]
>> Well, you don't have a head, it's headless.
>> So, once upon a time, there weren't iPhones, there were Nokia phones, and they only [LAUGH] had a certain number of pixels on them, and this is all you got. You didn't even get a real-looking apple, you just got a different dot that you were chasing down, and to text, you only had numbers.
[00:04:06]
And if you wanted to make an S, you had to hit 9 like four times, or 7 four times, okay? Life is hard sometimes, [LAUGH] I also had to walk uphill to school both ways, I don't know how that happened. [INAUDIBLE]
>> [LAUGH] It's pixel style.
>> Really?
[00:04:26]
[LAUGH]
>> This is the snake I grew up with, okay? [LAUGH]
>> Can you run into your tail?
>> This is your dad snake. [LAUGH]
>> Can you run into your tail or no?
>> No.
>> Does it work that way?
>> I had it set on Nokia Snake rules, you cannot run into your own tail.
[00:04:41]
[LAUGH] I followed Nokia Snake, Margaret, are you fact checking whether it was true in Nokia snake? [LAUGH] [INAUDIBLE]
>> I am pretty sure that under official Snake rules you cannot turn back on yourself.
>> I don't think you can. [CROSSTALK]
>> I have the Snake game.
>> You have the Snake game, okay?
[00:05:02]
>> What?
>> In Google Snake, if you turn back on yourself, nothing happens.
>> Correct.
>> Did I get fact-checked?
>> [LAUGH]
>> Yeah, I'm playing it right now.
>> I am pretty sure I have played, yeah, can you turn back or you're just showing it?
>> Yeah, I was just showing them what it was like, but yeah.
[00:05:21]
>> Yeah, that is the-
>> The point of Snake is it gets harder when the tail gets longer.
>> Yeah, I think you can't double back on yourself, cuz then-
>> The hard part was not to bump into other snakes.
>> The original Snake-
>> [LAUGH] OG Snake.
[00:05:36]
>> You are the snake, you continue to grow until you fill up the whole screen almost.
>> Do you see the technology we were working with?
>> [LAUGH]
>> Yeah, this is basically inspired from a 1976 arcade game called Blockade, you really wanna go deep.
>> This is the version of Snake that we are looking at, that snake can go through, I think it looks like.
[00:05:58]
>> To the other side?
>> Yeah.
>> There's no walls?
>> Well, it's like the old Pac-Man where if you come out of one side, it comes out on the other side, yeah.
>> [LAUGH] I was thinking this is what's gonna happen on my flight home, right now my snake does not go through the walls.
[00:06:15]
It might by the time [LAUGH] you watch this, I'm like, that's cool. But yeah, the pen tool, other than the idea that I'm keeping track of two x's and y's simultaneously to kinda keep the x and y coordinates. Cuz, for job descriptors in the room, there is no objects or anything, you only have arrays of single values.
[00:06:38]
Other than that, it's like a lot of the same concepts we've talked about in this, just with a pen tool drawing behind it. So I would check that out, let me just show you one more cool thing that I would encourage you on your explorations. And all of these are linked from the website with how to do them and stuff along those lines.
[00:07:00]
Check it out, there's a video sensing, and this is just the stupidest thing in the world. I think I might have to, hold on, let's go into my projects. Where's my video cat? When I, Touch the cat, it says hello, so now I am the sprite.
>> I didn't know you were a cat.
[00:07:44]
>> I'm not the cat, I'm me, the cat is the cat. Right, and so you can actually use the webcam you can control stuff through. There's a bunch of really cool extensions for how you can build on top of this as well.
>> You can kind of try to, can you do Fruit Ninja?
[00:08:04]
>> Yo. [LAUGH]
>> Very interesting to play on the ride home. [LAUGH]
>> Boy, there went my weekend. [LAUGH]
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops