Skip to content

Commit a0213ac

Browse files
committed
Examples: timer.py should use Text object
1 parent 6cf6605 commit a0213ac

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

arcade/examples/timer.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
If Python and Arcade are installed, this example can be run from the command line with:
55
python -m arcade.examples.timer
66
"""
7-
87
import arcade
98

109
SCREEN_WIDTH = 800
@@ -20,7 +19,14 @@ class MyGame(arcade.Window):
2019
def __init__(self):
2120
super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
2221
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+
)
2430

2531
def setup(self):
2632
"""
@@ -31,21 +37,17 @@ def setup(self):
3137

3238
def on_draw(self):
3339
""" 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
3741
self.clear()
3842

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()
4445

4546
def on_update(self, delta_time):
4647
"""
4748
All the logic to move, and the game logic goes here.
4849
"""
50+
# Accumulate the total time
4951
self.total_time += delta_time
5052

5153
# Calculate minutes
@@ -57,8 +59,8 @@ def on_update(self, delta_time):
5759
# Calculate 100s of a second
5860
seconds_100s = int((self.total_time - seconds) * 100)
5961

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}"
6264

6365

6466
def main():

0 commit comments

Comments
 (0)