Slow but Easy Text Drawing#
This example shows how to draw text. While it is simple to draw using the techniques shown here, it is also slow. Using “text objects” can result in significant performance improvements. See Fast Text Drawing.
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"""
7from __future__ import annotations
8
9import arcade
10
11SCREEN_WIDTH = 1200
12SCREEN_HEIGHT = 800
13SCREEN_TITLE = "Drawing Text Example"
14DEFAULT_LINE_HEIGHT = 45
15DEFAULT_FONT_SIZE = 20
16
17
18class MyGame(arcade.Window):
19 """
20 Main application class.
21 """
22
23 def __init__(self, width, height, title):
24 super().__init__(width, height, title)
25
26 self.background_color = arcade.color.BEIGE
27 self.text_angle = 0
28 self.time_elapsed = 0.0
29
30 def on_update(self, delta_time):
31 self.text_angle += 1
32 self.time_elapsed += delta_time
33
34 def on_draw(self):
35 """
36 Render the screen.
37 """
38
39 # This command should happen before we start drawing. It will clear
40 # the screen to the background color, and erase what we drew last frame.
41 self.clear()
42
43 # Add the screen title
44 start_x = 0
45 start_y = SCREEN_HEIGHT - DEFAULT_LINE_HEIGHT * 1.5
46 arcade.draw_text("Text Drawing Examples",
47 start_x,
48 start_y,
49 arcade.color.BLACK,
50 DEFAULT_FONT_SIZE * 2,
51 width=SCREEN_WIDTH,
52 align="center")
53
54 # start_x and start_y make the start point for the text. We draw a dot to make it
55 # easy to see the text in relation to its start x and y.
56 start_x = 10
57 start_y = SCREEN_HEIGHT - DEFAULT_LINE_HEIGHT * 3
58 arcade.draw_text("Fonts:",
59 start_x,
60 start_y,
61 arcade.color.FRENCH_WINE,
62 DEFAULT_FONT_SIZE, bold=True)
63
64 # Move the y value down to create another line of text
65 start_y -= DEFAULT_LINE_HEIGHT
66 arcade.draw_text("Default Font (Arial)",
67 start_x,
68 start_y,
69 arcade.color.BLACK,
70 DEFAULT_FONT_SIZE)
71
72 # Show some built-in fonts
73 start_y -= DEFAULT_LINE_HEIGHT
74 arcade.draw_text("Kenney Blocks Font",
75 start_x,
76 start_y,
77 arcade.color.BLACK,
78 DEFAULT_FONT_SIZE,
79 font_name="Kenney Blocks")
80
81 start_y -= DEFAULT_LINE_HEIGHT
82 arcade.draw_text("Kenney Future Font",
83 start_x,
84 start_y,
85 arcade.color.BLACK,
86 DEFAULT_FONT_SIZE,
87 font_name="Kenney Future")
88
89 start_y -= DEFAULT_LINE_HEIGHT
90 arcade.draw_text("Kenney High Font",
91 start_x,
92 start_y,
93 arcade.color.BLACK,
94 DEFAULT_FONT_SIZE,
95 font_name="Kenney High")
96
97 start_y -= DEFAULT_LINE_HEIGHT
98 arcade.draw_text("Kenney High Square Font",
99 start_x,
100 start_y,
101 arcade.color.BLACK,
102 DEFAULT_FONT_SIZE,
103 font_name="Kenney High Square")
104
105 start_y -= DEFAULT_LINE_HEIGHT
106 arcade.draw_text("Kenney Mini Square Font",
107 start_x, start_y,
108 arcade.color.BLACK,
109 DEFAULT_FONT_SIZE,
110 font_name="Kenney Mini Square")
111
112 start_y -= DEFAULT_LINE_HEIGHT
113 arcade.draw_text("Kenney Pixel Font",
114 start_x, start_y,
115 arcade.color.BLACK,
116 DEFAULT_FONT_SIZE,
117 font_name="Kenney Pixel")
118
119 start_y -= DEFAULT_LINE_HEIGHT
120 arcade.draw_text("Kenney Pixel Square Font",
121 start_x, start_y,
122 arcade.color.BLACK,
123 DEFAULT_FONT_SIZE,
124 font_name="Kenney Pixel Square")
125
126 start_y -= DEFAULT_LINE_HEIGHT
127 arcade.draw_text("Kenney Rocket Font",
128 start_x, start_y,
129 arcade.color.BLACK,
130 DEFAULT_FONT_SIZE,
131 font_name="Kenney Rocket")
132
133 start_y -= DEFAULT_LINE_HEIGHT
134 arcade.draw_text("Kenney Rocket Square Font",
135 start_x, start_y,
136 arcade.color.BLACK,
137 DEFAULT_FONT_SIZE,
138 font_name="Kenney Rocket Square")
139
140 start_y -= DEFAULT_LINE_HEIGHT
141 # When trying to use system fonts, it can be risky to specify
142 # only a single font because someone else's computer might not
143 # have it installed. This is especially true if they run a
144 # different operating system. For example, if you are on Windows
145 # and a friend has a mac or Linux, they might not have the same
146 # fonts. Your game could look different or broken on their computer.
147 # One way around that is to provide multiple options for draw_text
148 # to try. It will use the first one it finds, and use Arial as a
149 # default if it can't find any of them.
150 # In the example below, draw_text is given a tuple of names for very
151 # similar fonts, each of which is common on a different major
152 # operating systems.
153 arcade.draw_text("Times New Roman (Or closest match on system)",
154 start_x, start_y,
155 arcade.color.BLACK,
156 DEFAULT_FONT_SIZE,
157 font_name=(
158 "Times New Roman", # Comes with Windows
159 "Times", # MacOS may sometimes have this variant
160 "Liberation Serif" # Common on Linux systems
161 ))
162
163 start_y -= DEFAULT_LINE_HEIGHT
164 arcade.draw_text("Multi-Line\ntext using\n\\n characters.",
165 start_x, start_y,
166 arcade.color.BLACK,
167 DEFAULT_FONT_SIZE / 2,
168 multiline=True,
169 width=300)
170
171 start_y -= DEFAULT_LINE_HEIGHT * 1.5
172 arcade.draw_text("Wrapping really long text automatically to a new line. "
173 "The quick brown fox jumped over the lazy dogs.",
174 start_x,
175 start_y,
176 arcade.color.BLACK,
177 DEFAULT_FONT_SIZE / 2,
178 multiline=True,
179 width=300)
180
181 # --- Column 2 ---
182 start_x = 750
183 start_y = SCREEN_HEIGHT - DEFAULT_LINE_HEIGHT * 3
184 arcade.draw_text("Text Positioning:",
185 start_x,
186 start_y,
187 arcade.color.FRENCH_WINE,
188 DEFAULT_FONT_SIZE,
189 bold=True)
190
191 # start_x and start_y make the start point for the text.
192 # We draw a dot to make it easy too see the text in relation to
193 # its start x and y.
194 start_y -= DEFAULT_LINE_HEIGHT
195 arcade.draw_point(start_x, start_y, arcade.color.BARN_RED, 5)
196 arcade.draw_text("Default of 'baseline' and 'Left'",
197 start_x,
198 start_y,
199 arcade.color.BLACK,
200 DEFAULT_FONT_SIZE)
201
202 start_y -= DEFAULT_LINE_HEIGHT
203 arcade.draw_point(start_x, start_y, arcade.color.BARN_RED, 5)
204 arcade.draw_text("'bottom' and 'left'",
205 start_x,
206 start_y,
207 arcade.color.BLACK,
208 DEFAULT_FONT_SIZE,
209 anchor_x="left",
210 anchor_y="bottom")
211
212 start_y -= DEFAULT_LINE_HEIGHT
213 arcade.draw_point(start_x, start_y, arcade.color.BARN_RED, 5)
214 arcade.draw_text("'top' and 'left'",
215 start_x, start_y,
216 arcade.color.BLACK,
217 DEFAULT_FONT_SIZE,
218 anchor_x="left",
219 anchor_y="top")
220
221 start_y -= DEFAULT_LINE_HEIGHT * 2
222 arcade.draw_point(start_x, start_y, arcade.color.BARN_RED, 5)
223 arcade.draw_text("'baseline' and 'center'",
224 start_x, start_y,
225 arcade.color.BLACK,
226 DEFAULT_FONT_SIZE,
227 anchor_x="center",
228 anchor_y="baseline")
229
230 start_y -= DEFAULT_LINE_HEIGHT
231 arcade.draw_point(start_x, start_y, arcade.color.BARN_RED, 5)
232 arcade.draw_text("'baseline' and 'right'",
233 start_x,
234 start_y,
235 arcade.color.BLACK,
236 DEFAULT_FONT_SIZE,
237 anchor_x="right",
238 anchor_y="baseline")
239
240 start_y -= DEFAULT_LINE_HEIGHT
241 arcade.draw_point(start_x, start_y, arcade.color.BARN_RED, 5)
242 arcade.draw_text("'center' and 'center'",
243 start_x,
244 start_y,
245 arcade.color.BLACK,
246 DEFAULT_FONT_SIZE,
247 anchor_x="center",
248 anchor_y="center")
249
250 start_y -= DEFAULT_LINE_HEIGHT * 4
251 # start_x = 0
252 # start_y = 0
253 arcade.draw_point(start_x, start_y, arcade.color.BARN_RED, 5)
254 arcade.draw_text("Rotating Text",
255 start_x, start_y,
256 arcade.color.BLACK,
257 DEFAULT_FONT_SIZE,
258 anchor_x="center",
259 anchor_y="center",
260 rotation=self.text_angle)
261
262
263def main():
264 MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
265 arcade.run()
266
267
268if __name__ == "__main__":
269 main()