Skip to content

Instantly share code, notes, and snippets.

@amyghotra
Last active December 20, 2018 17:19
Show Gist options
  • Save amyghotra/be419fb7ec1717bf08b37cda7603c4fe to your computer and use it in GitHub Desktop.
Save amyghotra/be419fb7ec1717bf08b37cda7603c4fe to your computer and use it in GitHub Desktop.
Using python, create continuously falling snow.
import pygame
import random
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
pygame.init()
# Set the width and height of the screen [width, height]
size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Snow")
class SnowFlake():
'''
screen, WHITE, snow_list[i], 2
'''
def __init__(self, size, position, wind=False):
self.size = size
self.position = position
self.wind = wind
def fall(self, speed):
"""
Take in a integer that represnts the speed at which the snowflake is falling in the y-direction.
A positive integer will have the snowflake falling down the screen.
A negative integer will have the snowflake falling up the screen.
If wind = True
- the x direction of the snowflake changes
"""
#[x,y]
#self.position[0] += speed #x position
self.position[1] += speed #y position
def draw(self):
"""
Uses pygame and the global screen variable to draw the snowflake on the screen
"""
pygame.draw.circle(screen, WHITE, self.position, self.size)
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# Speed
speed = 1
#y_position = 0
#INITIALIZE YOUR SNOWFLAKE HERE!
# Snow List
snow_list = []
# -------- Main Program Loop -----------
while not done:
# --- Main event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# --- Game logic should go here
# --- Screen-clearing code goes here
# Here, we clear the screen to white. Don't put other drawing commands
# above this, or they will be erased with this command.
# If you want a background image, replace this clear with blit'ing the
# background image.
screen.fill(BLACK)
# --- Drawing code should go here
# Begin Snow
y_position = 0 #makes it look like it only stays at 0
x_position = random.randint(0,700)
speed = 1
y_position += speed
flake = SnowFlake(2, [x_position, y_position], False)
snow_list.append(flake)
for flake in snow_list:
flake.draw()
flake.fall(speed) #def fall (self, speed):
# End Snow
# --- Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# --- Limit to 60 frames per second
clock.tick(60)
# Close the window and quit.
pygame.quit()
exit() # Needed when using IDLE
@amyghotra
Copy link
Author

snowshot

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