Table of contents
1.
Introduction
2.
Import the modules
3.
Start the game engine.
4.
Set the screen resolution
5.
Give your snowfall display window screen a name
6.
Make a blank array for your snowfall
7.
Looping to determine snowfall locations
8.
Keep track of time
9.
Establish criteria for snowfall occurrence
10.
Set the background of the screen
11.
Process and make the snowfall
12.
Including movement
13.
Complete Program
14.
Frequently Asked Questions
14.1.
What are the steps you should take for creating a snowfall display in pygame? 
14.2.
What is the key working element in creating a snowfall display in pygame?
14.3.
What are the other similar things like a snowfall display that we can create using the same method?
15.
Conclusion
Last Updated: Mar 27, 2024
Easy

Snowfall Display Using Pygame

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

"When the snow falls, and the white wind blows, the lone wolf dies, but the pack survives" ~ Sansa Stark (Game of Thrones, S7 E2 "Stormborn"). Many of us had seen snowfalls in person, but almost all of us had seen snowfalls in many movies and television series. 

Experiencing it in person is good, but it is equally awesome to see on the screen. 

What if we display this snowfall on the screen with a few lines of code? Exciting! Isn't it. In this article, we are going to create a snowfall display using pygame.

Before diving into this topic, it is highly recommended that you review the fundamentals of Pygame.

Import the modules

In order to create a snowfall display using pygame, we will follow these steps: 

import pygame
import random

 

Pygame is an open-source library for creating games and other multimedia tasks. We are including the pygame library here because it will provide us with functions for creating windows, and shapes, reading keyboard and mouse events and performing functions or tasks. We will begin with importing the necessary modules for our program.

Start the game engine.

Now we will create some colors,

WHITE = [255, 255, 255]
SKY BLUE = [0, 245, 255]

 

Do remember that, “COLOR” = [COLORS’ RED CODE, COLORS’ GREEN CODE, COLORS’ BLUE CODE], this is going to be the format of the color code. If we are going to add color in pygame, we are going to follow this format.  

Afterward, now we will create a game display;

display= pygame.display.set_mode((width,height)) 

 

Here, two parameters are specified the first one would be width, second would be height.

Set the screen resolution

Depending on your system's resolution, it could be a new number.

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

 

  • screen = pygame.display.set_mode(SIZE): Make a window with the specified dimensions (400,400), we will set this as the window size here.

Give your snowfall display window screen a name

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

 

  • pygame.display.set_caption("Programming World of Ninjas"): It will set a title to the window. The given name can be seen in the upper left corner of the output window.

Make a blank array for your snowfall

snowFall = []

 

  • snowFall = []: It will create a blank array that will store the snowfall information.

Looping to determine snowfall locations

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

 

  • Make a loop and run it 50 times, then use the random Module to add snowfall in a random x,y position.
  • random.randrange(0, 400): It will create the random range for the x and y variables from (0,400).

Keep track of time

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

 

  • clock = pygame.time.Clock(): Making an object to help us keep track of time. It will help us monitor snowfall timing and loop structure.

Establish criteria for snowfall occurrence

# loop till the close button is pressed by the user
done = False

while not done:

#  When the 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

 

  • Snowfall should continue until the user presses the close button, and a for loop should be used inside the while loop.

Set the background of the screen

screen.fill(BLUE)

 

  • screen.fill(BLUE): It will fill the screen with blue color, remember the color code we defined at the start of the program. 

Process and make the snowfall

for i in range(len(snowFall)):
pygame.draw.circle(screen, WHITE, snowFall[i], 2)

 

  • for i in range(len(snowFall)): It will help us to process each Snowfall in the list, using a for a loop.
  • pygame.draw.circle(screen, WHITE, snowFall[i], 2): It will make the appearance of snowfall on the screen.

Including movement

# Move the snowFall down one pixel
	snowFall[i][1] += 1

# If the snowFall has moved off to 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

	pygame.display.flip()
	clock.tick(20)
pygame.quit()

 

  • pygame.display.flip(): It will help us go forward and update the screen with what we have drawn.

And Yes, the snowfall has started!!

Complete Program

import pygame
import random
pygame.init()

BLUE = [0, 245, 255]
WHITE= [255,255,255]
SIZE = [400, 400]

screen = pygame.display.set_mode(SIZE)
pygame.display.set_caption("Programming World of Ninjas")

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(BLUE)
	for i in range(len(snowFall)):
		pygame.draw.circle(screen, WHITE, 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()

 

Output: 

Frequently Asked Questions

What are the steps you should take for creating a snowfall display in pygame? 

Following are the detailed steps that we took for creating the snowfall display:

  • Module importation and starting the game engine.
  • Set the screen resolution, and screen name, and initialize the array for snowfall.
  • Looping to determine snowfall locations
  • Keep track of time.
  • Establish criteria for and set the background for snowfall occurrence
  • Processing, making the snowfall, and including the movement.

What is the key working element in creating a snowfall display in pygame?

The most important thing that you should keep in mind while creating a snowfall display is to create a loop and keep track of the time of motion.

What are the other similar things like a snowfall display that we can create using the same method?

You can create the falling of leaves from a tree, a rainfall (blue droplet of water falling from the sky), the falling of meteors (meteor shower) on a dark night, and much more.

Conclusion

In this article, we have extensively discussed how to create a snowfall display using the pygame. You should check this out to know more about pygame applications. You should also check out this; it will help you with the concept of pyglet. If you would like to learn more, check out our articles here. Do upvote our blog to help other ninjas grow.

As Sansa Stark talks about the survival of the pack when the winter arrives, here in Coding Ninjas we will give you the pack who will provide you with the guidance and mentorship for your winter storm of interview. So you can count on us for everything. 

Check out this problem - Optimal Strategy For A Game

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available, interview puzzles. Also, look at the interview experiences and interview bundle for placement preparations. Please look at this YouTube tutorial to explore the preparation strategy for SDE placements.

Happy learning!

Live masterclass