Drawing with Functions

This example shows how to define functions that draw objects. Such as draw_tree(x, y) and draw_bird(x, y).

For more information see:

Drawing with Functions from Paul Vincent Craven on Vimeo.

drawing_with_functions.py
 1"""
 2Example "Arcade" library code.
 3
 4This example shows how to use functions to draw a scene.
 5It does not assume that the programmer knows how to use classes yet.
 6
 7A video walk-through of this code is available at:
 8https://vimeo.com/167296062
 9
10If Python and Arcade are installed, this example can be run from the command line with:
11python -m arcade.examples.drawing_with_functions
12"""
13
14# Library imports
15import arcade
16
17# Constants - variables that do not change
18SCREEN_WIDTH = 600
19SCREEN_HEIGHT = 600
20SCREEN_TITLE = "Drawing With Functions Example"
21
22
23def draw_background():
24    """
25    This function draws the background. Specifically, the sky and ground.
26    """
27    # Draw the sky in the top two-thirds
28    arcade.draw_lrtb_rectangle_filled(0,
29                                      SCREEN_WIDTH,
30                                      SCREEN_HEIGHT,
31                                      SCREEN_HEIGHT * (1 / 3),
32                                      arcade.color.SKY_BLUE)
33
34    # Draw the ground in the bottom third
35    arcade.draw_lrtb_rectangle_filled(0,
36                                      SCREEN_WIDTH,
37                                      SCREEN_HEIGHT / 3,
38                                      0,
39                                      arcade.color.DARK_SPRING_GREEN)
40
41
42def draw_bird(x, y):
43    """
44    Draw a bird using a couple arcs.
45    """
46    arcade.draw_arc_outline(x, y, 20, 20, arcade.color.BLACK, 0, 90)
47    arcade.draw_arc_outline(x + 40, y, 20, 20, arcade.color.BLACK, 90, 180)
48
49
50def draw_pine_tree(x, y):
51    """
52    This function draws a pine tree at the specified location.
53    """
54    # Draw the triangle on top of the trunk
55    arcade.draw_triangle_filled(x + 40, y,
56                                x, y - 100,
57                                x + 80, y - 100,
58                                arcade.color.DARK_GREEN)
59
60    # Draw the trunk
61    arcade.draw_lrtb_rectangle_filled(x + 30, x + 50, y - 100, y - 140,
62                                      arcade.color.DARK_BROWN)
63
64
65def main():
66    """
67    This is the main program.
68    """
69
70    # Open the window
71    arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
72
73    # Start the render process. This must be done before any drawing commands.
74    arcade.start_render()
75
76    # Call our drawing functions.
77    draw_background()
78    draw_pine_tree(50, 250)
79    draw_pine_tree(350, 320)
80    draw_bird(70, 500)
81    draw_bird(470, 550)
82
83    # Finish the render.
84    # Nothing will be drawn without this.
85    # Must happen after all draw commands
86    arcade.finish_render()
87
88    # Keep the window up until someone closes it.
89    arcade.run()
90
91
92if __name__ == "__main__":
93    main()