Draw Tree Function ================== Goals ----- - Draw a tree - Draw two trees - Reference every position to single ``(x, y)`` point - Define the ``draw_tree`` function - Vary the ``y`` position. Need to create a list *outside* the game loop. Starter code ------------ - :doc:`pygame-template` What you need to do ------------------- - Make a blue background - Move the trees down and draw a single cloud (composed of multiple circles) - Extract an x and y coordinate for the cloud and have every circle reference that point. - Define a draw_cloud function that takes an x and y value. - Draw multiple clouds in the sky - Create a list for the cloud locations. And use this list to draw the clouds (with a for loop). - In the game updates section, loop through the cloud locations and update only the x component (+= 3). Also in this loop, to prevent the clouds from traveling too far off screen, reset the x position if it goes too far. Final Code ---------- .. code-block:: python :linenos: :emphasize-lines: 7-9, 53 import random import pygame from pygame.locals import K_ESCAPE, KEYDOWN, QUIT def draw_tree(x: int, y: int): pygame.draw.rect(screen, (150,75,0), (x, y, 25, 150)) # trunk pygame.draw.circle(screen, (0, 175, 0), (x + 12, y - 50), 75) # leaves pygame.init() WIDTH = 640 HEIGHT = 480 SIZE = (WIDTH, HEIGHT) screen = pygame.display.set_mode(SIZE) clock = pygame.time.Clock() # --------------------------- # Initialize global variables tree_locations = [] for x in range(50, WIDTH, 50): y = 200 + random.randrange(-20, 20) location = [x, y] tree_locations.append(location) # --------------------------- running = True while running: # EVENT HANDLING for event in pygame.event.get(): if event.type == KEYDOWN: if event.key == K_ESCAPE: running = False elif event.type == QUIT: running = False # GAME STATE UPDATES # DRAWING screen.fill((255, 255, 255)) # always the first drawing command for location in tree_locations: x = location[0] y = location[1] draw_tree(x, y) # Must be the last two lines # of the game loop pygame.display.flip() clock.tick(30) #--------------------------- pygame.quit()