In the last step,
we figured out how to detect when the player turtle hits a car and to stop the
while loop which stops the game at that point.
Now we're going to figure out how we can detect when the turtle reaches the
other side of the screen so that once our turtle makes a successful crossing,
then we can return the turtle to the starting position and speed up all the
cars. So I'm going to do that right here,
and I'm going to detect a successful crossing.
So what is a successful crossing? Well, the player turtle has the reach
the finish line, right?
Which we've defined the Y value for as 280 here in the constant.
Now you could do this in a number of ways.
You could define a method here or simply just get hold of the player's position
and then figure out if it's over 280 inside our main.py.
Now I'm going to create a method here instead
and I'm going to call the method is_at_finish_line.
This method is going to return true
if the turtle is at the finish line and false while it's not.
So how do we detect this? Well,
we can get our self.ycor
and we can see if that is greater than 280,
which is our finished line Y value.
What that means we've pretty much gone over the finish line, right?
So if this is the case, then we're going to return true.
But while that's not the case, any other time when we check this,
we're going to return false.
So now coming back to our main.py,
we can call our player.is_at_finish_line and we can say,
if this is true, well in that case
we're going to get the player to go back to the starting position.
So inside our player class let's create yet another method which I'm going to
call go_to_start.
And this just involves getting our player to go to the starting position
which we've defined here in a constant.
So notice how we've got a little bit of repetition here,
so we can actually delete this line and simply call self.
go_to_start. Back in our main.py
once the player is at the finish line, we're going to get the player to go
to the stat.r And if we go ahead and run our code
as it is right now,
you can see that once the turtle makes it across the road,
[inaudible]
it goes back to the starting position.
Now the very last thing we need to do is to increase the speed of the cars when
the player reaches the other side,
because effectively they've just gone to the next level.
What we can do is when the player reaches the finish line,
we can get the car_manager to increase the speed of the cars.
Let's create a method inside our CarManager class
which we'll call level_up. And in this method,
we're going to get the cars' speed to increase by 10 every time.
So if we have an attribute called car_speed and we initially set it as the
starting move distance,
and then we use this car_speed to determine how far it should move backward
each time. Well then when we level up,
we can say self.car_speed now increases by the increment that we've defined
here, which is the move increment.
So now when our player reaches the other side at the finish line,
then in addition to getting the player to go to the start,
we're going to get the car_manager to level up our cars as well.
So now when my turtle reaches the other side of the screen,
not only does the turtle go back to the original position,
but the cars also increase in speed.