Slow but Easy Text Drawing

Screenshot of drawing text

This example shows an easy but very slow way to draw text.

Its main benefit is ease of use. More complicated approaches with arcade.Text objects can run thousands of times faster.

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