Drawing Primitives#

This example shows how to use the drawing commands. It is a good place to start for new programmers because they do not need to know how to define functions or classes before drawing images.

See Happy Face for an example of drawing primitives in action. To keep things simple, this example does not have many of the things commonly shown in the Starting Template Using Window Class.

Note

The method of drawing shown here is simple, but it is very slow. Modern graphics typically draw in a ‘batch’, a technique not shown in this example.

To look up all the available commands search for the Drawing Primitives in the API Index.

Screenshot of drawing with functions example program
drawing_primitives.py#
  1"""
  2Example "Arcade" library code.
  3
  4This example shows the drawing primitives and how they are used.
  5It does not assume the programmer knows how to define functions or classes
  6yet.
  7
  8API documentation for the draw commands can be found here:
  9https://api.arcade.academy/en/latest/quick_index.html
 10
 11A video explaining this example can be found here:
 12https://vimeo.com/167158158
 13
 14If Python and Arcade are installed, this example can be run from the command line with:
 15python -m arcade.examples.drawing_primitives
 16"""
 17
 18from __future__ import annotations
 19
 20# Import the Arcade library. If this fails, then try following the instructions
 21# for how to install arcade:
 22# https://api.arcade.academy/en/latest/install/index.html
 23import arcade
 24
 25# Open the window. Set the window title and dimensions (width and height)
 26arcade.open_window(600, 600, "Drawing Primitives Example")
 27
 28# Set the background color to white
 29# For a list of named colors see
 30# https://api.arcade.academy/en/latest/arcade.color.html
 31# Colors can also be specified in (red, green, blue) format and
 32# (red, green, blue, alpha) format.
 33arcade.set_background_color(arcade.color.WHITE)
 34
 35# Start the render process. This must be done before any drawing commands.
 36arcade.start_render()
 37
 38# Draw a grid
 39# Draw vertical lines every 120 pixels
 40for x in range(0, 601, 120):
 41    arcade.draw_line(x, 0, x, 600, arcade.color.BLACK, 2)
 42
 43# Draw horizontal lines every 200 pixels
 44for y in range(0, 601, 200):
 45    arcade.draw_line(0, y, 800, y, arcade.color.BLACK, 2)
 46
 47# Draw a point
 48arcade.draw_text("draw_point", 3, 405, arcade.color.BLACK, 12)
 49arcade.draw_point(60, 495, arcade.color.RED, 10)
 50
 51# Draw a set of points
 52arcade.draw_text("draw_points", 123, 405, arcade.color.BLACK, 12)
 53point_list = ((165, 495),
 54              (165, 480),
 55              (165, 465),
 56              (195, 495),
 57              (195, 480),
 58              (195, 465))
 59arcade.draw_points(point_list, arcade.color.ZAFFRE, 10)
 60
 61# Draw a line
 62arcade.draw_text("draw_line", 243, 405, arcade.color.BLACK, 12)
 63arcade.draw_line(270, 495, 300, 450, arcade.color.WOOD_BROWN, 3)
 64
 65# Draw a set of lines
 66arcade.draw_text("draw_lines", 363, 405, arcade.color.BLACK, 12)
 67point_list = ((390, 450),
 68              (450, 450),
 69              (390, 480),
 70              (450, 480),
 71              (390, 510),
 72              (450, 510)
 73              )
 74arcade.draw_lines(point_list, arcade.color.BLUE, 3)
 75
 76# Draw a line strip
 77arcade.draw_text("draw_line_strip", 483, 405, arcade.color.BLACK, 12)
 78point_list = ((510, 450),
 79              (570, 450),
 80              (510, 480),
 81              (570, 480),
 82              (510, 510),
 83              (570, 510)
 84              )
 85arcade.draw_line_strip(point_list, arcade.color.TROPICAL_RAIN_FOREST, 3)
 86
 87# Draw a polygon
 88arcade.draw_text("draw_polygon_outline", 3, 207, arcade.color.BLACK, 9)
 89point_list = ((30, 240),
 90              (45, 240),
 91              (60, 255),
 92              (60, 285),
 93              (45, 300),
 94              (30, 300))
 95arcade.draw_polygon_outline(point_list, arcade.color.SPANISH_VIOLET, 3)
 96
 97# Draw a filled in polygon
 98arcade.draw_text("draw_polygon_filled", 123, 207, arcade.color.BLACK, 9)
 99point_list = ((150, 240),
100              (165, 240),
101              (180, 255),
102              (180, 285),
103              (165, 300),
104              (150, 300))
105arcade.draw_polygon_filled(point_list, arcade.color.SPANISH_VIOLET)
106
107# Draw an outline of a circle
108arcade.draw_text("draw_circle_outline", 243, 207, arcade.color.BLACK, 10)
109arcade.draw_circle_outline(300, 285, 18, arcade.color.WISTERIA, 3)
110
111# Draw a filled in circle
112arcade.draw_text("draw_circle_filled", 363, 207, arcade.color.BLACK, 10)
113arcade.draw_circle_filled(420, 285, 18, arcade.color.GREEN)
114
115# Draw an ellipse outline, and another one rotated
116arcade.draw_text("draw_ellipse_outline", 483, 207, arcade.color.BLACK, 10)
117arcade.draw_ellipse_outline(540, 273, 15, 36, arcade.color.AMBER, 3)
118arcade.draw_ellipse_outline(540, 336, 15, 36,
119                            arcade.color.BLACK_BEAN, 3, 45)
120
121# Draw a filled ellipse, and another one rotated
122arcade.draw_text("draw_ellipse_filled", 3, 3, arcade.color.BLACK, 10)
123arcade.draw_ellipse_filled(60, 81, 15, 36, arcade.color.AMBER)
124arcade.draw_ellipse_filled(60, 144, 15, 36,
125                           arcade.color.BLACK_BEAN, 45)
126
127# Draw an arc, and another one rotated
128arcade.draw_text("draw_arc/filled_arc", 123, 3, arcade.color.BLACK, 10)
129arcade.draw_arc_outline(150, 81, 15, 36,
130                        arcade.color.BRIGHT_MAROON, 90, 360)
131arcade.draw_arc_filled(150, 144, 15, 36,
132                       arcade.color.BOTTLE_GREEN, 90, 360, 45)
133
134# Draw an rectangle outline
135arcade.draw_text("draw_rect", 243, 3, arcade.color.BLACK, 10)
136arcade.draw_rectangle_outline(295, 100, 45, 65,
137                              arcade.color.BRITISH_RACING_GREEN)
138arcade.draw_rectangle_outline(295, 160, 20, 45,
139                              arcade.color.BRITISH_RACING_GREEN, 3, 45)
140
141# Draw a filled in rectangle
142arcade.draw_text("draw_filled_rect", 363, 3, arcade.color.BLACK, 10)
143arcade.draw_rectangle_filled(420, 100, 45, 65, arcade.color.BLUSH)
144arcade.draw_rectangle_filled(420, 160, 20, 40, arcade.color.BLUSH, 45)
145
146# Load and draw an image to the screen
147# Image from kenney.nl asset pack #1
148arcade.draw_text("draw_bitmap", 483, 3, arcade.color.BLACK, 12)
149texture = arcade.load_texture(":resources:images/space_shooter/playerShip1_orange.png")
150scale = .6
151arcade.draw_scaled_texture_rectangle(540, 120, texture, scale, 0)
152arcade.draw_scaled_texture_rectangle(540, 60, texture, scale, 45)
153
154# Finish the render.
155# Nothing will be drawn without this.
156# Must happen after all draw commands
157arcade.finish_render()
158
159# Keep the window up until someone closes it.
160arcade.run()