Snowfall display using Pygame in Python

Not everybody must have witnessed Snowfall personally but wait a minute, What if you can see the snowfall right on your screen by just a few lines of creativity and Programming.

 Before starting the topic, it is highly recommended revising the basics of Pygame. 

Steps for snowfall creation

1. Importing modules

First, we need to import the Pygame module by using the command.

import pygame
 



Also, along with Pygame, we will also need random module. Python has a built-in module that you can use to make random numbers just by importing random module.

import random
 

2. Initialize the game engine 

It simply means choose the colors you want to use. In programming World, Whatever you can think you can make. At the end of the article , you will find green snowfall on the white background.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# initialize
pygame.init()
  
# choosen colours will be used
# to display the output
WHITE = [255, 255, 255]
GREEN = [0, 255, 0]

chevron_right


3.  Specify the size of the screen 

 It can be a new number depending upon the resolution of your system.

Python3



filter_none

edit
close

play_arrow

link
brightness_4
code

# specify the size
  
SIZE = [400, 400]
screen = pygame.display.set_mode(SIZE)

chevron_right


4. Assign a name to your snowfall window screen

The name given can be seen on the left corner of the output window.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# caption for output window
  
pygame.display.set_caption("Programming World of GFG")

chevron_right


5. Create an empty array for your snowfall 

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

snowFall = []

chevron_right


6. Looping to get snowfall positions

 Make a loop and run to 50 times and add a snowfall in a random x,y position using random Module.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

for i in range(50):
    x = random.randrange(0, 400)
    y = random.randrange(0, 400)
    snowFall.append([x, y])

chevron_right


7. Track time

Create an object to help track time



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# object to track time
  
clock = pygame.time.Clock()

chevron_right


8. Set criteria for snowfall occurrence

 Snowfall should occur until the user presses the close button and for this inside while loop, use a for loop.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# loop till the close button is pressed
done = False
  
while not done:
  
  # User did something
    for event in pygame.event.get():
  
       # If user clicked close
        if event.type == pygame.QUIT:
  
          # Flag that we are done so
          # we exit this loop
            done = True

chevron_right


9. Set the screen background :

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

screen.fill(WHITE)

chevron_right


10. Process the snowfall

Now use a for loop to process each Snowfall in the list :

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

for i in range(len(snowFall)):

chevron_right


11. Draw the snowfall 



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

pygame.draw.circle(screen, GREEN, snowFall[i], 2)

chevron_right


      

12. Adding movement

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Move the snowFall down one pixel
        snowFall[i][1] += 1
  
        # If the snowFall has moved off the bottom of the screen
        if snowFall[i][1] > 400:
          
            # Reset it just above the top
            y = random.randrange(-50, -10)
            snowFall[i][1] = y
              
            # Give it a new x position
            x = random.randrange(0, 400)
            snowFall[i][0] = x
  
    # Go ahead and update the screen with what we've drawn.
    pygame.display.flip()
    clock.tick(20)
pygame.quit()

chevron_right


And Yes, Green snowfall has started!!

Complete Program

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import pygame
import random
pygame.init()
  
WHITE = [255, 255, 255]
GREEN  = [0,255,0]
SIZE = [400, 400]
  
screen = pygame.display.set_mode(SIZE)
pygame.display.set_caption("Programming World of GFG")
  
snowFall = []
for i in range(50):
    x = random.randrange(0, 400)
    y = random.randrange(0, 400)
    snowFall.append([x, y])
  
clock = pygame.time.Clock()
done = False
while not done:
  
    for event in pygame.event.get():  
        if event.type == pygame.QUIT:  
            done = True 
    screen.fill(WHITE)
    for i in range(len(snowFall)):
        pygame.draw.circle(screen, GREEN, snowFall[i], 2)
  
        snowFall[i][1] += 1
        if snowFall[i][1] > 400:
            y = random.randrange(-50, -10)
            snowFall[i][1] = y
         
            x = random.randrange(0, 400)
            snowFall[i][0] = x
  
    pygame.display.flip()
    clock.tick(20)
pygame.quit()

chevron_right


Output


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

4


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.