In the last lesson,
we figured out how we can get our turtle race up and running and get our turtles
at the starting positions. Now,
the next job we have is to figure out how we can get these turtles to start
moving. The movement is going to have to be randomized
and each step they take will have to be a random number. That way they don't go
straight to the end
and we end up with a terrible gaming experience.
So what we're going to do is we're going to create a random number,
let's say between 0 and 10,
and then we're going to tell the turtle to go forward by that random amount.
So it could be anywhere between 0 and 10.
And then we're going to get this to repeat itself many, many times.
As you might've guessed,
this is probably going to involve some sort of while loop.
So let's say we start out with a variable called is_race_on and it starts out as
false.
But then once the user has made their bet and we can check to see
user_bet exists, then in that case
we're going to switch that is_race_on to true.
And then we can use our is_race_on inside our while loop.
Now this way we prevent our while loop from starting up while the user is still
deciding on which turtle they're going to bet on.
So it doesn't start prematurely.
Now when the user has made their bet and the race is now on, well
now we're going to need to get hold of a random number.
We're going to use the random module to get hold of a random int between the
range of 0 and 10.
Now randint is actually inclusive,
so the random number could be zero all the way up to 10.
So now we're gonna call this the rand_distance,
and this is going to be the distance that one of our turtles is going to move
forwards by.
So we want some sort of turtle.forward and the distance that it goes forward
is the random distance.
Now we have lots of turtles that we've created,
but we kind of haven't really done anything with them.
What we want to do instead is to have some sort of list of turtle.
So we'll call all_turtles,
and this list starts out as empty.
But every single time we create a new turtle,
then we can append that turtle to this list. So at this stage,
it doesn't really make sense to call all the turtles tim anymore, right?
Because they're all going to be individual instances.
So let's go ahead and refactor and rename this to a new_turtle.
And now what's happening in our for loop is we're creating a new turtle and
each of those new turtles are going to be appended to our all_turtles list.
So now we're going to have a list of multiple turtle instances,
and each of those instances is going to have a different state.
So for example,
they already start out with a different Y-position and a different color.
But later on,
we're also going to get them to move forwards by a different amount.
So we can loop through our list of turtles,
so for turtle in all_turtles,
let's go ahead and create a random distance for each of them and then move that
turtle forward by the random distance.
So now if we run our code
and see it in action,
you can see that once all of the turtles have gone into their starting
positions, they stop creeping along each time by a different amount.
Now, however, our turtles are actually going to keep going until forever.
It's basically a loop that's never going to stop.
So let's think about what the stopping conditions should be. Well,
once any of these turtles have actually reached the end of the screen
which is the X-value of +250,
once they've reached this line, then that's kind of the end of the race,
right?
But a turtle object is a 40 by 40 object.
So if we're only detecting for the 250,
then our turtle would have already overshot.
So instead, we want to detect when the turtle has reached the 230 mark. Back in our
code in our for-loop,
we can check to see if the turtle that we're currently looping through,
if it's X-coordinate is greater than
230. Well, in this case, it's basically won.
Now at this stage,
if we print this current turtle's color,
then you'll actually see something a little bit strange.
You can see that firstly,
it prints multiple values because the while loop is still continuing and the
other turtles continue to go forward.
But the first value is going to be the turtle that won.
And you can see it prints yellow and yellow.
So what's going on here? Well, if we look at this color method,
you can see that you can either use it to set the pen color and fill color,
or it will return the pen color and fill color.
So when it prints out two colors,
the first one is going to be the pen color and the second is going to be the
fill color. So if we only want to know the pen color,
then we can just get hold of the turtle.pencolor.
This is going to be the color of the body of the turtle. So in our code,
we can say that the winning color is equal to the current turtle.pencolor.
And then we can now check to see if the winning color is equal to the user's
bet, because if this is the case, well then the user has won, right?
So we can tell them you've won.
And then we can tell them the color of the winning turtle.
Now, however,
if this is false or rather in the else statement,
well, in that case, we're going to have to print the same thing
but now we're going to tell them that they've lost.
Yeah.
Finally, when the first turtle reaches the X-coordinate,
when this first color gets printed,
we want to stop the while loop. This way
the remaining turtles don't continue to nudge forward
and we get end up with lots of winners like this. So inside this
if statement, when the turtle's X-coordinate has surpassed 230,
we're going to change the is_race_on to false.
Now we're ready to test out our code. And again,
I'm going to bet on the red turtle and I'm going to speed up the playback speed
of this race when I'm editing this video.
And now the game has ended and you can see that the blue turtle is the one that
first got its head across the finish line.
And it tells me just as much. I've lost and the blue turtle is the winner.
So we've now used Python turtle to build a very simple turtle racing game.
And we're starting to use these X and Y positions in the turtle coordinate
system. Now, these coordinates take a little bit of getting used to,
especially thinking about things such as if a turtle is 40 pixels wide,
then in order to get the turtle to the starting line
we have to subtract half of the width of the turtle, which is 20.
So then 250 minus 20 becomes 230.
And it's things like this that takes a little bit of getting used to.
So what I really recommend is to have a play around with these values,
make it smaller, or make it larger, change the size of the screen.
And just until you're really familiar with how the coordinate system works
before you proceed to the next lesson, because as you keep going,
I'm going to presume that you've already had a play around with it and you know
exactly how it works. Now,
the other thing that we saw a lot of in this project is this concept of having
multiple objects created from the same class. So in this case,
we're using the turtle class to construct a turtle object called Timmy,
but we've also got a separate object called Tommy. And Timmy and Tommy are both
Tuttle objects, but they're different instances. So they can act independently,
they can have different appearances, they can have different colors,
different attributes, and also a different speed of movement.
So we've seen in our turtle race that each of these instances can act of their
own accord and have different state.
And that's shown in the different speed of movement and this capability of
creating multiple objects which can act and behave independently
is really the secret to why Object Oriented Programming can be so
powerful.
We can create so many of these objects and get them to do our bidding in various
parts of our program. And in the coming lessons
as we build more and more complex projects,
you'll see this concept of having multiple instances with different state being
shown again and again. And we're going to come to see and enjoy the benefits.