-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnowfall.py
78 lines (60 loc) · 1.97 KB
/
snowfall.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import os # to centre the screen
import sys # to exit the program gracefully
import pygame as pg
from random import randrange
CAPTION = "Sit back, and watch snowfall"
SCREEN_SIZE = (1366,768)
WHITE = (255, 255, 255)
CHARCOAL = (54, 69, 79)
class Snow(object):
def __init__(self):
self.snow_density = 600
self.snow_list = []
self.populate_snow_list()
def populate_snow_list(self):
for i in range(600):
x = randrange(0, SCREEN_SIZE[0])
y = randrange(0, SCREEN_SIZE[1])
self.snow_list.append([x, y])
def update(self):
for coord in self.snow_list:
coord[1] += 2
if coord[1] > 768:
coord[0] = randrange(0, SCREEN_SIZE[0])
coord[1] = randrange(-50, 0)
def draw(self, screen):
for coord in self.snow_list:
pg.draw.circle(screen, WHITE, coord, 5)
class App(object):
def __init__(self):
self.screen = pg.display.get_surface() # just gives you the reference code
self.clock = pg.time.Clock()
self.fps = 60
self.done = False
self.keys = pg.key.get_pressed()
self.color = CHARCOAL
self.snow = Snow()
def event_loop(self):
for event in pg.event.get():
if event.type == pg.QUIT:
self.done = True
def render(self):
self.screen.fill(self.color)
self.snow.draw(self.screen)
pg.display.update()
def main_loop(self):
while not self.done:
self.event_loop()
self.snow.update()
self.render()
self.clock.tick(self.fps)
def main():
os.environ['SDL_VIDEO_CENTERED'] = '1' # centre screen
pg.init()
pg.display.set_caption(CAPTION)
pg.display.set_mode(SCREEN_SIZE)
App().main_loop()
pg.quit()
sys.exit() # fancy exit
if __name__ == "__main__":
main()