Placeholder Image

Subtitles section Play video

  • So let's just go ahead and get a look at what this final product is gonna look like Esso on like an X pi x cred.

  • So, whatever you want it to be, you have little snake moving around.

  • There's a snack is what I want to call it that shows up, and as you collect it, it increases the length of the tail.

  • And then, obviously, if you run into yourself and I just did that by clicking the back, you always going forward.

  • It gives you a lair message.

  • Years as you ran into your tale of your length was nine dot, dot, dot Whatever, um and then you can continue and play as long as you want.

  • Anyways, what I'm gonna be doing in here is gonna be coating everything object orientated.

  • I know that there's a much faster and easier way to do this is really teach you that much.

  • This way, you're gonna learn a bit about objects.

  • But class is how we can kind of work with, um and you'll see like how the flow of my program goes.

  • And you should learn quite a bit, especially if you're someone a beginner programmer.

  • What we're gonna be using to make this is module called Pi Game.

  • If you don't know the pie game and you want to learn about it, before you do this, I would recommend that.

  • All right, so let's start coating so pretty much.

  • What I've done is I've just set up the flow, the program, all the classes and the function just to remind myself what I need to write.

  • And while I'm talking here, it's really good ideas.

  • If you guys copy this down, so we're gonna have is gonna have to main objects will have a cube object and a snake object.

  • And our snake object is going to contain cube objects if that makes sense.

  • So each one of those little red squares it was moving around is a cube object in the whole thing is our snake object.

  • We're gonna have a few functions here.

  • Draw grid, redraw window, random snack, message box and main.

  • So let's get started with our main function here, and this is what's gonna be our main loop.

  • So essentially, what I need for the main Luke to start is we need to make a service on high game to do this.

  • We do When he was a game got display, but sent underscoring mode.

  • And then I'm gonna set it.

  • Uh, I don't see here.

  • We're doing with the height to be with and heights like this.

  • Oops, bud.

  • And I'm going to make a very well pickled with e equals height equals.

  • And we'll just set this to 500 by but have 500 like, So now we need another variable.

  • And justice as Rose can delete this down here is gonna do something.

  • We'll do that later on.

  • This is how many rose you're gonna have.

  • What, Rose or columns?

  • Whatever you consider us, Whatever you want.

  • Just make sure it divides 500 evenly.

  • Otherwise, you're gonna have, like, weird looking Rose, if you know what I mean.

  • So I'm just gonna set mine to 20.

  • That's what I'm using the other one.

  • But if you want to make it harder, said it to something like 10 on and there won't be much room for the snake to move around.

  • And the games ago, Faster.

  • Pretty well.

  • Okay.

  • Next, Where we need to do is we just set up a snake object, so I know that we haven't even created, like anything in the snake classy up.

  • I'm going to s snake, and I'm just gonna give it a position.

  • So in this case, we want to color say, just gonna be red because red, green, blue to 5500.

  • And I'm going to give a position.

  • We're just start in the middle of something to start at 10.

  • 10.

  • Now, let's move into our main loop.

  • So I was gonna say, Wow, flag create my variable here.

  • Fly equals true.

  • And then we're just gonna start by doing a pie game.

  • Ticks up my game, Got time delay 50.

  • Like so.

  • And it's just gonna delay us.

  • Uh, I want to say, like, 500.5 or 50 milliseconds every time so that our program doesn't run too fast.

  • Must look, it's gonna create this clocked on Tick.

  • Gonna put that at 10.

  • And I'm going to use them to create a clock object up here on this is a built in thing in a game.

  • And what this is gonna do is I explain in just one second.

  • What's that?

  • Typing it by game.

  • The clock.

  • I believe it's time clock.

  • I think so.

  • So what this clock toe don't tick is actually going to do is gonna make sure that our game doesn't run more than 10 frames per second.

  • So that would mean essentially that are snake would be able to move, like, 10 blocks in one second.

  • And again, we don't want it to be that fast.

  • So that's why we're else of delaying by a few milliseconds here if you put this too low.

  • Um, Then again, it's going to kill.

  • You can play around with speed.

  • This is kind of what I found was the best speed in a May.

  • Very depending on what machine it shouldn't.

  • It might.

  • So you might want to just change these numbers again.

  • Lower.

  • This ghost is the faster it's gonna be.

  • And the lower this one goes is the slower it's gonna be.

  • So they're kind of inversely proportional like that.

  • All right.

  • Next what we're gonna do here, I'm just gonna call redraw Window.

  • And I bet you that so bad redraw window like so I'm just gonna give it a service, which is in this case is gonna be win that we've created up here.

  • And for now, that's all I'm gonna put in my main loop here, and we'll move more into that once I start coating some other stuff.

  • Okay, so now that we have our redraw window being called here, I'm gonna go and I'm gonna start coding our redraw window.

  • So what this is essentially gonna do Because we first need to update the display supply game display dogs update like so.

  • And we also need thio draw the Great.

  • So what's it going to draw?

  • A grid?

  • We're gonna cost about same surface that we were given.

  • Um, and we've owned that.

  • Where else we gonna fill the screen?

  • So when God fill in this case, I'm just gonna use black 000 And there we are.

  • That's already need to do right now, But essentially, we're also gonna need to draw this snake.

  • Uh, we're gonna need to draw a few other things on the screen that we'll we'll get to later.

  • Okay, So drug raid, let's move into this one.

  • Now, what we need to do here is like draw a grid, which is pretty easy to do.

  • So what we do is we were given rose and surface, so it's probably a good idea if I actually passed in the rose and surface.

  • So I'm just gonna global them here so that I can reference them.

  • I think this is gonna work with the global rose and with, like that.

  • And here we also need to make these global because I don't pass them.

  • And every time I draw a grid, we're gonna say with uh, and Rose And this height variable is actually not necessary because we are just gonna draw a square like a square surface every time.

  • So we can just make this the same.

  • We don't need two variables here within height.

  • Do it.

  • We're gonna contain the same number.

  • And we're so global with.

  • And Rose, I go here, draw grid given with rose and surface.

  • So now ran.

  • Put in.

  • Well, first, we're gonna roast with row and service like that.

  • Okay, so that should be working now in here.

  • What we first need to do when we're drawing a grid is we want to figure out how big each square in the grid is going to be because of what we're gonna do is we're gonna just draw lines going down and across, but we need to figure out where to draw those lines.

  • So we have to figure out kind of like the gap between each of the lines.

  • So the way we do that understand create very well here.

  • Size between is equal to or with insecure, divided by our rose.

  • This is just so that we don't get, like, large decimal numbers because that we cannot pass into our draw line method in ah, in Python.

  • Okay, so now I'm just gonna create X variable and y very and I mean, it's settings to zero to say four l in range.

  • And it's just staying for line pretty well and Rose cut.

  • Now what I'm gonna do is I'm gonna increment my ex by thes size between I'm gonna increment my wife are the same.

  • So why?

  • Because why plus size between get rid of one of the spaces here What?

  • Okay, and now we're simply just gonna draw two lines.

  • So to draw a line in pie game, all we have to do is pregame dot draw don't wind.

  • And the arguments that takes is a surface we need a cover in this case is gonna be white.

  • So you 2 52 55 to 55.

  • Whites were gonna draw at X zero comma X w and I'll go through this in just one second what this does, and I'm gonna copy this and draw one more and then explain how this is gonna work pretty well.

  • Okay, so now we need zero boy and w Why, Like so.

  • Okay, So what this is gonna do is this is gonna drop two lines for us, every loop of this for them.

  • And these arguments here is the star position of the line and the end position of the line.

  • So the first line that we want to draw is going horizontal, Which means we don't want to change the Y value at all.

  • Eh?

  • So what we need to do is we need to find the X, and then we're gonna put why at zero, because we're gonna be at the top.

  • I think that's right, at least.

  • Oh, sorry.

  • This list line is drunk being drawn down, not being drawn to the right.

  • I was getting confused.

  • Okay, so we're gonna change the X, but we're going to keep the wife at zero, and then we're going to stay at that same X.

  • And we're gonna keep the why at the width of the screen so that we're going far enough down.

  • And then same thing here, this one is going horizontal.

  • Excuse me.

  • I must set up a force.

  • Our exes always gonna stay at zero.

  • And our wise gonna be what's changing a CZ.

  • We draw horizontal lines across the screen.

  • I hope that makes them stare on how that works.

  • And I'm gonna move on from that.

  • And that's all we need for the drug greed, uh, function.

  • So just go ahead.

  • I probably mistake here, but we'll run the program to see if everything's working.

  • Named Win is not to find Window Phil s.

  • So I've called this win.

  • One really needs to be surface like that.

  • So it's trying out, and there we go.

  • We get a nice little grid on our screen, like so 20 by 20.

  • Great, they were.

  • So now let's move into another function.

  • Or actually, let's start coating on the classes here.

  • So, like I was talking about before, we're gonna have a snake object which is gonna contain a bunch of cube objects.

  • So essentially, what we're gonna do is we're gonna have a list of cubes on, and that's gonna be known as the snake body.

  • So let's go ahead and write that in.