Drawing with Loops

Screen shot of drawing with loops example program
drawing_with_loops.py
  1"""
  2Example "Arcade" library code.
  3
  4This example shows how to use functions and loops to draw a scene.
  5It does not assume that the programmer knows how to use classes yet.
  6
  7If Python and Arcade are installed, this example can be run from the command line with:
  8python -m arcade.examples.drawing_with_loops
  9"""
 10
 11# Library imports
 12import arcade
 13import random
 14
 15SCREEN_WIDTH = 800
 16SCREEN_HEIGHT = 600
 17SCREEN_TITLE = "Drawing With Loops Example"
 18
 19
 20def draw_background():
 21    """
 22    This function draws the background. Specifically, the sky and ground.
 23    """
 24    # Draw the sky in the top two-thirds
 25    arcade.draw_rectangle_filled(SCREEN_WIDTH / 2, SCREEN_HEIGHT * 2 / 3,
 26                                 SCREEN_WIDTH - 1, SCREEN_HEIGHT * 2 / 3,
 27                                 arcade.color.SKY_BLUE)
 28
 29    # Draw the ground in the bottom third
 30    arcade.draw_rectangle_filled(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 6,
 31                                 SCREEN_WIDTH - 1, SCREEN_HEIGHT / 3,
 32                                 arcade.color.DARK_SPRING_GREEN)
 33
 34
 35def draw_bird(x, y):
 36    """
 37    Draw a bird using a couple arcs.
 38    """
 39    arcade.draw_arc_outline(x, y, 20, 20, arcade.color.BLACK, 0, 90)
 40    arcade.draw_arc_outline(x + 40, y, 20, 20, arcade.color.BLACK, 90, 180)
 41
 42
 43def draw_pine_tree(center_x, center_y):
 44    """
 45    This function draws a pine tree at the specified location.
 46
 47    Args:
 48      :center_x: x position of the tree center.
 49      :center_y: y position of the tree trunk center.
 50    """
 51    # Draw the trunkcenter_x
 52    arcade.draw_rectangle_filled(center_x, center_y, 20, 40,
 53                                 arcade.color.DARK_BROWN)
 54
 55    tree_bottom_y = center_y + 20
 56
 57    # Draw the triangle on top of the trunk
 58    point_list = ((center_x - 40, tree_bottom_y),
 59                  (center_x, tree_bottom_y + 100),
 60                  (center_x + 40, tree_bottom_y))
 61
 62    arcade.draw_polygon_filled(point_list, arcade.color.DARK_GREEN)
 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
 79    # Loop to draw ten birds in random locations.
 80    for bird_count in range(10):
 81        # Any random x from 0 to the width of the screen
 82        x = random.randrange(0, SCREEN_WIDTH)
 83
 84        # Any random y from in the top 2/3 of the screen.
 85        # No birds on the ground.
 86        y = random.randrange(SCREEN_HEIGHT / 3, SCREEN_HEIGHT - 20)
 87
 88        # Draw the bird.
 89        draw_bird(x, y)
 90
 91    # Draw the top row of trees
 92    for x in range(45, SCREEN_WIDTH, 90):
 93        draw_pine_tree(x, SCREEN_HEIGHT / 3)
 94
 95    # Draw the bottom row of trees
 96    for x in range(65, SCREEN_WIDTH, 90):
 97        draw_pine_tree(x, (SCREEN_HEIGHT / 3) - 120)
 98
 99    # Finish the render.
100    # Nothing will be drawn without this.
101    # Must happen after all draw commands
102    arcade.finish_render()
103
104    # Keep the window up until someone closes it.
105    arcade.run()
106
107
108if __name__ == "__main__":
109    main()