Module 7 - Section 0 - Snow fall and snowmen again
Site: | ΕΛ/ΛΑΚ Moodle |
Course: | Python Lab |
Book: | Module 7 - Section 0 - Snow fall and snowmen again |
Printed by: | Guest user |
Date: | Thursday, 21 November 2024, 5:14 PM |
Description
7.0.1 - Drawing functions
In the previous module we have created a drawing function to draw a snowman. In the drawing function we have used variables to draw the snow man in different position inside the screen.
The function was written outside the pygame main loop:
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)
7.0.2 - More than one snowmen
To draw the snowman in multiple places we just have to repeat the draw_snowman function.
For this purpose we can use a for loop or nested loops for the number of snowmen that we want to be drawn inside the screen.
The loop should be in the Drawing Area inside the main loop.
# --- Drawing code should go here
for i in range(9):
for j in range(3):
draw_snowman(screen, 70*i, 160*j)
7.0.3 - Animate shapes using and updating a list
To create multiple instances of a shape, or item, we can use a list to store the characteristics of the items. We used this technique to create snow flakes and then update the position of coordinate Y to look like a snowfall. Also after dissapearing from the screen the snowflake's position updated in a new position above the screen and start to fall again.
import random
snow_list = []
for i in range(50):
x = random.randrange(10,690)
y = random.randrange(10,490)
snow_list.append([x,y])
print(snow_list)