Drawing Text¶
drawing_text.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 | """
Example showing how to draw text to the screen.
If Python and Arcade are installed, this example can be run from the command line with:
python -m arcade.examples.drawing_text
"""
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)
arcade.set_background_color(arcade.color.BEIGE)
self.text_angle = 0
self.time_elapsed = 0.0
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.
arcade.start_render()
# Add the screen title
start_x = 0
start_y = SCREEN_HEIGHT - DEFAULT_LINE_HEIGHT * 1.5
arcade.draw_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
arcade.draw_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
arcade.draw_text("Default Font (Arial)", start_x, start_y, arcade.color.BLACK, DEFAULT_FONT_SIZE)
# Show some built-in fonts
start_y -= DEFAULT_LINE_HEIGHT
arcade.draw_text("Kenney Blocks Font",
start_x, start_y, arcade.color.BLACK, DEFAULT_FONT_SIZE, font_name="Kenney Blocks")
start_y -= DEFAULT_LINE_HEIGHT
arcade.draw_text("Kenney Future Font",
start_x, start_y, arcade.color.BLACK, DEFAULT_FONT_SIZE, font_name="Kenney Future")
start_y -= DEFAULT_LINE_HEIGHT
arcade.draw_text("Kenney High Font",
start_x, start_y, arcade.color.BLACK, DEFAULT_FONT_SIZE, font_name="Kenney High")
start_y -= DEFAULT_LINE_HEIGHT
arcade.draw_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
arcade.draw_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
arcade.draw_text("Kenney Pixel Font",
start_x, start_y, arcade.color.BLACK, DEFAULT_FONT_SIZE, font_name="Kenney Pixel")
start_y -= DEFAULT_LINE_HEIGHT
arcade.draw_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
arcade.draw_text("Kenney Rocket Font",
start_x, start_y, arcade.color.BLACK, DEFAULT_FONT_SIZE, font_name="Kenney Rocket")
start_y -= DEFAULT_LINE_HEIGHT
arcade.draw_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
arcade.draw_text("Garamond Font",
start_x, start_y, arcade.color.BLACK, DEFAULT_FONT_SIZE, font_name="Garamond")
start_y -= DEFAULT_LINE_HEIGHT
arcade.draw_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
arcade.draw_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
arcade.draw_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
arcade.draw_point(start_x, start_y, arcade.color.BARN_RED, 5)
arcade.draw_text("Default of 'baseline' and 'Left'",
start_x, start_y,
arcade.color.BLACK, DEFAULT_FONT_SIZE)
start_y -= DEFAULT_LINE_HEIGHT
arcade.draw_point(start_x, start_y, arcade.color.BARN_RED, 5)
arcade.draw_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
arcade.draw_point(start_x, start_y, arcade.color.BARN_RED, 5)
arcade.draw_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
arcade.draw_point(start_x, start_y, arcade.color.BARN_RED, 5)
arcade.draw_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
arcade.draw_point(start_x, start_y, arcade.color.BARN_RED, 5)
arcade.draw_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
arcade.draw_point(start_x, start_y, arcade.color.BARN_RED, 5)
arcade.draw_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
arcade.draw_point(start_x, start_y, arcade.color.BARN_RED, 5)
arcade.draw_text("Rotating Text",
start_x, start_y,
arcade.color.BLACK, DEFAULT_FONT_SIZE, anchor_x="center", anchor_y="center",
rotation=self.text_angle)
def main():
MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
arcade.run()
if __name__ == "__main__":
main()
|