Ενότητα 6 - Τμήμα 1 - Επαναλήψεις και κινούμενα σχέδια
Ενότητα 6 - Τμήμα 1 - Επαναλήψεις και κινούμενα σχέδια
- δημιουργούν τυχαία κινούμενα σχήματα
- ξαναγράφουν τον κώδικα χρησιμοποιώντας βρόχους
- χρησιμοποιούν εμφωλευμένους βρόχους για συνεχή κίνηση
- προβλέψουν και να διορθώσουν τον κώδικα έτσι ώστε τα αντικείμενα να επανεμφανίζονται σε τυχαίες θέσεις
6.1.5 - Many snowflakes
To draw all the snowflakes, we have to create a loop inside the drawing section.
Save the script as snow_flakes_50 inside your project's folder
If you haven't managed to create the script download it from here and save it inside your project's folder as snow_flakes_50
We will change the drawing command with these two commands (taking care of indentation):
for i in range(50):
pygame.draw.circle(screen, WHITE, snow_list[i] , 2)
This loop will run 50 times and everytime will draw a snowflake, the first, the second, the third and so on.
And what if we wanted to draw 100 snowflakes? What changes we have to do?
Can we change the code so we can make only one change and not two?
Do you remember the len function? Write the following command inside CLI area:
>>> len (snow_list)
Change the for loop inside the drawing are to this:
for i in range(len(snow_list)):
Now you can create as many snowflakes as you like changing only the for loop inside the initialization area.
If you haven't managed to write the script you can download it from here.
Can you change the script to ask the user how many snow flakes he wants to be drawn?