Drawing Text¶
drawing_text.py¶
1"""
2Example showing how to draw text to the screen.
3
4If Python and Arcade are installed, this example can be run from the command line with:
5python -m arcade.examples.drawing_text
6"""
7import arcade
8
9SCREEN_WIDTH = 500
10SCREEN_HEIGHT = 500
11SCREEN_TITLE = "Drawing Text Example"
12
13
14class MyGame(arcade.Window):
15 """
16 Main application class.
17 """
18
19 def __init__(self, width, height, title):
20 super().__init__(width, height, title)
21
22 arcade.set_background_color(arcade.color.WHITE)
23 self.text_angle = 0
24 self.time_elapsed = 0.0
25
26 def on_update(self, delta_time):
27 self.text_angle += 1
28 self.time_elapsed += delta_time
29
30 def on_draw(self):
31 """
32 Render the screen.
33 """
34
35 # This command should happen before we start drawing. It will clear
36 # the screen to the background color, and erase what we drew last frame.
37 arcade.start_render()
38
39 # start_x and start_y make the start point for the text. We draw a dot to make it easy too see
40 # the text in relation to its start x and y.
41 start_x = 50
42 start_y = 450
43 arcade.draw_point(start_x, start_y, arcade.color.BLUE, 5)
44 arcade.draw_text("Simple line of text in 12 point", start_x, start_y, arcade.color.BLACK, 12)
45
46 start_x = 50
47 start_y = 150
48 arcade.draw_point(start_x, start_y, arcade.color.BLUE, 5)
49 arcade.draw_text("Garamond Text", start_x, start_y, arcade.color.BLACK, 15, font_name='GARA')
50
51 start_x = 50
52 start_y = 400
53 arcade.draw_point(start_x, start_y, arcade.color.BLUE, 5)
54 arcade.draw_text("Text anchored 'top' and 'left'.",
55 start_x, start_y, arcade.color.BLACK, 12, anchor_x="left", anchor_y="top")
56
57 start_y = 350
58 arcade.draw_point(start_x, start_y, arcade.color.BLUE, 5)
59 arcade.draw_text("14 point multi\nline\ntext",
60 start_x, start_y, arcade.color.BLACK, 14, anchor_y="top")
61
62 start_y = 450
63 start_x = 300
64 width = 200
65 height = 20
66 arcade.draw_point(start_x, start_y, arcade.color.BLUE, 5)
67 arcade.draw_lrtb_rectangle_outline(start_x, start_x + width,
68 start_y + height, start_y,
69 arcade.color.BLUE, 1)
70 arcade.draw_text("Centered Text.",
71 start_x, start_y, arcade.color.BLACK, 14, width=200, align="center")
72
73 start_y = 250
74 start_x = 300
75 arcade.draw_point(start_x, start_y, arcade.color.BLUE, 5)
76 arcade.draw_text("Text centered on\na point",
77 start_x, start_y, arcade.color.BLACK, 14, width=200, align="center",
78 anchor_x="center", anchor_y="center")
79
80 start_y = 150
81 start_x = 300
82 arcade.draw_point(start_x, start_y, arcade.color.BLUE, 5)
83 arcade.draw_text("Text rotated on\na point", start_x, start_y,
84 arcade.color.BLACK, 14, width=200, align="center", anchor_x="center",
85 anchor_y="center", rotation=self.text_angle)
86
87 start_y = 150
88 start_x = 20
89 arcade.draw_point(start_x, start_y, arcade.color.BLUE, 5)
90 arcade.draw_text("Sideways text", start_x, start_y,
91 arcade.color.BLACK, 14, width=200, align="center",
92 anchor_x="center", anchor_y="center", rotation=90.0)
93
94 start_y = 20
95 start_x = 50
96 arcade.draw_point(start_x, start_y, arcade.color.BLUE, 5)
97 arcade.draw_text(f"Time elapsed: {self.time_elapsed:7.1f}",
98 start_x, start_y, arcade.color.BLACK, 14)
99
100
101def main():
102 MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
103 arcade.run()
104
105
106if __name__ == "__main__":
107 main()