4
4
If Python and Arcade are installed, this example can be run from the command line with:
5
5
python -m arcade.examples.timer
6
6
"""
7
-
8
7
import arcade
9
8
10
9
SCREEN_WIDTH = 800
@@ -20,7 +19,14 @@ class MyGame(arcade.Window):
20
19
def __init__ (self ):
21
20
super ().__init__ (SCREEN_WIDTH , SCREEN_HEIGHT , SCREEN_TITLE )
22
21
self .total_time = 0.0
23
- self .output = "00:00:00"
22
+ self .timer_text = arcade .Text (
23
+ text = "00:00:00" ,
24
+ start_x = SCREEN_WIDTH // 2 ,
25
+ start_y = SCREEN_HEIGHT // 2 - 50 ,
26
+ color = arcade .color .WHITE ,
27
+ font_size = 100 ,
28
+ anchor_x = "center" ,
29
+ )
24
30
25
31
def setup (self ):
26
32
"""
@@ -31,21 +37,17 @@ def setup(self):
31
37
32
38
def on_draw (self ):
33
39
""" Use this function to draw everything to the screen. """
34
-
35
- # Start the render. This must happen before any drawing
36
- # commands. We do NOT need an stop render command.
40
+ # Clear all pixels in the window
37
41
self .clear ()
38
42
39
- # Output the timer text.
40
- arcade .draw_text (self .output ,
41
- SCREEN_WIDTH // 2 , SCREEN_HEIGHT // 2 - 50 ,
42
- arcade .color .WHITE , 100 ,
43
- anchor_x = "center" )
43
+ # Draw the timer text
44
+ self .timer_text .draw ()
44
45
45
46
def on_update (self , delta_time ):
46
47
"""
47
48
All the logic to move, and the game logic goes here.
48
49
"""
50
+ # Accumulate the total time
49
51
self .total_time += delta_time
50
52
51
53
# Calculate minutes
@@ -57,8 +59,8 @@ def on_update(self, delta_time):
57
59
# Calculate 100s of a second
58
60
seconds_100s = int ((self .total_time - seconds ) * 100 )
59
61
60
- # Figure out our output
61
- self .output = f"{ minutes :02d} :{ seconds :02d} :{ seconds_100s :02d} "
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} "
62
64
63
65
64
66
def main ():
0 commit comments