-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.py
123 lines (101 loc) · 5.08 KB
/
application.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import pygame
from pygame.locals import *
import branchmap3 as branchmap
import snowflakes
import math,numpy,random
class App(): #Based on my PygameBase
def __init__(self,width,height,title="pygame window",icon=None):
self.running = False
self.size = (width,height)
self.title = title
self.icon = icon
pygame.init()
def init(self):
"""Commands to be processed before the application starts"""
NUM_SNOWFLAKES = 10 #How many snowflakes to make. 1 to 5 should be fine at 100 fps, it starts to slow noticeably from there.
self.NUM_BRANCHES = 4 #Changes the size/complexity of snowflakes
pygame.display.set_caption(self.title)
if self.icon != None:
self.icon = pygame.image.load(self.icon)
pygame.display.set_icon(self.icon)
self.display = pygame.display.set_mode(self.size, pygame.HWSURFACE | DOUBLEBUF)
self.font = pygame.font.SysFont("kruella",40) #Preference for Kruella. Otherwise standard pygame font should be used.
self.snowflakes = []
for x in range(NUM_SNOWFLAKES):
snowflake = snowflakes.Snowflake() #All generated snowflakes are stored here.
for i in range(self.NUM_BRANCHES):
snowflake.branch()
snowflake.complete()
snowflake.position = (random.randrange(0,self.size[0]+1),random.randrange(int(-snowflake.size),self.size[1]))
self.snowflakes.append(snowflake)
self.clock = pygame.time.Clock()
return True
def get_transform_array(self,TURN_BY):
"""Get a transformation array based on degree rotation"""
RADIANS = math.pi/180.
return numpy.array([[math.cos(RADIANS*TURN_BY),-math.sin(RADIANS*TURN_BY)],
[math.sin(RADIANS*TURN_BY),math.cos(RADIANS*TURN_BY)]])
def __loop__(self):
"""Commands processed every frame"""
GRAVITY = 0.75 # affects how fast snowflakes fall
WIND_CONSTANT = 5.25 # wind = sin(time)+constant. Affects how much the snowflakes will actually be moved. 0 means they will just oscillate back and forth around a mean position.
wind = math.sin(pygame.time.get_ticks()/1000.)+WIND_CONSTANT
for snowflake in self.snowflakes:
#self.snowflake = snowflake
snowflake.transform(self.get_transform_array(snowflake.rotate*((self.clock.get_time()/1000.)*60)))
if snowflake.position[1] <= snowflake.size + self.size[1]:
snowflake.position = (snowflake.position[0]+(wind/snowflake.weight),snowflake.position[1]+GRAVITY*snowflake.weight*((self.clock.get_time()/1000.)*60))
if snowflake.position[0]-snowflake.size > self.size[0]:
snowflake.position = (-snowflake.size,snowflake.position[1])
elif snowflake.position[0]+snowflake.size < 0:
snowflake.position = (self.size[0]+snowflake.size,snowflake.position[1])
else:
self.snowflakes.remove(snowflake) #Destroy the snowflake and make a new one
snowflake = snowflakes.Snowflake()
for i in range(self.NUM_BRANCHES):
snowflake.branch()
snowflake.complete()
snowflake.position = (random.randrange(0,self.size[0]+1),-snowflake.size)
self.snowflakes.append(snowflake)
def __events__(self, event):
"""Event Handling"""
if event.type == pygame.QUIT:
self.running = False
def adjust_coords(self,coord):
"""Redundant"""
return (self.size[0]/2.+coord[0],self.size[1]/2.-coord[1])
def __render__(self):
"""Rendering"""
self.display.fill((0,0,0))
self.display.blit(self.font.render(str(round(self.clock.get_fps(),1)),1,(255,255,255)),(0,0))
for snowflake in self.snowflakes:
for branch in snowflake:
for c in branch[1]:
pygame.draw.aaline(self.display,(255,255,255),snowflake.render_coordinates(branch[0]),snowflake.render_coordinates(c))
pygame.display.flip()
self.clock.tick(self.fps_limit)
def __cleanup__(self,e=None):
"""Commands to be processed when quiiting"""
pygame.quit()
if e != None:
raise e
def start(self,fps_limit=0):
"""Start the application"""
self.fps_limit = fps_limit
ex = None
try:
self.running = self.init()
except Exception,e:
ex = e
while self.running == True:
try:
for event in pygame.event.get():
self.__events__(event)
self.__loop__()
self.__render__()
except Exception,e:
self.running = False
ex = e
self.__cleanup__(ex)
a = App(800,800,"Snowstorm")
a.start(30) #framelimit of 30 - change this as you like, movement is frame independent but it probably won't look as smooth