-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathRainsim.py
163 lines (127 loc) · 4.15 KB
/
Rainsim.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
tic=60
def getMousePos():
p = pygame.mouse.get_pos()
return [
p[0] / main.SCALE, p[1] / main.SCALE
]
def getDistance(p1, p2):
return int(
math.sqrt((p2[0] - p1[0])**2 + (p2[1] - p1[1])**2)
)
def colorValid(_color, _min = 0, _max = 255):
newColor = math.fabs(_color)
n = _max - _min
if newColor > n:
if int(newColor / n) % 2 == 0:
newColor = newColor % n
else:
newColor = n - (newColor % n)
return int(newColor) + _min
def getCirclePos(_pos, _radius, _angle):
return [
int(_pos[0] + _radius * math.cos(math.radians(_angle))),
int(_pos[1] + _radius * math.sin(math.radians(_angle)))
]
def boolSwap(_bool):
if _bool:
return False
else:
return True
class raindrop():
def __init__(self, _pos, _velocity, _size):
self.pos = _pos
self.velocity = _velocity
self.size = _size
self.dead = False
def render(self, _surface):
pygame.draw.circle(_surface, [0, 0, 255], self.pos, self.size, 0)
def tick(self):
self.pos[1] += self.velocity[1]
if self.pos[1] - (self.size / 2) > main.HEIGHT:
self.dead = True
def tick():
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYUP:
if event.key in main.KEYSDOWN:
main.KEYSDOWN.remove(event.key)
if event.type == pygame.KEYDOWN:
if event.key not in main.KEYSDOWN:
main.KEYSDOWN.append(event.key)
if event.key == pygame.K_DOWN:
global tic
tic*=1.5
print(tic)
if event.key == pygame.K_UP:
global tic
tic*=0.7
print(tic)
if main.TICKS % 1 == 0:
main.DROPS.append(raindrop([random.randint(0, main.WIDTH), 0], [0, random.randint(2, 10)], random.randint(2, 5)))
for _ in main.DROPS:
_.tick()
if _.dead:
main.DROPS.remove(_)
def render():
# fill
main.SCREEN.fill(main.BACKGROUNDCOLOR)
if main.FILL:
main.SURF.fill(main.COLOR)
for _ in main.DROPS:
_.render(main.SURF)
main.SCREEN.blit(pygame.transform.scale(main.SURF, [int(main.WIDTH * main.SCALE), int(main.HEIGHT * main.SCALE)]), [0, 0])
pygame.display.flip()
def run():
ticksPerSecond = tic
lastTime = time.time() * 1000000000
nsPerTick = 1000000000.0 / float(ticksPerSecond)
ticks = 0
frames = 0
lastTimer = time.time() * 1000
delta = 0.0
while True:
now = time.time() * 1000000000
nsPerTick = 1000000000.0 / float(tic)
delta += float(now - lastTime) / float(nsPerTick)
lastTime = now
shouldRender = False
while delta >= 1:
ticks += 1
main.TICKS += 1
tick()
delta -= 1
shouldRender = True
if shouldRender:
frames += 1
render()
if time.time() * 1000 - lastTimer >= 1000:
lastTimer += 1000
# debug
print("Frames: " + str(frames) + ", ticks: " + str(ticks))
frames = 0
ticks = 0
class dummy():
pass
import pygame, sys, time, math, os, random
# main dummy
main = dummy()
main.TICKS = 0
main.RES = 1
main.WIDTH = 1080 / main.RES
main.HEIGHT = 720 / main.RES
main.SIZE = [main.WIDTH, main.HEIGHT]
main.CENTER = [main.WIDTH / 2, main.HEIGHT / 2]
main.CENTERFLOAT = [main.WIDTH / 2.0, main.HEIGHT / 2.0]
main.SCALE = 1 * main.RES
main.SCALEDSIZE = [int(main.WIDTH * main.SCALE), int(main.HEIGHT * main.SCALE)]
main.SCALEDSIZEFLOAT = [main.WIDTH * main.SCALE, main.HEIGHT * main.SCALE]
main.SURF = pygame.Surface(main.SIZE)
main.SCREEN = pygame.display.set_mode(main.SCALEDSIZE)
pygame.display.set_caption("Rain Simulator")
main.BACKGROUNDCOLOR = [0, 0, 0]
main.COLOR = [0, 0, 0]
main.FILL = True
main.KEYSDOWN = []
main.DROPS = []
run()