-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscene.py
256 lines (213 loc) · 8.28 KB
/
scene.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import time
import math
import pygame
import settings
import os
class Scene:
def __init__(self, game):
self.game = game
self.active = True
self.screen: pygame.Surface = game.screen
self.start = time.time()
self.shadow_intensity = 100
self.shadow_depth = 3
self.box_delay = 0.25
def elapsed(self):
return time.time() - self.start
def constrain(self, n, n_min, n_max):
return max(min(n_max, n), n_min)
def update(self):
print("Scene's update method has not been implemented")
def draw(self):
# the values of sin range from -1 to 1
r = math.sin(time.time() * 1.33 + 1.5) * 127 + 128
g = math.sin(time.time()) * 127 + 128
b = math.sin(time.time() * 0.25 - 0.6666) * 127 + 128
self.screen.fill((r, g, b))
print("Scene's draw method has not been implemented")
def draw_box(self, position: tuple, size: tuple):
if self.elapsed() < self.box_delay:
# expand the box from it's center to it's full size
position = (
position[0] + size[0] / 2 * (1 - self.elapsed() / self.box_delay),
position[1] + size[1] / 2 * (1 - self.elapsed() / self.box_delay),
)
size = (
size[0] * self.elapsed() / self.box_delay,
size[1] * self.elapsed() / self.box_delay,
)
# draw the blue background
pygame.draw.rect(
self.game.screen,
(0, 0, 255),
(position[0], position[1], size[0], size[1]),
width=0,
)
# draw the white border
pygame.draw.rect(
self.game.screen,
(255, 255, 255),
(position[0], position[1], size[0], size[1]),
width=2,
)
return
# create a drop shadow for the box
for i in range(1, self.shadow_depth + 1):
self.draw_rect_alpha(
self.game.screen,
(0, 0, 0, self.shadow_intensity),
(position[0] + i, position[1] + i, size[0], size[1]),
)
# draw the blue background
pygame.draw.rect(
self.game.screen,
(0, 0, 255),
(position[0], position[1], size[0], size[1]),
width=0,
)
# draw the white border
pygame.draw.rect(
self.game.screen,
(255, 255, 255),
(position[0], position[1], size[0], size[1]),
width=2,
)
def draw_box_centered(self, position: tuple, size: tuple):
if self.elapsed() < self.box_delay:
# expand the box from it's center to it's full size
size = (
size[0] * self.elapsed() / self.box_delay,
size[1] * self.elapsed() / self.box_delay,
)
# draw the blue background
pygame.draw.rect(
self.game.screen,
(0, 0, 255),
(
position[0] - size[0] / 2,
position[1] - size[1] / 2,
size[0],
size[1],
),
width=0,
)
# draw the white border
pygame.draw.rect(
self.game.screen,
(255, 255, 255),
(
position[0] - size[0] / 2,
position[1] - size[1] / 2,
size[0],
size[1],
),
width=2,
)
return
# create a drop shadow for the box
for i in range(1, self.shadow_depth + 1):
self.draw_rect_alpha(
self.game.screen,
(0, 0, 0, self.shadow_intensity),
(
position[0] - size[0] / 2 + i,
position[1] - size[1] / 2 + i,
size[0],
size[1],
),
)
# draw the blue background
pygame.draw.rect(
self.game.screen,
(0, 0, 255),
(position[0] - size[0] / 2, position[1] - size[1] / 2, size[0], size[1]),
width=0,
)
# draw the white border
pygame.draw.rect(
self.game.screen,
(255, 255, 255),
(position[0] - size[0] / 2, position[1] - size[1] / 2, size[0], size[1]),
width=2,
)
def draw_rect_alpha(self, surface, color, rect):
shape_surf = pygame.Surface(pygame.Rect(rect).size, pygame.SRCALPHA)
pygame.draw.rect(shape_surf, color, shape_surf.get_rect())
surface.blit(shape_surf, rect)
def make_transparent_surface(self, size):
return pygame.Surface(size, pygame.SRCALPHA, 32).convert_alpha()
def make_text(
self,
text,
color,
fontSize,
font=None,
stroke=False,
strokeColor=(0, 0, 0),
strokeThickness=1,
):
if font is None:
font = "assets/fonts/" + settings.FONT
# if we aren't stroking return the text directly
if not stroke:
return pygame.font.Font(font, fontSize).render(text, 1, color)
# if we are stroking, render the text with the stroke
# first render the text without the stroke
# create a version of the text in the stroke color and blit it to the surface
surf_text = pygame.font.Font(font, fontSize).render(text, 1, color)
surf_text_stroke = pygame.font.Font(font, fontSize).render(text, 1, strokeColor)
# create a transparent surface to draw the text and stroke on
size = (
surf_text.get_width() + strokeThickness * 3,
surf_text.get_height() + strokeThickness * 3,
)
surface = self.make_transparent_surface(size)
# blit the stroke text to the surface
for i in range(strokeThickness * 2 + 1):
for j in range(strokeThickness * 2 + 1):
surface.blit(surf_text_stroke, (i, j))
# blit the text on top of the stroke
surface.blit(surf_text, (strokeThickness, strokeThickness))
# return the surface
return surface
def blit_centered(self, source, target, position=(0.5, 0.5)):
"""
This function places a given surface at a specified position on the target surface.
Parameters:
source (pygame.Surface): The source surface to be placed. This is a pygame Surface object, which can be
created using pygame.font.Font.render() method.
target (pygame.Surface): The target surface on which the surface is to be placed. This could be
the game screen or any other surface.
position (tuple): A tuple of two values between 0 and 1, representing the relative position
on the target surface where the surface should be placed. The values correspond to the horizontal
and vertical position respectively. For example, a position of (0.5, 0.5) will place the text dead
center on the target surface.
"""
source_position = source.get_rect()
source_position.centerx = target.get_rect().centerx * position[0] * 2
source_position.centery = target.get_rect().centery * position[1] * 2
target.blit(source, source_position)
def play_sound(self, sound):
# verify the sound is loaded
if sound not in self.game.sfx:
print("Sound not found: " + sound)
return
print("play_sound: " + sound)
# set the volume of the sound based on the settings
self.game.sfx[sound].set_volume(self.game.volume_effects / 100)
pygame.mixer.Sound.play(self.game.sfx[sound])
# from the pygame tutorial:
# https://www.pygame.org/docs/tut/tom_games3.html
def load_png(self, name):
"""Load image and return image object"""
fullname = os.path.join("assets/images", name)
try:
image = pygame.image.load(fullname)
if image.get_alpha() is None:
image = image.convert()
else:
image = image.convert_alpha()
except FileNotFoundError:
print(f"Cannot load image: {fullname}")
raise SystemExit
return image, image.get_rect()