Skip to content

Instantly share code, notes, and snippets.

@AnimeshShaw
Last active January 25, 2022 20:24
Show Gist options
  • Save AnimeshShaw/7fa2441e329f40ee3fca to your computer and use it in GitHub Desktop.
Save AnimeshShaw/7fa2441e329f40ee3fca to your computer and use it in GitHub Desktop.
Blinking Snowfall Animation using PyGame
"""
@author : Psycho_Coder
For Details : http://www.rawcoders.com/Thread-Python-Blinking-Snowfall-Animation-using-PyGame
"""
import os
import random
import sys
import pygame
from pygame.constants import KEYDOWN, K_ESCAPE, NOFRAME
class SnowFall:
def __init__( self, size = ( 640, 480 ), NO_OF_FLAKES = 100 ):
pygame.init()
os.environ['SDL_VIDEO_CENTERED'] = '1'
self.screen_depth = 32
if size is not None and type( size ) is tuple or type( size ) is list and len( size ) == 2:
self.size = ( self.width, self.height ) = size
else:
raise ValueError( "size must be a tuple or list of width or height and should not be mull" )
if NO_OF_FLAKES is not None and type( NO_OF_FLAKES ) is int and NO_OF_FLAKES > 0:
self.NO_OF_FLAKES = NO_OF_FLAKES
else:
raise ValueError( "NO_OF_FLAKES must be int and greater than zero." )
self.white = ( 255, 255, 255 )
self.rCBack = ( 27, 27, 27 )
self.screen = pygame.display.set_mode( size, NOFRAME, self.screen_depth )
self.screen.fill( self.rCBack )
self.FPS = 30
self.timer = pygame.time.Clock()
self.game_loop()
def game_loop( self ):
snow_flakes_list = []
loc_x, loc_y = 0, 0
img = pygame.image.load( "images/snowman.gif" ).convert_alpha()
imgw, imgh = img.get_size()
img1 = pygame.image.load( "images/snowman2.gif" ).convert_alpha()
imgw1, imgh1 = img1.get_size()
img2 = pygame.image.load( "images/angelglobe.gif" ).convert_alpha()
imgw2, imgh2 = img2.get_size()
for i in range( self.NO_OF_FLAKES ):
loc_x = random.randrange( 0, self.width )
loc_y = random.randrange( 0, self.height )
snow_flakes_list.append( [ loc_x, loc_y ] )
while True:
for event in pygame.event.get():
if event.type == KEYDOWN and event.key == K_ESCAPE:
pygame.quit()
sys.exit()
self.screen.fill( self.rCBack )
self.screen.blit( img, ( self.width - imgw, self.height - imgh ) )
self.screen.blit( img1, ( 0 , self.height - imgh1 ) )
self.screen.blit( img2, ( ( self.width - imgw2 ) // 2, self.height - imgh2 ) )
for i in range( len( snow_flakes_list ) ):
pygame.draw.circle( self.screen, self.white, snow_flakes_list[i], random.choice( [2, 3, 4] ) )
snow_flakes_list[i][1] += 1
if snow_flakes_list[i][1] > self.height:
snow_flakes_list[i][1] = random.randrange( -30, -20 )
snow_flakes_list[i][0] = random.randrange( 0, self.width )
pygame.display.update()
self.timer.tick( self.FPS )
def main():
SnowFall( ( 800, 600 ), 150 )
if __name__ == "__main__":
main()
@BaxiAbhijit
Copy link

Hey Buddy...Where are the images " images/snowman.gif" and others. Your project looks cool . I tried to clone your repo and tried to run it. It threw me an error "Couldn't open images " .Can you please add the folder images with the repo so that I can try to run . Pardon me if I am wrong . I am actually new to pygame and came across your project and felt its pretty awsome.

@AnimeshShaw
Copy link
Author

Hi BaxiAbhijit

I have attached the images. Sorry for the late reply.

snowman2

angelglobe

snowman

@cfranz1598
Copy link

I needed an image to flicker for a few seconds, I hadn't even considered random.choices... Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment