Compound Shapes

Goals

  • Create compund shapes.

  • Learn how to group compound shapes together using variables.

Starter code

Pygame Template

Video Tutorial

What you need to do

  1. Create an image of a cloud composed of three or more circles/ellipses and group them together into a compound shape. Ensure the whole group moves when you change the position variables.

  2. Take the face you drew in Drawing Shapes and group all the sub-shapes together so you can move the entire face by changing the x and y variables.

  3. Advanced (Optional): Create a function to draw the face and another to draw the clouds.

Final Code

# Compound Shapes

"""
- Draw a tree (rectangle and circle)
- Abstract the position into x and y variables
- Make group position relative to the position variables
"""

import pygame
from pygame.locals import K_ESCAPE, KEYDOWN, QUIT, MOUSEBUTTONDOWN

pygame.init()

WIDTH = 640
HEIGHT = 480
SIZE = (WIDTH, HEIGHT)

screen = pygame.display.set_mode(SIZE)
clock = pygame.time.Clock()

# ---------------------------
# Initialize global variables

EVERGREEN = (5, 71, 42)
FOREST_GREEN = (34, 139, 34)
DARK_GREEN = (0, 100, 0)

tree_x = 100
tree_y = 100

bush_x = 244
bush_y = 208
# ---------------------------

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
        elif event.type == MOUSEBUTTONDOWN:
            print(event.pos)

    # GAME STATE UPDATES
    # All game math and comparisons happen here

    # DRAWING
    screen.fill((255, 255, 255))  # always the first drawing command

    pygame.draw.rect(screen, EVERGREEN, (tree_x, tree_y, 50, 150))  # trunk
    pygame.draw.circle(screen, FOREST_GREEN, (tree_x + 25, tree_y), 50)  # leaves

    # Bush
    pygame.draw.circle(screen, DARK_GREEN, (bush_x, bush_y), 40)
    pygame.draw.circle(screen, DARK_GREEN, (bush_x + 26, bush_y), 25)
    pygame.draw.circle(screen, DARK_GREEN, (bush_x - 25, bush_y - 16), 25)


    # Must be the last two lines
    # of the game loop
    pygame.display.flip()
    clock.tick(30)
    #---------------------------


pygame.quit()