Module 6 - Section 2 - Functions and Drawing
Site: | ΕΛ/ΛΑΚ Moodle |
Course: | Python Lab |
Book: | Module 6 - Section 2 - Functions and Drawing |
Printed by: | Guest user |
Date: | Saturday, 23 November 2024, 3:30 PM |
6.2.1 - One snowman
We have created an animation of snow but after so snow we haven't created a snowman.
Using the pygame draw commands, draw a snowman. To give you an idea this is our trial.
The commands that you may need are:
- pygame.draw.circle
- pygame.draw.rect
- pygame.draw.arc
- pygame.draw.polygon
6.2.2 - 27 Snowmen!
It is time to multiply our snowman. With one command inside some nested loops!
In the above image take a close look at the lines 77-79. Do you understand how all these snowmen were created?
What is the draw_snowman command?
What's inside the parenthesis?
6.2.3 - Snowman code revealed
draw_snowman is a function that creates one snowman at some position on the screen. (screen, x, y) are the parameters (arguments) of the function.
As you can see in the picture below, every draw command depends on this parameters. In this way if we change x and y then we can create a snowman in another position.
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()