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