All right guys. In this lesson, we're going to be building out our turtle race.
And through building this race,
we're going to get more familiar with these concepts of state and instances.
And that's because we need to create multiple turtles to join our race.
This is what we're aiming for. In the beginning,
there's going to be a little popup that will ask us to bet who will win the
race. And it asked me to choose a color.
Now the turtles are in the color of the rainbow,
so I'm just going to pick the red one. Once I hit okay,
we have all of our turtles lined up in the starting position and they will start
making random steps towards the right edge of the screen. Now,
the first hurdle that reaches past the right edge of the screen is going to be
the winner.
And at the moment it looks like the green turtle is probably going to get there
first. There we go. So as soon as a turtle crosses the finish line,
then it will print out a line telling us whether if we won or whether
if we lost our bets and which turtle won the game.
So this is the goal.
Now I'm going to get started by deleting all of the code inside our previous
Etch-A-Sketch project other than these few lines.
So I've got Tim my turtle, screen
which is a screen object that is going to hold the screen until I click on it so
that I can see what's going on. Now, because in this game
the dimensions of the screen or of the window is really crucial,
I'm not just going to get the default screen size show up. Instead
I'm actually going to use one of the methods that is in the screen object
which is called setup. And setup, as you can see,
allows me to set up the width and the height of this window
that's going to show up.
So I'm going to set the width of my screen to 500 and the height to 400.
So I should end up with a 500 pixel wide, 400 pixel high screen.
And once I hit run, you can see this is what it gets resized to.
Now, if you came across this code and it was somebody else's code,
then these numbers and their positions don't actually make a lot of sense,
which one's the height, which one's the width. So in these situations,
I recommend using keyword arguments rather than positional arguments. Oh,
let's write out the parameters explicitly. Our width
to be 500 and our height to be 400. This way
when somebody else comes along and reads your code,
it's a lot easier to understand rather than just 500 and 400,
cause it could be both ways.
So now that we've got our screen set up,
the next thing I want to do is to bring up that popup and ask the user to make a
bet. Now, if we take a look at the Python documentation,
um, where we've been spending a lot of time recently,
you can see that there is a method on the screen
which is called textinput.
And this is going to show up a popup window and it allow the user to look at the
prompt and the title and to enter a piece of text.
And if you want the user to enter a number, then you would use numinput.
So let's go ahead and add that here. So it will be a method from screen
which is called textinput and then it's got two parameters.
One is the title which in my case
I'm just going to say 'Make your bet'.
And then the second parameter is going to be the prompt. And in my case,
I will probably say, um,
which turtle win the race and tell them to enter a color.
Now, this works really similarly to our input
which we've been really used to because it will return the string and we can
catch that string inside a variable.
So we'll call it user_bet and we'll set it to equal the output from this
method.
Now let's give this a shot.
Let's run the code and then let's enter a color, hit okay.
And if we take a look in our console,
you can see that that bet string is printed in here.
So now the next thing we're going to do is we need to do something with our
turtle. I'm going to move it down a little bit.
And what I want my turtle to do is I want it to go to the start of the line.
So the very left edge of my screen. Now I could,
of course, just say tim.backward by, um,
however many paces,
but thinking forward and knowing that I'm going to have six or seven of these
turtles,
I can't just tell them all to go backwards because they're going to go to the
same position. Instead, we're going to use a method that the turtle has
which is called goto.
And this allows us to define an X value and a Y value.
But how do we know what X and Y values to give it? Well,
we first have to understand how the Python turtle coordinate system works.
So if you imagine your program window as a graph where the center of it is at
the coordinate (0, 0). Well
then if you have a window that has a height of 400,
then that graph will have a Y-axis that extends from the center at zero all the
way up to the top edge and then from the middle
it goes down to the very bottom edge, which is going to be -200.
So 200 plus 200 makes up 400 and the same happens with the X-axis.
So if the width of the screen is 500, then
the X-axis goes from the center 0 to positive 250,
so half of 500. And then from zero to -250.
So here's a question. Let's say we wanted to move our turtle here.
What do you think would be the X and Y coordinate of that point?
2: Okay. So let's say
that this is roughly halfway on the X-axis and about halfway on the Y-axis.
Then half of 250 is 125 and half of 200 is 100.
So at this point would have the coordinate of 125 by 100.
If we wanted our turtle to go all the way to the left side of the screen
say maybe starting over here or starting over here, well,
then we would have to use that
goto method and then specify a X-value and a Y-value. Following the logic of the
total coordinate system then in order to move our turtle to the very left edge,
then we should supply an X-value of minus 250
which is half of the width of 500.
Now the Y-value will determine where on the Y-axis our turtle moves to.
So if we had it at zero
then it would basically just go straight backwards and it would not go up or
down. But let's say that we put it at, um,
-100. So now if I run this code,
you can see that it goes in the right direction,
but it's actually gone off the screen.
So what's happened here is that -250 is on the very,
very edge of the window.
And once our turtle moves over there and the arrow is at the back,
then you can't actually see it at all.
So let's try changing that X-value and shifting our turtle a little bit more to
the right. Then you can see it moves to pretty much the start of the window.
So I recommend using this goto method and trying out some different numbers
just to see where it ends up on the screen.
And once you put in some different numbers in here,
you'll start to get the hang of how this coordinate system actually works.
But just remember that the X-axis is along the horizontal.
It goes from zero to positive and zero to negative.
And then the Y-axis is along the vertical,
going from zero to positive and zero to negative.
So we want to get rid of this line and we don't actually want our turtle to draw
at all. So to do that, we're going to do tim.penup,
and we're actually never going to put the pen down because we're going to be
moving the turtle itself.
And the other thing is that it would be really nice if instead of an arrow,
we actually got a turtle.
So we can of course say tim.shape and set it to a turtle shape,
but here's an even easier way. When we create a new turtle,
look at the prompt that it's giving us.
It's actually giving us a way to initialize a new turtle object already with a
shape set up.
Now this shape is set to have a default value
which is basically the arrow.
But we can also specify the shape and give it the turtle shape to begin with.
Now, when I run the code you can see that firstly,
I get a turtle shape and then the pen is up and I'm not drawing
and then it moves to this place on the graph; -230, -100.
So now the next thing we want to do is to be able to create lots of turtles,
right?
What if we had all of the colors in the rainbow and we create a turtle for each
color?
So there's six colors in this list and these of course,
correspond to the colors which turtle will recognize.
So here's a challenge for you.
I want you to create six turtles,
one for each of the colors in this list of colors.
You're aiming for the turtles to all go to the starting line in a distribution
that looks something like this. It doesn't have to be precise,
you don't have to get it perfectly right.
But just make sure that they're sort of evenly spaced out and they are at the
starting line. So this is what you're aiming for; six turtles, six colors,
all starting at the starting point along a different point on the Y axis.
Pause the video and give that a go.
So we know that in order to create lots of turtles,
we're going to need some sort of a loop.
So let's go ahead and use a for loop to say turtle_index
and we're going to create a range to specify how many turtles we need.
Our range function will go from 0 to 6.
And remember that the range function actually doesn't include the number six,
so it'll create a range from 0 to 5. And once we've done that,
then we can indent this block of code so that we create six turtles.
But at the moment, they're all going to the same position.
So how can we change this? Well,
we want the X-position to always be the same for all the turtles,
because the X-position is along the horizontal axis
and we want all the turtles to start at the starting point.
Nobody gets a headstart. But the Y-position is the thing that we want to change.
A really simple way of doing this is to simply create a list of Y-positions
and you can work out some sort of reasonable position that you want to take.
So let's say we start out at -70 and then we just increased by 30 each
time.
So then that becomes -40 and then -10 and then 20,
50 and 80.
So it's roughly distributed somewhere along the middle. Now,
instead of using this Y-position
which is hardcoded -100,
we're going to take the Y-positions and then pass in our turtle_index like
this. So now when we rerun this code,
you can see that for each of the turtles
they're all going to get an individual Y-position
and they're all separated by 30 in distance.
Now we've got our turtles neatly lined up at the starting point.
The next thing is to give them a different color.
So we're going to use the list of colors and then picking out the color using
the turtle_index.
So now we should get some multicolored turtles and it will be much easier to bet
on a turtle. So now that we have our turtle race
all set up and ready to go,
the next step is to actually get the turtles to start moving.
But before we can do that,
I really want you to get a good grasp of how the coordinate system works in
turtle. So in the next lesson,
I've got a quick quiz for you just to make sure that you really understand
what's going on here.