On-Screen Timer#
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"""
7from __future__ import annotations
8
9import arcade
10
11SCREEN_WIDTH = 800
12SCREEN_HEIGHT = 600
13SCREEN_TITLE = "Timer Example"
14
15
16class MyGame(arcade.Window):
17 """
18 Main application class.
19 """
20
21 def __init__(self):
22 super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
23 self.total_time = 0.0
24 self.timer_text = arcade.Text(
25 text="00:00:00",
26 start_x=SCREEN_WIDTH // 2,
27 start_y=SCREEN_HEIGHT // 2 - 50,
28 color=arcade.color.WHITE,
29 font_size=100,
30 anchor_x="center",
31 )
32
33 def setup(self):
34 """
35 Set up the application.
36 """
37 self.background_color = arcade.color.ALABAMA_CRIMSON
38 self.total_time = 0.0
39
40 def on_draw(self):
41 """ Use this function to draw everything to the screen. """
42 # Clear all pixels in the window
43 self.clear()
44
45 # Draw the timer text
46 self.timer_text.draw()
47
48 def on_update(self, delta_time):
49 """
50 All the logic to move, and the game logic goes here.
51 """
52 # Accumulate the total time
53 self.total_time += delta_time
54
55 # Calculate minutes
56 minutes = int(self.total_time) // 60
57
58 # Calculate seconds by using a modulus (remainder)
59 seconds = int(self.total_time) % 60
60
61 # Calculate 100s of a second
62 seconds_100s = int((self.total_time - seconds) * 100)
63
64 # Use string formatting to create a new text string for our timer
65 self.timer_text.text = f"{minutes:02d}:{seconds:02d}:{seconds_100s:02d}"
66
67
68def main():
69 window = MyGame()
70 window.setup()
71 arcade.run()
72
73
74if __name__ == "__main__":
75 main()