On-Screen Timer

Screenshot of running a timer
timer.py
 1"""
 2Show a timer on-screen.
 3
 4If Python and Arcade are installed, this example can be run from the command line with:
 5python -m arcade.examples.timer
 6"""
 7
 8import arcade
 9
10SCREEN_WIDTH = 800
11SCREEN_HEIGHT = 600
12SCREEN_TITLE = "Timer Example"
13
14
15class MyGame(arcade.Window):
16    """
17    Main application class.
18    """
19
20    def __init__(self):
21        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
22        self.total_time = 0.0
23
24    def setup(self):
25        """
26        Set up the application.
27        """
28        arcade.set_background_color(arcade.color.WHITE)
29        self.total_time = 0.0
30
31    def on_draw(self):
32        """ Use this function to draw everything to the screen. """
33
34        # Start the render. This must happen before any drawing
35        # commands. We do NOT need an stop render command.
36        arcade.start_render()
37
38        # Calculate minutes
39        minutes = int(self.total_time) // 60
40
41        # Calculate seconds by using a modulus (remainder)
42        seconds = int(self.total_time) % 60
43
44        # Figure out our output
45        output = f"Time: {minutes:02d}:{seconds:02d}"
46
47        # Output the timer text.
48        arcade.draw_text(output, 300, 300, arcade.color.BLACK, 30)
49
50    def on_update(self, delta_time):
51        """
52        All the logic to move, and the game logic goes here.
53        """
54        self.total_time += delta_time
55
56
57def main():
58    window = MyGame()
59    window.setup()
60    arcade.run()
61
62
63if __name__ == "__main__":
64    main()