forked from senweim/JumpKingAtHome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.py
217 lines (116 loc) · 4.35 KB
/
weather.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr.bin/env python
#
#
#
#
import pygame
import collections
import os
import re
import sys
class Weathers:
def __init__(self):
self.width, self.height = int(os.environ.get("screen_width")), int(os.environ.get("screen_height"))
self.wind_levels = [25, 26, 27, 28, 29, 30, 31]
self.directory = "weather"
self.rain_color = (101, 157, 191)
self.snow_color = (100, 100, 100)
self.weather = collections.defaultdict()
self._load_images()
def _load_weather(self, weather):
frames = []
for file in sorted(os.listdir(self.directory), key = lambda filename: int(re.search(r'\d+', filename).group())):
if re.search(r"^{weather}\d+".format(weather = weather), file):
frame = pygame.image.load(f"{self.directory}\\{file}")
frames.append(frame)
return frames
def _load_images(self):
for file in os.listdir(self.directory):
name = re.search(r"(.*)mask(\d+)", file)
if name:
mask = self._load_mask(file)
weather_frames = self._load_weather(name.group(1))
if "snow" in name.group(1):
color = self.snow_color
elif "rain" in name.group(1):
color = self.rain_color
level = int(name.group(2)) - 1
hasWind = level in self.wind_levels
images = []
for frame in weather_frames:
image = pygame.Surface((self.width, self.height), pygame.SRCALPHA).convert()
image.fill(color)
image.set_colorkey(color, pygame.RLEACCEL)
beta_image = pygame.Surface((self.width, self.height), pygame.SRCALPHA).convert()
beta_image.fill(color)
beta_image.set_colorkey((0, 0, 0), pygame.RLEACCEL)
pygame.draw.polygon(beta_image, (0, 0, 0), mask.outline(), 0)
for i in range(self.height // frame.get_height()):
for j in range(self.width // frame.get_width()):
image.blit(frame, (j * frame.get_width(), i * frame.get_height()))
image.blit(beta_image, beta_image.get_rect())
images.append(image)
self.weather[level] = Weather(images, hasWind)
def _load_mask(self, file):
mask = pygame.image.load(f"{self.directory}\\{file}")
mask = pygame.mask.from_surface(mask)
return mask
class Weather:
def __init__(self, images, wind):
self.images = images
self.hasWind = wind
self.counter = 0
self.interval = 6
def blitme(self, screen, wind):
if self.counter >= self.interval * len(self.images):
self.counter = 0
image = self.images[self.counter // self.interval]
if self.hasWind:
rect = wind
if rect.x > image.get_width():
rect.x -= rect.x // image.get_width() * rect.width
if rect.x < -image.get_width():
rect.x += rect.x // image.get_width() * rect.width
if rect.colliderect(screen.get_rect()):
screen.blit(image, rect)
if rect.move(image.get_width(), 0).colliderect(screen.get_rect()):
screen.blit(image, rect.move(image.get_width(), 0))
if rect.move(-image.get_width(), 0).colliderect(screen.get_rect()):
screen.blit(image, rect.move(-image.get_width(), 0))
else:
screen.blit(image, (0, 0))
self.counter += 1
if __name__ == "__main__":
pygame.init()
mega_screen = pygame.display.set_mode((480, 360), pygame.SRCALPHA)
alpha_screen = pygame.Surface((480, 360), pygame.SRCALPHA).convert()
alpha_screen.set_colorkey((0, 0, 0), pygame.RLEACCEL)
screen = pygame.Surface((480, 360), pygame.SRCALPHA).convert()
screen.set_colorkey((0, 255, 0), pygame.RLEACCEL)
mask = pygame.mask.from_surface(pygame.image.load("weather\\rainmask13.png"))
beta_screen = pygame.Surface((480, 360), pygame.SRCALPHA).convert()
beta_screen.set_colorkey((255, 0, 0), pygame.RLEACCEL)
#print(mask.outline())
clock = pygame.time.Clock()
a = 0
while True:
a += 1
if a > 3:
a = 1
clock.tick(5)
mega_screen.fill((200, 200, 200))
screen.fill((0, 0, 0))
beta_screen.fill((0, 255, 0))
wack = pygame.Surface((480, 360), pygame.SRCALPHA).convert()
rain = pygame.image.load(f"weather\\light_rain{a}.png").convert_alpha()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
for i in range(screen.get_height() // rain.get_height()):
for j in range(screen.get_width() // rain.get_width()):
screen.blit(rain, (rain.get_width() * j, rain.get_height() * i))
pygame.draw.polygon(beta_screen, (255, 0, 0), mask.outline(), 0)
screen.blit(beta_screen, (0, 0))
alpha_screen.blit(screen, (0, 0))
mega_screen.blit(alpha_screen, (0, 0))
pygame.display.flip()