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.

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