Drawing with Text Objects#

Screenshot of drawing with text objects

This example shows how to draw text using “text objects.” It is the fastest way to draw text. If you are looking for something simpler, see Drawing Text.

drawing_text_objects.py#
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
"""
Example showing how to draw text to the screen using Text objects.
This is much faster than using draw_text

If Python and Arcade are installed, this example can be run from the command line with:
python -m arcade.examples.drawing_text_objects
"""
import arcade

SCREEN_WIDTH = 1200
SCREEN_HEIGHT = 800
SCREEN_TITLE = "Drawing Text Example"
DEFAULT_LINE_HEIGHT = 45
DEFAULT_FONT_SIZE = 20


class MyGame(arcade.Window):
    """
    Main application class.
    """

    def __init__(self, width, height, title):
        super().__init__(width, height, title)

        self.background_color = arcade.color.BEIGE
        self.text_angle = 0
        self.time_elapsed = 0.0

        # Add the screen title
        start_x = 0
        start_y = SCREEN_HEIGHT - DEFAULT_LINE_HEIGHT * 1.5
        self.title = arcade.Text(
            "Text Drawing Examples",
            start_x,
            start_y,
            arcade.color.BLACK,
            DEFAULT_FONT_SIZE * 2,
            width=SCREEN_WIDTH,
            align="center",
        )

        # start_x and start_y make the start point for the text. We draw a dot to make it
        # easy too see the text in relation to its start x and y.
        start_x = 10
        start_y = SCREEN_HEIGHT - DEFAULT_LINE_HEIGHT * 3
        self.fonts = arcade.Text(
            "Fonts:",
            start_x,
            start_y,
            arcade.color.FRENCH_WINE,
            DEFAULT_FONT_SIZE, bold=True,
        )

        # Move the y value down to create another line of text
        start_y -= DEFAULT_LINE_HEIGHT
        self.font_default = arcade.Text(
            "Default Font (Arial)",
            start_x,
            start_y,
            arcade.color.BLACK,
            DEFAULT_FONT_SIZE    
        )

        # Show some built-in fonts
        start_y -= DEFAULT_LINE_HEIGHT
        self.font_kenney_blocks = arcade.Text(
            "Kenney Blocks Font",
            start_x,
            start_y,
            arcade.color.BLACK,
            DEFAULT_FONT_SIZE,
            font_name="Kenney Blocks",
        )

        start_y -= DEFAULT_LINE_HEIGHT
        self.font_kenney_future = arcade.Text(
            "Kenney Future Font",
            start_x,
            start_y,
            arcade.color.BLACK,
            DEFAULT_FONT_SIZE,
            font_name="Kenney Future",
        )

        start_y -= DEFAULT_LINE_HEIGHT
        self.font_kenney_high = arcade.Text(
            "Kenney High Font",
            start_x,
            start_y,
            arcade.color.BLACK,
            DEFAULT_FONT_SIZE,
            font_name="Kenney High"
        )

        start_y -= DEFAULT_LINE_HEIGHT
        self.font_kenney_high_square = arcade.Text(
            "Kenney High Square Font",
            start_x,
            start_y,
            arcade.color.BLACK,
            DEFAULT_FONT_SIZE,
            font_name="Kenney High Square",
        )

        start_y -= DEFAULT_LINE_HEIGHT
        self.font_kenney_mini_square = arcade.Text(
            "Kenney Mini Square Font",
            start_x, start_y,
            arcade.color.BLACK,
            DEFAULT_FONT_SIZE,
            font_name="Kenney Mini Square",   
        )

        start_y -= DEFAULT_LINE_HEIGHT
        self.font_kenney_pixel = arcade.Text(
            "Kenney Pixel Font",
            start_x, start_y,
            arcade.color.BLACK,
            DEFAULT_FONT_SIZE,
            font_name="Kenney Pixel",
        )

        start_y -= DEFAULT_LINE_HEIGHT
        self.font_kenney_pixel_square = arcade.Text(
            "Kenney Pixel Square Font",
            start_x, start_y,
            arcade.color.BLACK,
            DEFAULT_FONT_SIZE,
            font_name="Kenney Pixel Square",
        )

        start_y -= DEFAULT_LINE_HEIGHT
        self.font_kenney_rocket = arcade.Text(
            "Kenney Rocket Font",
            start_x, start_y,
            arcade.color.BLACK,
            DEFAULT_FONT_SIZE,
            font_name="Kenney Rocket",    
        )

        start_y -= DEFAULT_LINE_HEIGHT
        self.font_kenney_rocket_square = arcade.Text(
            "Kenney Rocket Square Font",
            start_x, start_y,
            arcade.color.BLACK,
            DEFAULT_FONT_SIZE,
            font_name="Kenney Rocket Square",
        )

        start_y -= DEFAULT_LINE_HEIGHT
        # When trying to use system fonts, it can be risky to specify
        # only a single font because someone else's computer might not
        # have it installed. This is especially true if they run a
        # different operating system. For example, if you are on Windows
        # and a friend has a mac or Linux, they might not have the same
        # fonts. Your game could look different or broken on their computer.
        # One way around that is to provide multiple options for draw_text
        # to try. It will use the first one it finds, and use Arial as a
        # default if it can't find any of them.
        # In the example below, draw_text is given a tuple of names for very
        # similar fonts, each of which is common on a different major
        # operating systems.
        self.font_times_new_roman = arcade.Text(
            "Times New Roman (Or closest match on system)",
            start_x, start_y,
            arcade.color.BLACK,
            DEFAULT_FONT_SIZE,
            font_name=(
                "Times New Roman",  # Comes with Windows
                "Times",  # MacOS may sometimes have this variant
                "Liberation Serif"  # Common on Linux systems
            )
        )

        start_y -= DEFAULT_LINE_HEIGHT
        self.multi_line_breaks = arcade.Text(
            "Multi-Line\ntext using\n\\n characters.",
            start_x, start_y,
            arcade.color.BLACK,
            DEFAULT_FONT_SIZE / 2,
            multiline=True,
            width=300,
        )

        start_y -= DEFAULT_LINE_HEIGHT * 1.5
        self.multiline_wrap = arcade.Text(
            "Wrapping really long text automatically to a new line. "
            "The quick brown fox jumped over the lazy dogs.",
            start_x,
            start_y,
            arcade.color.BLACK,
            DEFAULT_FONT_SIZE / 2,
            multiline=True,
            width=300
        )

        # --- Column 2 ---
        start_x = 750
        start_y = SCREEN_HEIGHT - DEFAULT_LINE_HEIGHT * 3
        self.text_positioning = arcade.Text(
            "Text Positioning:",
            start_x,
            start_y,
            arcade.color.FRENCH_WINE,
            DEFAULT_FONT_SIZE,
            bold=True,
        )

        # start_x and start_y make the start point for the text.
        # We draw a dot to make it easy too see the text in relation to
        # its start x and y.
        start_y -= DEFAULT_LINE_HEIGHT

        self.default_baseline_left = arcade.Text(
            "Default of 'baseline' and 'Left'",
            start_x,
            start_y,
            arcade.color.BLACK,
            DEFAULT_FONT_SIZE,
        )

        start_y -= DEFAULT_LINE_HEIGHT
        self.bottom_left = arcade.Text(
            "'bottom' and 'left'",
            start_x,
            start_y,
            arcade.color.BLACK,
            DEFAULT_FONT_SIZE,
            anchor_x="left",
            anchor_y="bottom",
        )

        start_y -= DEFAULT_LINE_HEIGHT
        self.top_left = arcade.Text(
            "'top' and 'left'",
            start_x, start_y,
            arcade.color.BLACK,
            DEFAULT_FONT_SIZE,
            anchor_x="left",
            anchor_y="top",            
        )

        start_y -= DEFAULT_LINE_HEIGHT * 2
        self.basline_center = arcade.Text(
            "'baseline' and 'center'",
            start_x, start_y,
            arcade.color.BLACK,
            DEFAULT_FONT_SIZE,
            anchor_x="center",
            anchor_y="baseline",
        )

        start_y -= DEFAULT_LINE_HEIGHT
        self.baseline_right = arcade.Text(
            "'baseline' and 'right'",
            start_x,
            start_y,
            arcade.color.BLACK,
            DEFAULT_FONT_SIZE,
            anchor_x="right",
            anchor_y="baseline",
        )

        start_y -= DEFAULT_LINE_HEIGHT
        self.center_center = arcade.Text(
            "'center' and 'center'",
            start_x,
            start_y,
            arcade.color.BLACK,
            DEFAULT_FONT_SIZE,
            anchor_x="center",
            anchor_y="center",
        )

        start_y -= DEFAULT_LINE_HEIGHT * 4
        # start_x = 0
        # start_y = 0
        self.rotating_text = arcade.Text(
            "Rotating Text",
            start_x, start_y,
            arcade.color.BLACK,
            DEFAULT_FONT_SIZE,
            anchor_x="center",
            anchor_y="center",
            rotation=self.text_angle
        )

    def on_update(self, delta_time):
        self.text_angle += 1
        self.time_elapsed += delta_time

    def on_draw(self):
        """
        Render the screen.
        """
        # This command should happen before we start drawing. It will clear
        # the screen to the background color, and erase what we drew last frame.
        self.clear()

        # Draw all the text objects
        # Column 1
        self.title.draw()
        self.fonts.draw()
        self.font_default.draw()
        self.font_kenney_blocks.draw()
        self.font_kenney_future.draw()
        self.font_kenney_high.draw()
        self.font_kenney_high_square.draw()
        self.font_kenney_mini_square.draw()
        self.font_kenney_pixel.draw()
        self.font_kenney_pixel_square.draw()
        self.font_kenney_rocket.draw()
        self.font_kenney_rocket_square.draw()
        self.font_times_new_roman.draw()
        self.multi_line_breaks.draw()
        self.multiline_wrap.draw()

        # Column 2
        self.text_positioning.draw()

        arcade.draw_point(
            self.default_baseline_left.x,
            self.default_baseline_left.y,
            arcade.color.BARN_RED,
            5,
        )
        self.default_baseline_left.draw()

        arcade.draw_point(
            self.bottom_left.x,
            self.bottom_left.y,
            arcade.color.BARN_RED,
            5,    
        )
        self.bottom_left.draw()

        arcade.draw_point(
            self.top_left.x,
            self.top_left.y,
            arcade.color.BARN_RED,
            5,
        )
        self.top_left.draw()

        arcade.draw_point(
            self.basline_center.x,
            self.basline_center.y,
            arcade.color.BARN_RED,
            5
        )
        self.basline_center.draw()

        arcade.draw_point(
            self.baseline_right.x,
            self.baseline_right.y,
            arcade.color.BARN_RED,
            5,
        )
        self.baseline_right.draw()

        arcade.draw_point(
            self.center_center.x,
            self.center_center.y,
            arcade.color.BARN_RED,
            5,
        )
        self.center_center.draw()

        arcade.draw_point(
            self.rotating_text.x,
            self.rotating_text.y,
            arcade.color.BARN_RED,
            5,
        )
        self.rotating_text.rotation = self.text_angle
        self.rotating_text.draw()


def main():
    MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
    arcade.run()


if __name__ == "__main__":
    main()