Pygame with Xmas ☃🎄

Hashini Munasinghe
5 min readDec 19, 2020

Snow usually occurs as tiny ice crystals cling together in clouds ☁to become snowflakes. They can become strong enough to collapse to the ground if enough crystals hold together. Snowflakes that descend into humid air that is marginally warmer than 0 ° C will melt and stick together around the edges to form large flakes ❄☃.
But in programming, we can create Snow🥶 using pygame and random libraries in python. 🐍

What is a Pygame library?

Python is the most popular programming language, or nothing wrong, to say it’s the programming language of the next decade. Python aggressively makes its presence accessible in the developing field of computer science. For various areas, such as Machine Learning (Numpy, Pandas, Matplotlib), Artificial Intelligence (Pytorch, TensorFlow), and Game Creation, Python has extensive libraries (Pygame,Pyglet).
Pygame is a cross-platform series of Python modules used for the development of video games. It consists of sound libraries and computer graphics that are intended to be used in the Python programming language. To substitute PySDL, Pygame has been officially published by Pete Shinners. To build client-side programs that can theoretically be bundled in a standalone executable, Pygame is suitable.

Usage of a random module to add pseudo-random number generators with different distributions. A uniform list from a set occurs for integers.
A uniform collection of a random variable is used for sequences, a function to produce a random permutation of an in-place array, and a function for random sampling without substitution.
We can create Snowfall Animation using only these two libraries

First of all, we should install pygame and random libraries following this path. I used pycharm IDE to do that program.

File — Settings -Project Interpreter -It will display the search box. Search the pygame and click on the install package button.

then we should import pygame and random libraries to our program. we use a random library to that program to do randomized the movement of snow

import pygame
import random

then we initialize the pygame game engine.

pygame.init()

then we give notation for black and white colors

black = (0, 0, 0)
white = (255, 255, 255)

then put a screen resolution, in this situation I gave (1110,600) resolution to display in my window and gave screen caption like a “Snow_Falling”

myDisplay = pygame.display.set_mode((1110, 600))
pygame.display.set_caption("Snow_Falling")

after I declared an array called SnowFlakes
put a for loop to generate an amount of Snow and give resolution to X-axis and Y-axis respectively 1110 and 590

SnowFlakes = []
for q in range(100):
x = random.randrange(0, 1110)
y = random.randrange(0, 590)
SnowFlakes.append([x, y])

next, I Created a new Clock object, which can be used for time tracking. The clock also offers several features to help monitor the framerate of an animation. It is necessary to name this process once per frame. This would measure the number of milliseconds that have elapsed since the previous call. Then put a code if the amination closed within only the user clicks close button on the screen and set background-position in [0,0] according to x-axis and y-axis. Then imported the background image. in this .covert() function use in pygame library. The convert() function is used to convert the game. Surface to the same pixel format as the pixel format that is used for the final display, the same pixel format that is generated by pygames.

clock = pygame.time.Clock()
snow = False
background_position = [0, 0]
background_image=pygame.image.load("Background.jpeg").convert()

while loop starts again from the beginning. In an event queue that can be received with the code pygame, Pygame can record all events from the user. Oh, case. The get() . Each entity is an event object in this queue and they will all have the form of the attribute, which is an integer reflecting what type of event it is. Then give user to close the program using python.QUIT: . In addition to making a window open on the screen, this program continually scans for a QUIT case, and then re-draws the unchanged window to the screen over and over again.

while not snow:
for event in pygame.event.get():
if event.type == pygame.QUIT:
snow = True

Block Swap stands for the word blit, and .blit() is how you copy the contents of one surface to another. From one surface to another, you can just use .blit(), but because the screen is just another surface, that’s not a concern.
myDisplay.blit(background_image,background_position)
then declare the variable I will process every SnowFlakes and increment by 1
then draw a white circle (snow ) size 7 on the screen process with I variable. you can use any size and color.

myDisplay.blit(background_image,background_position)
for i in SnowFlakes:
i[1]+= 1
pygame.draw.circle(myDisplay, white, i, 7)

then use a conditional statement to check height is greater than 580 .in random.randrange() it assign between -50 (included) to -5 (not included) value to i[1] and in second random.randrange() it assign between 0(included) to 110 (not included) value to i[0]

if i[1] > 580:
i[1] = random.randrange(-50, -5)
i[0] = random.randrange(1100)

display.flip() will update the contents of the entire display.
It is necessary to name this process once per frame. It’s going to compute how many. Since the previous call, milliseconds have gone by. If you pass the optional framerate statement, having the game running slower than the given ticks per second will pause the feature. This can be used to help restrict a game’s runtime tempo. The software can never run at more than 600 frames a second by calling Clock.tick(600) once per frame. you can change snow falling speed using Clock.tick() function. we use pygame.quit() to end the program

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

Output Animation 😎

Output

Merry Christmas🎄🎅

--

--

Hashini Munasinghe

Undergraduate at Uva Wellassa University of Sri Lanka