Placeholder Image

Subtitles section Play video

  • Hello.

  • Yes, I am on my holidays and it's incredibly help.

  • So I'm gonna keep this one quick and simple.

  • Let's make snake on.

  • Of course, I am talking classic retro snake really here in the console so you can see a snake here on I'm stealing from the snakes perspective, which could be quite a controversial way to do snake.

  • I remember this from the knock earphones on.

  • Of course, the snake grows as we pick up food and wee accumulator school.

  • And if the snake hits the wall or himself, that's the end of the game.

  • See the snake sent into a skeleton and we have to press space to play again.

  • Excellent.

  • Let's see how this don't.

  • It's not very many lines of code because I'm working from my possible man cave today.

  • I'm afraid we don't have as many luxuries as we usually do.

  • So I'm filming in a slightly lower resolution.

  • And of course I don't have my voice over microphone.

  • However, snake is so simple, I don't believe we need any of that to get the point across.

  • If you watch my videos, you'll know how my counsel programs go by now.

  • First thing we do is create extended blank council program, and then we'll want to have a screen buffer to it using the create console screen buffer.

  • I'll make sure there's some little taps Somewhere around here you can click on those.

  • I have a look at some of the videos.

  • We show exactly how this code works and what it does.

  • And, of course, what want to stop the screen with on screen Right now, I'm using 1 20 by 80.

  • I'm just going to been off the airport here, so we've got more screen to look at the code, and I'm going to create a simple game.

  • Luke, that's usually my game with dust is displayed.

  • Scream if it to the screen using the right counsel output character.

  • Like all my simple council games, we could break it down into three stages.

  • The first is timing and input.

  • The second is the game.

  • Logic on the third is displaced off to the player I've decided fundamentally are snake is going to be a link list.

  • This means I'm not going to have to derail anywhere story with snake segments up.

  • So I need a structure which represents a snake segment to store in our list, and this is very simply going to be on X and y.

  • Alternatively, I could stop the positions of the snakes head in snake tail.

  • And as the head moves along each time the tail also moves along.

  • I may take some sort of history of how the tail should fall in the head.

  • I think this might get a little bit complicated.

  • So instead, I'm just going to have a league list where each element in the list is a segment of the snakes body.

  • We'll see why this has some advantages.

  • Laser.

  • So each segment is just going to be a simple structure containing an X and Y permitted.

  • Therefore, we can create a little ist object to store a snake.

  • However, the snake needs some sort of default state.

  • I'm going to use the static initialize a list which which improved now in modern sea.

  • So for each element of the snake, I'm going to start some coordinates, and my snake is going to start at 60 15 which is the 60th column across the 15 throw down on the council.

  • And that's gonna be the first segment.

  • No, there's still the second segment now 61 Common 15.

  • And I'm gonna keep doing this all the way until I've got 10 segments of snake all in a line ours only on screen.

  • You're excellent.

  • You guys need some game state variables to so very simply, I'm going to have the location of where the food this food X and y go.

  • To maintain the score, I'm going to keep a variable, which is snake direction on.

  • This is going to be for the compass points, so zero will be pointing.

  • No one will be pointing east to south and freeze West compass points.

  • So the snake is going to start off heading west on Finally, I'm going to have a 1,000,000,000 for be debt.

  • This will be the end of game condition would Usually we're gonna work backwards this time.

  • We just stop with the presentation.

  • Of course, somebody is doing works right outside.

  • Might.

  • When I decide to start building, see if it's going to take very long.

  • So each famous gable today, the first thing I want to do is clear the screen.

  • I'm doing this by simply drawing space to every element of the screen buffer and then going to draw some furniture onto the screen.

  • So this is things like some shameless self promotion.

  • Yet on the title game on the players current score, this lawsuit was a little boundary of the top of the screen.

  • The next thing, of course, is to draw this snake, and this is quite easy with the also keyword, because the snake is a little list or we do is literate through the list, taking the coordinates of each body segment on drawing it accordingly.

  • Now, in this case, I'm using the be dead flag to draw either the skeleton, which is going to plus symbol or a segment of the snake body, which is going to be a capital in a very similar style.

  • I'm going to draw the snake's head on for this.

  • I just take the front elements off the linguist using front method here on the X and Y coordinates on in a similar way, I used to be dead variable to tell me, Should it be a capital exploring the snake is dead?

  • Or should it be that simple?

  • When the snake is alive, enjoying food couldn't be simpler.

  • We take food X and Y coordinates translate them, as we've done with all of these drawing methods, from a two dimensional corner system into a one dimensional for the screen.

  • But one was used, the percent sign to indicate food.

  • Let's take a quick look at this and see if it's working.

  • Excellence was compiled.

  • We can see at the top here.

  • We've got the border, including the shameless self promotion on the score.

  • On We've got a snake with the head on an element of food.

  • Sometimes getting the presentation out of the way early is the correct thing to do, because we could make little changes to the game logic now and immediately see the results.

  • There's only two controls in this game.

  • The left cake on the right.

  • So I'm crazy, Boolean said, which will start Lefty and Mikey onto the previous state off the lefty on the right.

  • I'm storing the previous states because I only want to acknowledge the input on the change of the inputs are first of all, compared the old state versus the new state.

  • But I also only want to look at when the key is pressed.

  • So I need this temple information in order to make this decision, this could be considered a latching behavior.

  • The usual press the key, but it will only send one event.

  • We're not going to continuously send events per frame, wants the keys.

  • Help on this will stop the snake from coiling Open, hitting yourself immediately as soon as the player presses.

  • Inara.

  • Kate.

  • Like all of my other counsel applications, I like to make liberal use of the Get a sink key state key Word is this will retrieve the immediate state off the left and right arrow keys.

  • These are the codes for it on these other conditions I just talked about.

  • So only in the condition off the key has been pressed for the right key increments.

  • Thes next direction.

  • And for the lefty, we will deck Ament the snake direction I've added some little Klaus is here to make sure that the snake direction wrapped around so it's always between 0123 Like Tetris, I don't want the game to run a full throttle of the CPU.

  • Instead, I needed to slow down a little bit just to make it possible for the player to actually acknowledge what's going up.

  • If we didn't do this the snake?

  • Would you shoot straight off the screen rather crudely?

  • I'm doing this with a hard delay.

  • Now we've dealt with presentation and input.

  • We can do the fun stuff.

  • Thegame logic.

  • Let's start by making the snake move.

  • Remember that my snake is just a leap list.

  • So to make it move pushed something into the head of the snake and I pop the tail on all of the snake will appear to move one along.

  • The tricky part is working out what coordinates to give the snake's head when you push the you had onto it.

  • So here I am, using the push front command on.

  • If the snake is going in the direction, I want to take one from the Y coordinates of the current front of the snake and you can see here I'm using the initializing list as on argument to keep the coat concise.

  • Exactly the same is done for the down except instead of minus one from Dwight's, plus one from the wife.

  • And as you might expect for right and left, we affect things in the X axis instead.

  • So that's adding the new head in.

  • You had position depending on where the snake is going.

  • And of course, at some point we're going to have to chop off a snake's tail on.

  • This is simply popping the last element of the linguist.

  • I see what happens now so we can see the snake scrolling along the screen.

  • Of course, we're not done any collision detection yet, so I don't expect anything to happen with food.

  • And if I press the right key and again and again again, the snake turns right.

  • Press the left cake.

  • The snake turns left.

  • Excellent.

  • So let's out in some collision detection.

  • The first thing we're going to check for this snake versus world.

  • So this is where we're checking If the head of the snake which remember, is always the front of the little list is within the boundaries that we've defined as being the gaming area, in this case, the X axis.

  • It's just between zero and the screen wet suits.

  • The whole weight of the consul on for the Y axis is between three and the screen height.

  • That's because I've got the border and information on the top of the screen.

  • I don't want Thio allowed the snake to go on top of that information.

  • If the snake does violate any of these conditions, we set our dead flag to truth, and we'll do something with the dead flight later.

  • But as you already know for the presentation this will This will change how the snake looks.

  • The second thing that snake and collide with is, of course, the food.

  • And this is a good thing.

  • It's easy to check for, because all we're looking for is whether the front of the late list I snakes had equals the same coordinates.

  • That's where we've decided food should be in that event.

  • We increase our school by one, and we want to play some new food.

  • This is where we have to think a little bit about the game design here.

  • We can't just randomly choose any word to put the food, because if we put it inside the snakes body, well, I guess it's a philosophical debate here as to whether thegame is relevant to tall.

  • But if this spoon is already inside the snakes body, the player can definitely not pick it up, so that's very unfair to the play.

  • Instead, what I've chosen to do here is to randomly choose coordinates on Dhe when the coordinates are valid, i e.

  • They are not somewhere that's already defined using this screen office.

  • As long as the screen buffer contains an empty space, that corner, then we're okay.

  • You might think it's a bit cheating to use the screen offer as a memory storage device will.

  • Indeed, you know why not.

  • If those blank space there, then we can accept that the randomly generated coordinates are valid.

  • This is one of those scenarios were thinking about the early game.

  • It doesn't really seem to make much sense.

  • But if you can imagine a player has been playing for five or six minutes of solar is a very long snake now that placing the food with inside the snake would be very in first place.

  • I guess one solution is we could always put the food where the snake had last been because we know that its tail will leave an empty space.

  • Of course, this would look like the snake is going to be eating his own feces on probably doesn't make a very clever game, because later on, when the snake is many, many loops were probably guaranteeing that we're putting the food in the space of the snake.

  • Never access.

  • Regardless, whatever the approach of choosing where to put the food, the next thing we need to do is grow the snake because it's a little list.

  • All I'm going to do is duplicate tale by another five segments.

  • It doesn't matter whether the tail segments overlap each other or not.

  • It's a late list, the system doesn't care and no information is lost.

  • And as a byproduct, we also get the nice aesthetic that the snake grows.

  • And, of course, the final collision to detect for is the snake versus itself.

  • Now we only need to check the head position of the snake when we need to see if it is overlapping anything of the snake segment.

  • In this instance, I chose not to use the auto keyword because I need access to the actual IT director.

  • So going old school for a moment and just manually entering the auto keyword.

  • With this highlighted here, it's effectively all gets results.

  • I check each snake body segment here, which is the coordinates, I accent.

  • I wipe with snakes head on.

  • If the head has the same coordinates as any off the body segments, then we set.

  • Being dead equals to truth may have noticed this at the front.

  • It, of course, I don't want to check the head against itself because that will always return true and it would be a very short game.

  • One of the nice things about see is when you do an if statement.

  • These conditions are evaluated in order so we can quickly reject things.

  • Let's do some testing.

  • The first thing to test its the snake.

  • It's insult food.

  • What we should see.

  • It's the snake growing.

  • Does the school has increased by one?

  • Well, now from the snake into a wall.

  • Oh dear.

  • Well, we saw the snake turned into a skeleton.

  • But of course the game didn't stop.

  • And that's because we don't do anything without being dead.

  • Lets out That's it.

  • In the event of snake death, I want to display a message on the screen, says Press space to play again.

  • So I just chosen the coordinates of my speedboat for an amusingly princess commands that very tightly for me.

  • I'm also going to change.

  • I'll make wild to exit on the events of B dead being true so in normal situations, I'll be dead is false.

  • This leaves me with a strange situation.

  • We're going to exit on me and gamely, but she's never going to be a good thing.

  • So I'm going to wreck battle in a second, Really.

  • And so when Interlude terminates on the event to be dead becoming true, I'm going to sit in a very tight while.

  • It's just waiting for the user to press the space bar, which again I'm using get a stinky state in order to do on it.

  • Sze, 32 which in taxes?

  • 20 Now this has a nice advantage that when they use it does press the space bar.

  • We go backgrounds.

  • While mainly on we reset the game to completely known state.

  • It's always good in programming to be able to, at any point, get to a known state.

  • Let's try again without testing.

  • This time, I'm going to crush the snake into the wall.

  • Excellent press space to play again.

  • I will.

  • We'll try to crush the snake into itself.

  • We go west space to play again, but it is something a bit strange here.

  • I can't get the snake to double back on itself.

  • I also feel the game isn't very well balanced in terms of the speed of the snake.

  • Both of these problems are because of the way that we're putting in a fix delay inside our main game live.

  • This delay effectively stops the game, so I'm not capturing all of the input that the player is putting in.

  • In fact, if I'm waiting 200 milliseconds, but that could be quite a lot of input.

  • That the player has tried to put in on the game is completely ignored.

  • Having a having fasted like this is causing this problem.

  • So I'm just going to come in out of the time being.

  • What I want to do is instead of delaying one chunk, I'm going to sit in a tighter Luke for a fixed amount of time.

  • But I'm going to acknowledge any key presses that kid.

  • In that way, I can update the snakes direction.

  • Hopefully, we can get the snake to double back on itself, which I think is gonna be very important for the player in the later stages of the game.

  • To do this, I'm going to use the krone library so gets very worthy here.

  • we get on dhe whilst I'm basically creating a time point here.

  • T one Getting the immediate type on the condition from my wallet is going to be that a duration created by subtracting the current time by the one that I've stored is less than 200 milliseconds with the next nice bracket.

  • So all of my input is now handle.

  • With this loop on this, Luke will only room whilst the duration is less than 200 milliseconds.

  • Let's see if that makes the snake easier to control.

  • Well, a snake.

  • It does look like it's controlling normally, and if I rapidly double tap, I could get the snake to immediately go back.

  • So and, of course, if I rapidly triple tap, the snake will completely crush into itself.

  • That's okay.

  • I think this rewards the player would be precise with their inputs.

  • There is one final thing, which I think the player might be a little bit wary off, and that is the game is hard that when the snake is moving open down rather than left right because of the aspect ratio of command problems, we can see that moving vertically, the snake moves much faster this gives the player the perception, having less time to make a decision.

  • We can address this by changing the delay based on whether the player is moving vertically or horizontally on.

  • This bit of code is exactly that.

  • If the snake direction is hard, I eat is going west or east when we might be late.

  • That, too will end up with the Warners remained, so we want to use a short delay.

  • In this case, we're going to use 120 milliseconds.

  • However, if this condition is false, snake must be moving vertically, so he wants to use a slightly larger delay.

  • And in this case, that's 200 milliseconds.

  • Let's see how that feels.

  • Well, it probably needs a little fine tuning, but you just feel a lot more balanced.

  • Quick, literally rotter.

  • I've set a screen height inadvertently to 80 years.

  • In fact, it is actually 30 on my console.

  • I only noticed this because food was generating and of course, I couldn't see where it was generating anywhere that's now fix.

  • The food should always appear on screen.

  • That's a very happy, very simple, yet feature complete version of snake in under 150 lines of code.

  • If you've enjoyed this video, please check out my on this program and you're the types of games and all sorts of utilities and applications.

  • Give us the thumbs up and have a think about subscribing.

  • As always, the code is available on gets up and you can follow me on Twitter at Jackie next night.

  • I'm gonna leave you this way with a little holiday movie.

  • See you next time.

  • Hello, perhaps the world's oldest computer.

  • Behind me, the operating system is simply the interpretation of an ancient race of the heavens processes that mechanics and physics of the universe a policeman's job, Spitz.

Hello.

Subtitles and vocabulary

Click the word to look it up Click the word to find further inforamtion about it