In this lesson we will be creating the course.
First I want you to go to the models folder.
We want to create a new python file.
So right click on the folder and choose new.
Then click on python file.
Let's set the name to course and hit.
OK now on this new file we'll start by importing the first one we'll be importing logging.
Also we will be using time then from the drone app models base.
We will import Singleton which we made in the previous lessons.
Don't forget to set the logger to logging.
Get logger underscore name based on PIP AIDS.
We would need to set two black lines here but I don't think we can fit all the codes in the screen so
I'll have to set aside the PEP 8 code stall for now.
First we set a class called base course later on this lesson we will be creating different courses like
course a and core speed.
But this base class will be the base for all courses the middle class will be set to Singleton.
Next we will set the edit followed by self name of the course and the drone that will be flying the
course as an argument self.
Name will be set to name so we can distinguish the courses we will set the status initially.
This will be set to 0.
The status will be used to operate the drone for example status 1 will be takeoff Status 2 will be flip.
Also we want to check if the drone is flying through the course.
So we set is running.
This will be set to false.
The game will be based on competing the time.
So we need to get the start time.
This we can set to None.
Also we need to get the elapsed time also set to None.
Lastly we set the drone to the drone attribute.
Now we go on to define the start method once the race starts.
We want to record the start time the start time will be set to the current time.
Once the drone is in the race we want to change is running to true.
Next we need a stop method.
If the drone is not in the race we don't need to stop the drone so we can return false.
If the drone is in the race we need to stop.
So we set is running to false as we set the drone to stop.
We need to set the status go back to zero again.
Next we need to figure out how long the race is taking.
So we set an update elapsed method if the drone is not in the race there is no elapsed time so we need
to return a nun.
If the drone is in the race we need to calculate the elapsed time which will be current time minus start
time.
Then the lapse time should be returned.
Next will be the method to run the drone on the course.
This will be underscore run.
This method will be invoked from the courses to make the drone take off or to fly forward or back.
So we want this method to override in this base class.
We will set to raise an error if the method is invoked before it's implemented.
Now we set the run method.
If the drone is not running we need to return false.
If the drone is running the status will be changed by adding 1 then the underscore run will be invoked
and the update elapse method will be invoked.
OK.
This part seems a bit complicated so let's go through the code so I can explain what will happen once
the smartphone is shaped.
This run method will be invoked then the status will be changed to 1 and the underscore run method will
be invoked in this underscore run.
If all the commands will to be defined based on the status number every time there is a shake the drone
will fly according to the status and with update elapsed we will receive how much time it took to complete
the course.
Once the course is finished the stop method will be invoked and all the values will go back to the initial
settings.
Now let's start by creating course a.
So you will get a better understanding set.
Class course a witch will inherit the base course defined the underscore run so it can overwrite.
Now we will set the actions to the status number starting with Status 1.
The drone will take off next let's set status 10 in the run method we set to add 1 to the status which
means to get to the next action.
We need to shake the smartphone 10 times.
Once it hits 10 the drone will rotate clockwise 90 degrees.
You can customize the actions on your program.
Before this lesson we'll set it to clockwise.
We can also add status 15 and 20 and 25 since the drone will be moving 90 degrees each time.
By the time we reach 25 the drone will be facing where it started.
Next we'll set the state is 30.
To flip the drone front forward then at State is 40.
We set the drone to flip back.
Lastly at status is 50 we will set the drone to land and run the stop method so the status will go back
to the initial settings.
Imagine that this is of course a Mario Kart.
Next let's set course.
B Again this will inherit the base course class.
I'll go on and create the Course B but you can customize the entire course if you wish to do so.
First we define underscore run and override at state as one we will set the drone to take off at status
10.
We set the drone to flip front the flips are quite fun to watch.
So you should definitely have it in the course at status 20.
We set the drone to flip back here we will set another status.
If self elapsed is not a nun and the elapsed time is bigger than 10 and smaller than 15 the status will
skip to status 45
next we sit status 30 which will flip the drone to the right as we added the if statement to state is
20.
Once all the condition is cleared the status will jump to 45 skipping status is in between.
Let's copy status 30 to make status 40 and set this to flip left.
You can think of this as a bonus stage in Mario Kart where you get a certain item that helps you warp
to a better position.
Lastly we set status 50 to land and not to forget to stop as we just seen.
You can use elapsed to create a bonus stage or even set back statuses to make the course more interesting.
It's all up to you so have fun creating the courses next suit we want to create a global function name.
Get courses this function will create the course and return the value.
The first course will be number one with course a as the course name followed by an argument drone.
This will be the same for the next course we main course name will be course be followed by an argument
set to drone as we said.
Each course to Singleton.
Even if the get course function is invoked multiple times the present course A or B will be returned
in our next lesson.
We will go to the server P Y file to invoke the horse.