Module 6 - Section 2 - Functions and Drawing

6.2.4 - Snowmen code analysed

Review the code line by line to understand the use of every command. You can download the code from here.


''' ELLAK - Python course Snowmen ''' import pygame # Define some colors BLACK = (0, 0, 0) WHITE = (255, 255, 255) GREEN = (0, 255, 0) RED = (255, 0, 0) ORANGE = (250, 147, 30) BLUE = (0, 0, 255) BROWN = (70, 25, 0) def draw_snowman(screen, x, y): # Draw a circle for the head pygame.draw.circle(screen, WHITE, [60 + x, 50 + y], 15) # Draw the middle snowman circle pygame.draw.circle(screen, WHITE, [60 + x, 80 + y], 20) # Draw the bottom snowman circle pygame.draw.circle(screen, WHITE, [60 + x, 120 + y], 30) # Draw the buttons pygame.draw.circle(screen, BLACK, [60 + x, 120 + y], 6) pygame.draw.circle(screen, RED, [60 + x, 120 + y], 4) pygame.draw.circle(screen, BLACK, [60 + x, 100 + y], 6) pygame.draw.circle(screen, RED, [60 + x, 100 + y], 4) pygame.draw.circle(screen, BLACK, [60 + x, 80 + y], 6) pygame.draw.circle(screen, RED, [60 + x, 80 + y], 4) #Draw the hat pygame.draw.rect(screen, BROWN, [45 + x, 8 + y, 30,30]) pygame.draw.rect(screen, BROWN, [30 + x, 34 + y, 60,5]) #Draw the eyes pygame.draw.circle(screen, BLACK, [54 + x, 44 + y], 3) pygame.draw.circle(screen, BLACK, [66 + x, 44 + y], 3) #Draw the mouth pygame.draw.arc(screen, RED, [50 + x, 40 + y, 20, 20] , 3.34, 6.08,3) #Draw the nose pygame.draw.polygon(screen, ORANGE, [[58 + x , 46 + y], [60 + x , 54 + y], [72 + x, 60 + y]], 0) pygame.init() # Set the width and height of the screen [width, height] size = (700, 500) screen = pygame.display.set_mode(size) pygame.display.set_caption("ELLAK - Snowmen") # Loop until the user clicks the close button. done = False # Used to manage how fast the screen updates clock = pygame.time.Clock() # -------- Main Program Loop ----------- while not done: # --- Main event loop for event in pygame.event.get(): if event.type == pygame.QUIT: done = True # --- Game logic should go here # --- Screen-clearing code goes here # Here, we clear the screen to white. Don't put other drawing commands # above this, or they will be erased with this command. # If you want a background image, replace this clear with blit'ing the # background image. screen.fill(BLACK) # --- Drawing code should go here # Snowman in upper left for i in range(9): for j in range(3): draw_snowman(screen, 70*i, 160*j) # --- Go ahead and update the screen with what we've drawn. pygame.display.flip() # --- Limit to 60 frames per second clock.tick(60) # Close the window and quit. pygame.quit()