-
Notifications
You must be signed in to change notification settings - Fork 1
/
overlay.py
268 lines (226 loc) · 10.4 KB
/
overlay.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
257
258
259
260
261
262
263
264
265
266
267
268
import pygame
import colors as cols
from stgs import *
from menu import *
pygame.font.init()
def transparentRect(size, alpha, color=(0, 0, 0)):
surf = pygame.Surface(size, pygame.SRCALPHA)
surf.fill((color[0], color[1], color[2], alpha))
return surf.convert_alpha()
class PauseOverlay(pygame.sprite.Sprite):
def __init__(self, game):
self.game = game
pygame.sprite.Sprite.__init__(self, game.overlayer)
self.components = pygame.sprite.Group()
self.text = []
self.active = False
self.rect = pygame.Rect(0, 0, winWidth, winHeight)
self.image = pygame.Surface((winWidth, winHeight), pygame.SRCALPHA).convert_alpha()
self.loadComponents()
self.render()
def loadComponents(self):
for comp in self.components:
comp.kill()
self.audioSlider1 = SettingSlider(self.game, (100, 350), addGroups = [self.components])
self.audioSlider2 = SettingSlider(self.game, (100, 500), addGroups = [self.components])
self.audioSlider1.image.set_colorkey((0,0,0))
self.audioSlider2.image.set_colorkey((0,0,0))
self.fpsButton = Button(self.game, (800, 250), text = 'Toggle FPS', onClick = self.game.toggleFps ,groups = [self.components], center = True, colors=(colors.yellow, colors.white))
self.aaliasButton = Button(self.game, (800, 330), text = 'Toggle Anti - Aliasing', onClick = self.game.toggleAalias ,groups = [self.components], center = True, colors=(colors.yellow, colors.white))
Button(self.game, (350, 550), groups = [self.components], text = "Return to menu", onClick=self.game.endgame, center = True, colors = (colors.yellow, colors.white))
self.text = [
Text('title2', 'Paused', cols.orangeRed, self.game.antialiasing, (self.rect.width/2.4, 10)),
Text('title1', 'Audio Control', cols.orangeRed, self.game.antialiasing, (75, 250)),
Text('caption1', 'Music Volume', cols.orangeRed, self.game.antialiasing, (75, 325)),
Text('caption1', 'Fx Volume', cols.orangeRed, self.game.antialiasing, (75, 475))
]
self.audioSlider1.setRatio(self.game.mixer.musicVolume)
self.audioSlider2.setRatio(self.game.mixer.fxVolume)
def applyComponents(self):
self.game.mixer.setMusicVolume(self.audioSlider1.get_ratio())
self.game.mixer.setFxVolume(self.audioSlider2.get_ratio())
def activate(self):
self.active = True
self.audioSlider1.setRatio(self.game.mixer.musicVolume)
self.audioSlider2.setRatio(self.game.mixer.fxVolume)
def deactivate(self):
self.active = False
def update(self):
if self.active:
self.render()
self.components.update()
self.applyComponents()
def render(self):
self.image.fill((0,0,0,190)) #self.transparent)
for comp in self.components:
self.image.blit(comp.image, comp.rect)
for text in self.text:
self.image.blit(text.image, text.pos)
class MapOverlay(pygame.sprite.Sprite):
def __init__(self, game):
self.game = game
pygame.sprite.Sprite.__init__(self, game.overlayer)
self.components = pygame.sprite.Group()
self.text = []
self.active = False
self.rect = pygame.Rect(0, 0, winWidth, winHeight)
self.image = pygame.Surface((winWidth, winHeight), pygame.SRCALPHA).convert()
self.loadComponents()
self.render()
def loadComponents(self):
for comp in self.components:
comp.kill()
self.mapImage = pygame.image.load(asset('gameMap.png'))
self.mapImage = pygame.transform.scale(self.mapImage, (int(winWidth), int(winHeight))).convert()
def activate(self):
self.active = True
def deactivate(self):
self.active = False
def update(self):
now = pygame.time.get_ticks()
if self.active:
self.render()
self.components.update()
if checkKey("map") and now - self.game.lastPause >= 180:
self.deactivate()
self.game.unPause()
self.game.lastPause = now
else:
if checkKey("map") and now - self.game.lastPause >= 180:
self.activate()
self.game.pause = True
self.game.lastPause = now
def render(self):
self.image.fill((0,0,0,190)) #self.transparent)
self.image.blit(self.mapImage, (0, 0))
# for comp in self.components:
# self.image.blit(comp.image, comp.rect)
# for text in self.text:
# self.image.blit(text.image, text.pos)
class DialogueOverlay(pygame.sprite.Sprite):
def __init__(self, game):
self.game = game
pygame.sprite.Sprite.__init__(self, game.overlayer)
self.components = pygame.sprite.Group()
self.text = []
self.active = True
self.rect = pygame.Rect(0, 0, winWidth, winHeight)
self.image = pygame.Surface((winWidth, winHeight), pygame.SRCALPHA).convert_alpha()
self.loadComponents()
self.render()
self.lastInteract = pygame.time.get_ticks()
def loadComponents(self):
self.components = pygame.sprite.Group()
def activate(self):
self.active = True
self.lastInteract = pygame.time.get_ticks()
self.game.pause = True
self.game.lastPause = pygame.time.get_ticks()
def deactivate(self):
self.active = False
self.lastInteract = pygame.time.get_ticks()
self.game.pause = False
self.game.lastPause = pygame.time.get_ticks()
def update(self):
now = pygame.time.get_ticks()
compLen = len(self.components.sprites())
if self.active:
self.render()
if compLen == 0:
self.deactivate()
else:
self.components.update()
if checkKey("interact") and now-self.lastInteract > 200:
lastSpr = self.components.sprites()[compLen-1]
if lastSpr.finished:
for comp in self.components:
comp.kill()
self.deactivate()
else:
pass
def render(self):
for comp in self.components:
self.image.blit(comp.image, comp.rect)
def dialogue(self, npc):
self.activate()
self.components.add(Dialogue(self.game, npc.text))
class Dialogue(pygame.sprite.Sprite):
def __init__(self, game, text, **kwargs):
self.groups = game.sprites, game.overlayer
self.game = game
pygame.sprite.Sprite.__init__(self, self.groups)
self.height = 8
self.tileSize = 32
self.text = text
self.rect = pygame.Rect(0, winHeight-self.height*self.tileSize, winWidth, self.height*self.tileSize)
self.textColor = colors.white
self.lastInteract = pygame.time.get_ticks()
self.aalias = True
self.borderPalette = pygame.image.load(asset('objects/dPallette2.png'))
if self.borderPalette.get_width()/self.tileSize < 3 or self.borderPalette.get_height()/self.tileSize < 3 :
print("Check your pallette size. It is currently invalid for the set tile size.")
self.render()
self.finished = False
def render(self):
self.image = pygame.Surface((winWidth, self.height*self.tileSize), pygame.SRCALPHA)
self.baseImage = createFrame(winWidth/self.tileSize, self.height)
self.rendText = dText('title1', self.text, colors.white, True, (self.tileSize, self.tileSize), (int(self.image.get_width()-self.tileSize*2), int(self.image.get_height()-self.tileSize*2)))
self.baseImage.convert_alpha()
self.image.blit(self.baseImage, (0, 0))
self.renderText()
self.image.convert_alpha()
def renderText(self):
self.image.blit(self.rendText.getCurrent(), self.rendText.pos)
def refresh(self):
self.image = self.baseImage
self.renderText()
def update(self):
now = pygame.time.get_ticks()
if len(self.rendText.images) > 1:
if checkKey("interact") and now-self.lastInteract > 200:
if self.rendText.index+1 >= len(self.rendText.images):
self.finished = True
else:
self.rendText.index += 1
self.refresh()
self.lastInteract = now
else:
self.finished = True
class dText:
def __init__(self, fNum, text, color, aalias=True, pos=(0, 0), size=(900, 600), bgColor=(0, 0, 0, 0)):
## This code is thanks to https://stackoverflow.com/questions/42014195/rendering-text-with-multiple-lines-in-pygame.
self.render(fNum, text, color, aalias, pos, size, bgColor)
self.pos = pygame.Vector2(pos)
self.rect = (self.pos.x, self.pos.y, size[0], size[1])
for img in self.images:
img.convert_alpha()
def getCurrent(self):
return self.images[self.index]
def render(self, fNum, text, color, aalias=True, pos=(0, 0), size=(900, 600), bgColor=(0, 0, 0, 0)):
self.images = [pygame.Surface(size, pygame.SRCALPHA)]
self.index = 0
self.getCurrent().fill(bgColor)
font = fonts[fNum]
words = [word.split(' ') for word in text.splitlines()] # 2D array where each row is a list of words.
space = font.size(' ')[0] # The width of a space.
max_width, max_height = size
x, y = 0, 0
for line in words:
for word in line:
if word != '':
word_surface = font.render(word, aalias, color)
word_width, word_height = word_surface.get_size()
if x + word_width >= max_width:
x = 0 # Reset the x.
y += word_height # Start on new row.
if y > max_height-word_height:
x, y = 0, 0
self.getCurrent().convert_alpha()
self.index += 1
self.images.append(pygame.Surface(size, pygame.SRCALPHA))
else:
self.getCurrent().blit(word_surface, (x, y))
x += word_width + space
self.index = 0
def __str__(self):
return self.image