Placeholder Image

字幕列表 影片播放

  • 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.

  • Now.

  • What I want to do for that is just create a list.

  • So we're just gonna say body is equal to and then a blink list.

  • I'm also just Well, we're already up here and creating a class Very well, I'm gonna create turns is equal to And it's just gonna be this set of squiggly brackets here that will will get into in a bit.

  • Okay, so now that we're already in this snake function, what we need to do is we just need to define our parameters here that were passed in summer's gonna say self dark color equals coming said itself.

  • Done head equals cube, which I'll get into a second.

  • Given the position, say self thought body dot Upend our head self, the head like this.

  • So what we're doing here now is we're saying Okay, So the head of our snake, which is gonna be important because we need to know where that is at all times so we can move accordingly, is equal to a cube at the given position.

  • And the given position is what we pass in here.

  • I was like, this starting position of our sneak.

  • Or we could create a new snake.

  • We have multiple snakes moving around and anyway stuffs a position.

  • Now we're saying we're gonna depend to the body, this head.

  • So now this is in our list here on.

  • And then we can go through that list.

  • You can draw things, we can move it, we can check things.

  • We want all of our cubes to be ordered within this list.

  • So that's why I'm putting that in first.

  • Now, I'm just gonna give this one, which is called during X is equal to zero and turn.

  • Why?

  • Which is equal to one.

  • So what this is gonna do is we have a direction for ex in a direction for why, from roofing are snakes.

  • Obviously these air gonna be ah value, like negative 11 where zero.

  • And that's gonna be the same for why an X?

  • If, why or X is equal to one negative one.

  • Then the other one is gonna be equal to zero because you can only be moving in one direction at the same time.

  • And this is just gonna keep track of what direction?

  • Removing it.

  • And we'll use this in the next function.

  • We're gonna code, which is our move function.

  • So moving is pretty straightforward, at least in terms of just if you have one object moving around the screen getting it go left straight right.

  • But when you have the snake object, it has to turn at certain points.

  • So when I click left, um, the rest of the snake is still moving forward.

  • Once it reaches the point where the head turned left, then it has to turn left.

  • So that's where things get a bit complicated.

  • Anyways, to start what I'm gonna do that is to say, four event in a game, my typing Here, my game events dot gets like so and that we're gonna say if events don't type equals equals Hi, game quit like this.

  • And this is the 1st 1 we always wanna code in because we want to make sure that if they cooked that little red arrow, it actually works.

  • And we can quit any problem dot Quit like that.

  • And I'm just gonna say, if I actually don't even another if because the way I'm gonna do this is, uh, this other way you might have seen in power game before to move things around.

  • You said if event type equals key down and then you just check which key was pressed and moved to the left.

  • This is a similar thing, but it works smoother.

  • I've used both of them a lot.

  • And this is what I prefer.

  • Something's gonna use this soap I game duck key.

  • Don't get underscored press.

  • So this is gonna do is it's gonna get a list.

  • I actually think it's like a dictionary or something like that that has all the key values.

  • And then if they were pressed or not, it's that way.

  • If you press more than one key at once when you're looping through, it'll adjust to that.

  • Where is the other one?

  • You could only click one at a time, and you couldn't move like per se diagonally.

  • If you're moving like a character on the screen, it doesn't matter too much for this, but this is the way I like to do.

  • It's not gonna say four key in Keyes because remember, this is gonna give us all of the keys and then like one or zero value if they were clicked or not.

  • So we need to look through all of them in check room.

  • Say, if keys and then pregame dot key or look stuck.

  • Okay, Underscore left.

  • I thought it was key there.

  • And then we'll say it.

  • Keyes game K underscored, writes like that and can continue done here with up and down.

  • And then we'll get into exactly what is gonna happen when we click the skis.

  • So last one here, all right?

  • And they're okay.

  • So now ever made you eyes?

  • We're gonna change the direction According to what?

  • Keep me click.

  • So if we're going left, that means that our X has to be negative because the way again coordinates work in pie Game is in the top.

  • Left hand corner of your screen is 00 s.

  • So if we want to be moving left, we should be making our xnegative to move more toward zero.

  • Right?

  • If we want to know right now, you make our ex positive, which would be one in this case because we're gonna move one cube at a time to go further to the right.

  • Same thing with the why.

  • It's kind of weird in my game, how the y works because the more why you add, the further down you go so we'll do the same for experience itself during X equals negative one against moving left and we're going to set our journey.

  • Why equal to zero?

  • And this is because again, we don't have been moving in two directions at once and be going dago now this is what I was talking about.

  • It's We need to remember where we actually turned because if you just have one cubit around, that's fine.

  • Don't have multiple cues.

  • We need to be littering left, right.

  • We need to remember where we turned.

  • So the tale of our cube can turn at that point.

  • So that's why we have that self turns the list up.

  • Your justice turns.

  • I want to say dictionary or set whatever.

  • So we're gonna add to that yourself, though turns and we're gonna give it a new key.

  • And I'll go through how this works in a second self dot Pause here is equal to, and then we're gonna give just a list of self dug during X and self during blind.

  • Okay, so you might be kind of confused on what's going on here.

  • So I've created a new dictionary.

  • Appear, that's what I'm gonna call it.

  • Officially could think of it as a set as well.

  • And pretty much we're gonna add a key, which is the current position of the head of our snake, and then it's gonna be set equal to what direction we turned.

  • So we're saying we have a new turn at this position and our character, Where are snake moved?

  • Left.

  • So it's going to say we have self doubt during why which is right here is negative.

  • One self served under an X ray and then during wise eagle to zero.

  • So we know what way we actually turned.

  • And I hope that makes sense.

  • That's, uh, that's how we're gonna do that.

  • Now, I'm just gonna copy and paste these and then change these values.

  • So when we're going right, this is gonna be one.

  • Oops.

  • Same thing here.

  • So put that in.

  • This is gonna be zero and turn.

  • Why we're going up is gonna be negative one.

  • Now, when we're going down, we need to change this one and this to zero, and I'm just gonna get rid of that.

  • And there you Okay, so that is pretty much how we go about doing this.

  • Um, and I can also just put LF here as well because we don't wanna be able to move in more than one direction.

  • We're gonna prioritize the left A rakhi.

  • If they're not looking left a rookie, then we'll check.

  • They're doing the right.

  • Otherwise, up.

  • Otherwise, down because we don't want them to build a click more than one key at once.

  • So next we need to actually move our cube.

  • And this is where it gets a little bit more tricky in terms of looking, where are Cuba's?

  • And if it's at that turn, then we're gonna turn it.

  • So I'm gonna start coating that.

  • I'm kind of gonna go through exactly what I do.

  • So before I see in a numerator and I stop there and get me like that's innovation, say self doubt body, we're gonna look through the list of positions that we haven't on steak and say p equal seat opposition.

  • And I'm just gonna copy this, by the way, that's what this little arrow does here.

  • It just makes a copy so that we're not changing the position of our snake when we do things which just just follow along with it's not super important case.

  • We'll say if P is in self dot turns like this because remember, what we did is we added the position of our head to terms.

  • It's now we're going to say, Well, if this position is in the turns, then we're gonna move.

  • We're gonna turn right.

  • So let's sit here and say turn proofs equals self.

  • The trends peek like that.

  • So now we've seen that it's in, There s so now it's in there, we can find the index of it on.

  • We can grab the direction value so we know what we're gonna be moving.

  • And I was gonna say so don't move like this and turn zero turned one like So Okay, now, next I'm going to say If I equals equals the length of self taught body minus one goose, Then I'm something of yourself.

  • I don't know why that's capital itself turns dot pump, so I know I just did a lot of coding here, and this probably doesn't make a lot of sense, you guys.

  • So I'm gonna kind of run through exactly what I just did.

  • It's pretty.

  • It's a little bit difficult to explain.

  • So anyways, what we started by doing is we're going to say we're gonna get the index, and we're going to get the cube object in ourselves about buddy.

  • Because remember, ourself the body is made up of cube objects right here, Um, that have these property, right?

  • They're gonna have a direction.

  • And why Direction ex.

  • Ah, start color.

  • So on.

  • And we're gonna get We're gonna get to coding Cube in just a second.

  • So they're cube objects.

  • Now, all of these cute objects have a position.

  • So I'm saying for each object here, I'm gonna grab their position.

  • I'm going to see if that position is in our turn list and we create the term.

  • Listen, add to the turn list when we turned right when we cook left center, right, so on.

  • And then we're going to say OK, so the actual turn.

  • So we're actually gonna be moving is equal to that.

  • Are the turns list at that index.

  • Right.

  • So we grab, uh, the turn direction X in direction.

  • Why?

  • Which we stored there.

  • And now we're gonna just gonna say our cube dot move, which is another method.

  • They were gonna code, and we're giving it that direction.

  • Exit that direction.

  • Why?

  • So it knows what?

  • Wait, It needs to move pretty well.

  • And then I say, if eyes equal to the length of self, that body minus one So that means if we are on the last cube, we're gonna remove that turn.

  • So once that last cube hits that turn, we're going to remove it.

  • Because if we were just to leave that turn in the lists and that would mean any time you hit that position on the screen, regardless of if the snake was turning there not you automatically change directions if you don't remove it from the list.

  • Hope that makes sense.

  • Okay, Now, also, after this, if you're we're going to say if the, um what you call it if it's not in the list.

  • So if our positions not in the list, we still need to the snake because it's constantly moving.

  • So what do we do?

  • This we write else and I'm just gonna copy this in because it's gonna take a second to type because I have another file open and I'll go through.

  • It happened how this works nose looks like a lot.

  • But pretty much what this is doing for us is we're checking whether or not we've reached the edge of the screen for saying if we're moving left and the position so the exposition of our cube is less than or equal to zero.

  • Um, then we're gonna change that position so that it goes to the right side of the screen.

  • So the way that we can do that is by saying, seed out Rose minus one, because again, and you start counting at zero and computers eso If our roses like 20 then the last cube would be 19.

  • Analyst, Right.

  • They see that rose minus one.

  • We're gonna get the same.

  • Why value?

  • So we'll leave that there.

  • Okay.

  • And then we say otherwise, we're gonna check if we're going right for moving, right.

  • And same thing if we're at the edge of the screen, moved back to the left side by putting zero here and the next one if we're going down, what we're gonna be doing is again checking.

  • Are we less than Ah Rose minus one or greater than Roseline is one?

  • No, we're not.

  • Or if we are, then let's move us back up to the top of the screen by changing our Y value and then same thing here.

  • If you're moving uppers now, if none of that's true, so we're not at the edge of the screen.

  • We're not moving up.

  • Left down to whatever I just said, then we're just simply going to move our cube at the direction X in the direction.

  • Why of that cube already.

  • So if that cube is moving upwards and it's not turning and it's not going to the edge of the screen, it doesn't need to be changed.

  • Just keep moving it in whatever direction it's going.

  • And that's why I'm simply rafting the cubes.

  • Already Existing direction X and direction.

  • Why we're not changing anything were simply too saying, Okay, let's move you forward one like that.

  • I hope that makes sense.

  • That was a lot, and that's probably one of the hardest things in this program is figuring out the movement of this steak, so if you guys understand that, then that is the hard part, kind of over with.

  • And we're gonna get into this cube objects.

  • You're gonna understand what all this means and how our rows and columns kind of work and stuff.

  • Okay, so I'm just gonna forget about resetting odd queue for now because we'll worry about those later.

  • But let's get into the drama because I want to see if there's actually working and put this on the screen.

  • So simply for I see and new rates, why can I not spell the name right today?

  • Like that's the same thing self body like that we're gonna say if I present zero si dot draw and you'll see wide use in a second.

  • True else, See Deandra circus.

  • Okay, So the reasons for doing this old check here, rather than just drawing every object, is because when we draw the first snake object on sure, if you remember my other program, it had eyes.

  • So what I was simply want to do is make sure that when we draw that first object, we add eyes onto it just so we know where the head of the snake is.

  • We don't get confused.

  • Like what direction?

  • Swimming it although you shouldn't be.

  • I want at the little Liza Maybe just for aesthetic as well.

  • So what this true does optional parameter simply says Dries, If it's the 1st 1 in our list like that, if it's the head.

  • Ah, And then in this drama that you can see I have eyes was false.

  • But when you make it equal the tree we're gonna do a special thing in here that's gonna draw eyes for us.

  • Okay, so now that we've coated this this whole mess of stuff into the snake class, let's just run the program quickly and see if anything's happening.

  • So currently we just have the grid.

  • We don't have any areas, anything.

  • That's because we haven't drawn the snake object onto the screen yet.

  • So what we need to do is we just need to simply to s dot draw within our redraw window here.

  • So I'm just gonna type.

  • I'm gonna global s here now, global it down there as well say, stop, draw like that.

  • I am going to give a surface.

  • So let's give it a service and I'm going global s so that we can reference it there.

  • Ah, and is there a reason we aren't being drawn to the screen.

  • Let's just have a look quickly.

  • Stop, Draw, stop!

  • Drop.

  • Oh, it's because, yes, we are indeed trying s But we haven't drawn any of the Cube objects yet.

  • And it is calling the draw method on all of our cube objects, which we haven't yet created.

  • Hopefully that makes sense, but we'll go ahead and do that now.

  • So we have to cut a little bit more before we can see everything.

  • So just go ahead and get into the cube object.

  • I'll let you say self.

  • Don't start, people start.

  • Except I'm actually just gonna change is to position because I believe this is what it's supposed to be.

  • And there is himself during X, because one self don't.

  • Daron Y is equal to zero and self thought color is equal to cover.

  • And the reason we have this is because we're gonna be drawing later.

  • What?

  • I want to call it a snack for the for the cube to collect for the snake to collect.

  • So we want to change the color with that.

  • And you spelled it wrong, like three times during accent.

  • And why the reason I have during X you're set toe one initially is because I want to make sure that we start moving in a direction.

  • If I had this at zero, you would have to click a key before the snakes are training.

  • But I want right when we run the program for the snake to just start moving.

  • So I'm setting it already with a direction, axe and the reason these are optional again.

  • It's so that when we create a new cue object, we don't have to be implicitly say direction Exes one direction y zero because it's assumed that's always gonna be like that unless otherwise stated.

  • Okay, let's have a look.

  • What else do we need to do here?

  • We need to now code this move function.

  • It's actually really easy.

  • Um, I'm gonna do is what I say.

  • Since we're changing the direction acts in the direction why in our snake class, we need to do that in here so that it stays with the object during X equals Day X and then something.

  • Something during why?

  • Because, Dan, why like that?

  • And then we'll go down here and all we're gonna do is gonna change our position so soft, That position basically self taught po zero, which is our already existing position, plus self thought dead ex.

  • And they're saying self done Pause one, which is our already existing.

  • Why Value plus equals Self thought.

  • What, like that.

  • And just in case I haven't mentioned here, right, we're working with a grid system of 20 rows in this instant.

  • So when I say something like, uh, if ourself the opposition is like 13 or 10 10 I'm saying we're in the 10th row in the 10th colt.

  • I'm not saying the X value is equal to 10 and the Y values equal to 10 because it's not in the case of drawing on the screen, because our screen is actually a width of 500 pixels.

  • But just where we are in the grade, which is most important to us right now, is equal to like that about you like 1435 So, just just to make that clear for anyone who might have been confused, Okay, so now for drawing, um, it's a little bit harder just because of what I mentioned as well is the fact that we need to figure out the distance between each X value and each y values Wonder drugs me and pie game.

  • It draws in the top left hand corner of the objects.

  • If we draw a cube, we're drawing that in the top.

  • Left something to figure out what that x and y But he's gonna be for each cube when we're drunk to the screen.

  • So I'm just gonna call this gap here.

  • That is gonna be our distance and the same thing.

  • Self doubt.

  • W myself thought Rose and we're gonna change w and rows of down in our program later.

  • But we should just type it in now like this.

  • 502 100.

  • Just so we have that scent case herself that w answer.

  • You divided myself that rose again.

  • That's the same thing we did when we were drawing the grid to figure out the distance between our X and Y values.

  • OK, we'll say aye is equal to self.

  • That pause.

  • Yeah, that's gonna save us about typing in our function here or in our method.

  • Just say Jada Self don't pause one.

  • So you're saying I would stand for row?

  • J stands for column like a classically convention for it s so that we don't have Keep typing.

  • Pause here.

  • Oppose one when we move.

  • Okay, so the next one next thing you do is not draw a rectangle gonna say pie game don't draw got wrecked like this.

  • And then we need a surface we need to cover so self color, and then we need erect.

  • And here is a little bit of math.

  • I'm gonna take that and go through what it does, James, by displace one despised too.

  • So the reason I have is a plus ones, and these minus twos here is just so you can still see the grid when we draw the rectangle.

  • Because if we were to draw with exactly the dimensions of this distance, then what would happen is we'd cover the white lines of the grid, and it just looks kind of weird if you can't see the white lines.

  • So that's why I have a plus one added here and minus two just sword drawn inside of the circle are inside of the square a little bit and you'll see that in seconds.

  • What I'm simply doing is a multiplying my I value, which is gonna be my current like row column by the distance that we need to get to the next one.

  • And the same thing for J on Denzil add ones minus twos is just to make sure that we stay inside so quick.

  • Quick example.

  • If you understand, is say we're in a position 00 So our i zero r J's you we're going to say I multiplied by distance.

  • Let's say distances like 40.

  • And so is your mom's blood by 40 0 So you are multiplied by 40 0 So our position is 00 And that's correct.

  • Because if we're drawing in the top left hand corner here and we're in position to your zero, then that's where we should drop again.

  • If you go something like 0 10 uh, and you are and you move down.

  • I guess so.

  • 10.

  • You moving wide down, you're gonna say zero miles distant zero and you're gonna say 10 multiplied by distance.

  • I'd say it's 40.

  • Then we moved to that correct position down the screen.

  • So this is rectangle X y with tight.

  • Okay, makes sense.

  • Now the next thing we need to do is withdraw the eyes Now you can skip this part if you want.

  • I'm just gonna copied in because it's a little bit of math to make it them, like, perfectly aligned on the square.

  • Um, yeah.

  • So you say if I tze and we're saying our center is equal to distance modules to because that's gonna be a center, obviously are much sort of saying, divided by two is going to the middle of our cube and we're saying the radius that something the eye radius is gonna be, And then we go through a little bit of math year.

  • So Circle Middle is gonna be high multiplied by distance, plus center minus radius on dhe.

  • Then we're finding out the X, and this is just like a random exit.

  • I'm setting by just putting it eight pixels up, like, every time.

  • Because I don't want to calculate the ex.

  • Um, I don't feel like they're not.

  • And I'm drawing two circles based on, sir.

  • Committal.

  • One circle metal to and then the radius and a cover of black or color of white.

  • Actually, sir are No, that's block.

  • I'm confused anyways, with all that being said and done, and you guys probably pretty confused right now.

  • Let's just run to see if anything's actually happening.

  • And you can see we have a cube in the middle of the screen.

  • Yeah.

  • So that's all we've done with all this coding, Um, so far is drawn a cute.

  • So actually, not quite.

  • Because we still all we have to do now simply to start having that cube move and seeing our key presses work is just do a little bit in the main loop.

  • So let's move down to the main move now and have a look here.

  • What we need to do.

  • Okay, So what I'm gonna do is I'm simply just gonna call s stop, Move Which there?

  • Snake object every time this main luke runs.

  • And what this will I was to do is I want to call us before I read your window.

  • Is it's gonna go up to that method and snake.

  • It's gonna check every time that we run the loop.

  • If AKI's been pressed, if it has, we're gonna move accordingly.

  • And remember that in this move loop here, it moves all of the objects for us.

  • So it moves all those cube objects, um and then we're just drawing the grid.

  • We're drawing everything in this redraw function, so it'll just display that and update that to the screen.

  • So let's see here, and we get there to pull Object is not cold self Tupa.

  • 0% that their necks.

  • Um all right, one second, guys.

  • Hope it's cause I used okay, So apparently I am an idiot, and I forgot to add an equal sign here.

  • So the issue was simply that this looked something like this, that I changed it to square brackets when all I needed to do was put equal sign like this.

  • And now we should be up and working.

  • So too, reiterate the line here that the issue is that you guys probably saw this when I coded.

  • To be honest, um, is in move in Cube.

  • It's near the top of the program.

  • All you have to do is change this at an equal sign between this, like the bracket and the pose.

  • Now that we run it, we can see that we have a moving object and that we can move up, down left, right, et cetera.

  • So on and so forth like that.

  • Okay, so play around with that for a second, And now we'll get into adding cubes and we're actually almost done.

  • We just need to add that add cubed function message box little bit in the main Lupin that will be finished.

  • Okay, so what we gonna do now is that's all great.

  • We're moving around, but we need to add stuff to our cute right.

  • Look, we need to progress the game.

  • So to do this, we are going to generate what I want to call a random slack that we're gonna place on the screen that our snake and eat.

  • So the way we do this, I'm just gonna say positions equals item dot body and this item shouldn't be item should be right down.

  • It's just gonna be a snake object.

  • I don't know why I'm calling an item, but that's what I'm calling it.

  • Um, and we're simply just gonna set, like, a new list equal to that list.

  • So Okay, it's not going to say Well, true capital T we're going to x random brand.

  • Raines, if you haven't imported, ran to be, it makes you do that.

  • And why equals random dot rand range and rose again?

  • Should be global.

  • So let's make sure that this works Global Rose, like that's and after this, Would you say if I don't know how, I just type that, but the length of and I'm doing some fancy here and I'll explain what this does in a second.

  • Okay, So I know I just typed this stuff That probably makes absolutely no sense you guys, if you haven't been using python for a little bit.

  • But pretty much what this is going to do is we're gonna get a list of a filtered list, and we're going to see if any of the positions are the same as, um, like the current position of the snake pretty well.

  • So what we're doing is we're saying that we want to make sure that we're not gonna put a snack on top of the snake because if we have a really long snake, the chance of that happens actually somewhat high.

  • And I didn't do this when I first go to the game.

  • So what happened is when you have your snake living on the screen all of a sudden, the stack of pop up, but it's like on the tail of the snake, which you don't want.

  • So what this is doing and just type it out?

  • You don't really have to understand.

  • It is We're saying this This room's a function said and we're just checking if the zed dot position eyes equal to X y.

  • So if it's equal to X y, which is the position we just generated here by getting to random numbers, so x and y, then we're gonna have to do this again.

  • So we say continue and else we break like that And I'm just got simply gonna return in the trouble or a tuba.

  • Larry.

  • Oh, Nicholas X.

  • Y Okay, so this confusing.

  • But you can do this in, like, a much longer way with the four loop where you pretty much loop through every position in this list.

  • You check it against X and Y.

  • You see if it's the same if it's the same than do this loop again, otherwise you break and you return next week.

  • That's all we're doing in there to generate a random sex.

  • That message was gonna leave that and in here Now what?

  • We're good.

  • Okay, so now that I've created this this random snack function what I need to do is going to use it.

  • So I'm just gonna create a new object.

  • I'm gonna call this snack, and it's gonna be equal to another cube, Right?

  • Because we want to have the same functionality.

  • Want to draw it, possibly move it around.

  • Right?

  • So we just can't create a new cube object.

  • We're gonna give it a position of a random stacked with skins about this random stack and the random stack.

  • I believe it takes it Roman an item.

  • So what we're gonna do is we're gonna give it ro Rose, and we'll just give it our item, which could be s, um And then after that, what else do we need for Cube?

  • I don't think we actually need anything else.

  • Think that's all we need?

  • Oh, I'm gonna change the color.

  • That's what I want to do is change the color equal to green.

  • So red, green, blue to 55 feet green s O.

  • That shows up in this visible on screen.

  • And now what I'm gonna do down here in this wall loop is going to say snack.

  • I'm gonna check if our head of our snake has hit the stack.

  • And if it has, we're gonna add another part to the body of the snake.

  • Otherwise, we're not gonna do it right.

  • And then we're gonna generate a new snack and so on through and it's gonna say, If s dot body zero dot pause and this is gonna be a head right, because we haven't ordered.

  • I don't need those brackets is equal to snacked up.

  • Pause.

  • And remember, these are both cube objects.

  • That's works Fine, because I'm gonna be two bulls.

  • Then what we're gonna do is there something to say s stock Add cube, which is a method that we have there that we haven't actually written yet.

  • And we're going to say snack is now in cool too.

  • Well, a new cute.

  • So I'm gonna copy this and paste it here.

  • So all we're doing is now generating a new cube four snack.

  • I'm sure I could put something in that like change it, but this is just the way it works.

  • Okay, so now that we've done not, we should be able to move around screen and get the snack.

  • But we need to code in the ad snack method here, which I believe I have here are cute.

  • So the odd cube is pretty straightforward.

  • We just have to figure out where we're adding that stack or wherever adding that cube, I don't know why I keep coming back to the list.

  • So what we're gonna do is figure out where the Taylors and we want to add it after that tail.

  • So tail is equal to self taught body naked form.

  • The last element in that list gonna say d x d y which again is gonna be a direction extraction.

  • I was shorting it, shorting it here it's gonna detail.

  • But during X and tail gone during, why make this now again?

  • You could do this in two lines understanding like this, because it's faster.

  • And now I'm gonna copy some again, and we'll just go through exactly what this does.

  • Okay, so pretty much this looks complicated, but all it's gonna be doing is checking what direction that we're currently moving in, like the head of the key or the tail actually of the cube is moving in so that we can then make sure that when we add that cube, we know where to add it.

  • So for adding it like to the right of the cube to the left of the cube above it below it on.

  • And we can give it the correct direction to be moving in so pretty much, Um, what we do here is we just say we're gonna upend a new cube to our body and its position if we're moving to the right, which in here we are because the X equals one is one less than the exposition of that tail.

  • So whatever the last cube is one lesson that and that's so that we don't add a Q per se to the right when we're moving to the right and then we're just gonna have overlapping cubes and you're going to see them, right?

  • Same thing down here for moving down.

  • That means are not downstairs removing left.

  • That means we need to add the cube to the right side.

  • Eso won.

  • Plus that tale of the expedition of the tale of the Cube of the Snake story so that we can have it in the proper position.

  • Right?

  • And the same thing here with X and Y, uh, are with why?

  • So we're moving up or moving down when we add it above otherwise to be moved, we put it below.

  • So I just stumbled through that because there was a lot of different words.

  • They're very similar.

  • We need to do now is simply set the direction for that keeps that we added it in.

  • If we just left it like that, you wouldn't be moving anywhere.

  • It's a reason to change that to the current direction of that tail.

  • So whatever the tail's moving, that's where this new key was gonna be moving in that direction.

  • So it's gonna self body negative one dot turn x equals dx and then same thing down here except Ridge can change easily.

  • Why, during like equals D y and that again?

  • It's just that, uh, where our tail is moving at the current moment.

  • Okay, so now that we've got a lot done, it's time to see what syntax there we get.

  • Next name Rose is parameter and global one second, So I've actually never even ran into that air.

  • Oh, global rose as Okay, So let's just make this our and let's change this to be Mmm.

  • One second.

  • Wait.

  • Just get rid of this and this work there.

  • You.

  • So now we move around like this.

  • The only issue is our snack is not being generated on the screen.

  • And I don't know why that is exactly happening.

  • Oh, it's because we're not drawing the snack, that's why.

  • So now I'm a global snack up here.

  • Believe that's what I called it.

  • And I'm simply just gonna say snack, don't drop.

  • Take that And I gotta give it a surface.

  • So let's give its surface.

  • Let's run.

  • And heir named Sack is not defined because I didn't global it down here.

  • So cool with that.

  • Okay, so now we have the snack, it's on the screen and you can see that when we collect it, we have that cube added to the end of our snake, and that pretty much is almost the game done.

  • So all we need to do now is write that reset method and then we need to just simply check like when we lose a great Now the snake can go through each other through itself, and you can see we have a huge air when the snake can coat through itself.

  • So we need to make sure that when we hit ourselves.

  • Um, we like, end the game, and we need to write that message.

  • Will method as well.

  • So to do this again, I'm just gonna copy in this four loop on What kind of go to go from there.

  • So smoothing here.

  • Okay, so four X and range the length of s stop body.

  • We're gonna se esta by the X, which is gonna be where looping through every cube in our snake body.

  • We're checking if the position is in a list of all the positions after that works.

  • That's how we're checking the collision.

  • If you want to do it your own way, go ahead.

  • But this is the way that I like to hear.

  • Okay, so now what I'm gonna do is just wanna print to the consulate like their score.

  • Just so that I know of this recess score.

  • And that would say, Plus is the length of a star body, but this is gonna have to be converted to string unless I put a comma like that.

  • So let's just do it like that.

  • And then I am going to simply display a message box there.

  • Say message.

  • Looks like this.

  • Such underscored box.

  • We have a subject, and we have content.

  • And we'll do that in just a second.

  • But I haven't yet actually coated that.

  • And then we're gonna reset our snake by just doing.

  • I start reset, and this is gonna take a position, which is gonna be our starting position again.

  • 10.

  • 10 on

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.

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

A2 初級

蛇遊戲Python教程 (Snake Game Python Tutorial)

  • 12 0
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字