Lesson 8
Stopwatch Decomposition
A label to show the running time
A start button to start/pause the running time
A reset button that sets the running time to 0
Exercise
Implement a basic stopwatch GUI. You do not have to change the time label. Only show the components on the screen.
Use print statements to check button clicks with the
on_left_click
method.
Extending the Label
Class
We can extend the Label
class,
similar to how we extend the Sprite
class.
from pycat.core import Label
class AwesomeLabel(Label):
def on_create(self):
pass
def on_update(self, dt):
pass
The on_update
’s dt
Parameter
The on_update
method is called approximately 60 times per second.
If we need to know the precise time since the previous on_update
, we
can check it using the dt
parameter.
def on_update(self, dt):
print('time since previous on_update:', dt)
Exercise
Write a custom class extending
Label
.Create an instance of your class with
window.create_label(YourLabelClassName)
add a property to your Label class to store the running time.
update the running time with the
dt
propertyupdate
self.text
to show the current running time.
Note: When you finish, your stopwatch time will increase automatically. You do not need to implement start/pause/reset yet.
Controlling the Stopwatch State
The stopwatch can be in one of two states. The behavior of the stopwatch is different depending on the current state. Either it is running (the time is increasing), or it is not running (the time is not increasing).
How/Where should we store the current state of our stopwatch?
What event changes the stopwatch’s state?
Where do we define the different behaviors?
Exercise
Add pause/start, and reset functionality to your stopwatch program. Can you find a python function to show fewer digits of the running time?
Extensions
After you finish the stopwatch, think of an original idea with time as a central theme and create it!
Some suggestions are:
Visualize the running time graphically.
Add sound effects
Implement a count-down timer. Use
input
to get the total time.