-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
296 lines (233 loc) · 10.1 KB
/
main.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#TODO: allow a text file to be dropped onto the game; use that text
import pygame, random, sys
from pygame.locals import *
from code_patterns import *
from itertools import cycle
TAPLENGTH = 90
class Game:
WINDOWWIDTH = 1200
WINDOWHEIGHT = 80
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (10, 255, 10)
RED = (255, 10, 10)
BACKGROUNDCOLOR = WHITE
TEXTCOLOR = BLACK
FPS = 60
ADDNEWBADDIERATE = 150
BADDIESPEED = -1
BADDIESIZE = 40
def __init__(self):
pygame.init()
pygame.display.set_caption('S.A.M.urai')
pygame.mouse.set_visible(False)
self.playerImage = [pygame.image.load('resources/adventurer-run3-0{}.png'.format(i)) for i in range(6)]
self.playerRect = self.playerImage[0].get_rect()
self.playerRect.bottom = self.WINDOWHEIGHT - 2
self.baddieAddCounter = 0
self.baddies = []
self.font = pygame.font.SysFont(None, 72)
#self.codePatterns = MorseCodePatterns()
self.codePatterns = TapCodePatterns()
self.score = 0
def playerHasHitBaddie(self, player, baddies):
return any(player.colliderect(b.get('rect')) for b in baddies)
def terminate(self):
pygame.quit()
sys.exit()
def spawnBaddie(self, baddies):
newBaddie = {
'rect': pygame.Rect(
self.WINDOWWIDTH,
(self.WINDOWHEIGHT - self.BADDIESIZE) - 2,
self.BADDIESIZE,
self.BADDIESIZE
),
'character': random.choice(list(self.codePatterns.getAlphabet()))
}
newBaddie['surface'] = self.font.render(newBaddie['character'], 1, self.TEXTCOLOR)
self.baddies.append(newBaddie)
def killBaddie(self, baddy):
self.baddies.remove(baddy)
def drawText(self, text, surface, x, y):
font = pygame.font.SysFont(None, 24)
textobj = font.render(text, 1, self.TEXTCOLOR)
textrect = textobj.get_rect()
textrect.topleft = (x, y)
surface.blit(textobj, textrect)
def fireWeapon(self, attackSequence):
if len(attackSequence) > 1:
rectifiedSequence = self.rectifySequence(attackSequence)
attackedCharacterList = self.codePatterns.getCharacter(rectifiedSequence)
if len(self.baddies) > 0:
if self.baddies[0]['character'] in attackedCharacterList:
self.killBaddie(self.baddies[0])
self.score += len(rectifiedSequence)
else:
self.score -= len(rectifiedSequence)
def drawPlayer(self, windowSurface):
playerIndex = 0
numImages = len(self.playerImage)
framesPerImage = 4
while True:
windowSurface.blit(self.playerImage[(playerIndex // framesPerImage) % numImages], self.playerRect)
playerIndex = (playerIndex + 1) % (framesPerImage * numImages)
yield
def drawBaddies(self, windowSurface):
for b in self.baddies:
windowSurface.blit(b['surface'], b['rect'])
def drawAttackSequence(self, windowSurface, attackSequence):
#font = pygame.font.SysFont(None, 36)
#x = 1
#y = 1
#self.drawText(' '.join(attackSequence), font, windowSurface, x, y)
#self.drawConcentricCircles(windowSurface, attackSequence)
#self.drawStackedBar(windowSurface, attackSequence)
self.drawDashes(windowSurface, attackSequence)
def drawPower(self, windowSurface, sequence):
drawConcentricCircles(windowSurface, sequence)
drawStackedBar(windowSurface, sequence)
drawDashes(windowSurface, sequence)
def drawStackedBar(self, windowSurface, sequence):
width = 5
radius = 10
windowHeight = windowSurface.get_height()
y = windowHeight
surface = pygame.Surface((width, windowHeight))
surface.fill(self.BACKGROUNDCOLOR)
colorCycle = cycle([self.GREEN, self.RED])
for i in range(0, len(sequence), 2):
if len(sequence) > i + 1:
color = next(colorCycle)
keyed = sequence[i+1] // TAPLENGTH + 1
height = radius * keyed
pygame.draw.rect(surface, color, (0, y - height, width, height))
y -= height + 1
windowSurface.blit(surface, (0, 0))
def drawConcentricCircles(self, windowSurface, sequence):
radiusInc = 3
circles = []
windowWidth = windowSurface.get_width()
windowHeight = windowSurface.get_height()
surface = pygame.Surface((windowWidth, windowHeight))
surface.fill(self.BACKGROUNDCOLOR)
radius = 0
for i in range(0, len(sequence), 2):
if len(sequence) > i + 1:
keyed = sequence[i+1] // TAPLENGTH + 1
width = radiusInc * keyed
circles.append(radius + width)
radius += width
colorCycle = cycle([self.GREEN, self.RED])
if len(circles) % 2 == 0:
circles.append(0)
for c in circles[::-1]:
color = next(colorCycle)
pygame.draw.circle(surface, color, (windowWidth // 2, windowHeight // 2), c)
windowSurface.blit(surface, (0, 0))
def drawDashes(self, windowSurface, sequence):
# These are fixed, should be moved out of the function
y = radius = xStart = xEnd = 5
surface = pygame.Surface((windowSurface.get_width(), 2*radius))
surface.fill(self.BACKGROUNDCOLOR)
for i in range(0, len(sequence), 2):
gap = sequence[i] // TAPLENGTH
xStart = xEnd + radius * 2 + radius * gap
if len(sequence) > i + 1:
keyed = sequence[i+1] // TAPLENGTH
width = radius * keyed
xEnd = xStart + width
# Start dot
pygame.draw.circle(surface, self.GREEN, (xStart, y), radius)
# Middle rect
pygame.draw.rect(surface, self.GREEN, (xStart, y - radius, width, radius * 2))
# End dot
pygame.draw.circle(surface, self.GREEN, (xEnd, y), radius)
windowSurface.blit(surface, (0, 20))
def handleEvents(self):
keying = False
timingSequence = [0]
gapStartTime = keyStartTime = pygame.time.get_ticks()
while True:
now = pygame.time.get_ticks()
for event in pygame.event.get():
if self.isTerminateEvent(event):
self.terminate()
if self.isFireEvent(event) and not keying:
keyStartTime = now
timingSequence.append(0)
keying = True
if self.isCeaseFireEvent(event) and keying:
gapStartTime = now
timingSequence.append(0)
keying = False
if keying:
timingSequence[-1] = now - keyStartTime
else:
timingSequence[-1] = now - gapStartTime
if timingSequence[-1] > self.codePatterns.getSymbolDefinition('LETTERSPACE').get('minDuration') * TAPLENGTH:
self.fireWeapon(timingSequence)
timingSequence = [0]
yield timingSequence
#if event.key == K_m:
# if musicPlaying:
# pygame.mixer.music.stop()
# else:
# pygame.mixer.music.play(-1, 0.0)
# musicPlaying = not musicPlaying
# if event.type == MOUSEBUTTONUP:
# foods.append(pygame.Rect(event.pos[0] - FOODSIZE // 2, event.pos[1] - FOODSIZE // 2, FOODSIZE, FOODSIZE))
def isTerminateEvent(self, event):
return event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE)
def isFireEvent(self, event):
return event.type in (MOUSEBUTTONDOWN, FINGERDOWN) or (event.type == KEYDOWN and event.key == K_SPACE)
def isCeaseFireEvent(self, event):
return event.type in (MOUSEBUTTONUP, FINGERUP) or (event.type == KEYUP and event.key == K_SPACE)
def drawScore(self, windowSurface, score, topScore):
self.drawText('Score: %s' % (score), windowSurface, 10, 0)
#self.drawText('Top Score: %s' % (topScore), windowSurface, 10, 20)
pass
def rectifySequence(self, sequence):
rectifiedSequence = []
step = 1 if self.codePatterns.keepInternalSpaces else 2
for i in range(1, len(sequence) - 1, step):
tap = sequence[i]
symbol = self.codePatterns.getSymbol(i % 2 == 1, tap)
if symbol != None:
rectifiedSequence.append(symbol)
#print(rectifiedSequence)
return rectifiedSequence
def playGame(self):
windowSurface = pygame.display.set_mode((self.WINDOWWIDTH, self.WINDOWHEIGHT))
mainClock = pygame.time.Clock()
drawPlayerGen = self.drawPlayer(windowSurface)
eventHandler = self.handleEvents()
topScore = 0
self.attackSequence = []
while True:
if len(self.baddies) == 0:
self.spawnBaddie(self.baddies)
self.BADDIESPEED -= 0.1
rawSequence = next(eventHandler)
for b in self.baddies:
b['rect'].move_ip(self.BADDIESPEED, 0)
for b in self.baddies[:]:
if b['rect'].left < 0:
self.killBaddie(b)
windowSurface.fill(self.BACKGROUNDCOLOR)
self.drawAttackSequence(windowSurface, rawSequence)
next(drawPlayerGen)
self.drawBaddies(windowSurface)
self.drawScore(windowSurface, self.score, topScore)
pygame.display.update()
if self.playerHasHitBaddie(self.playerRect, self.baddies):
topScore = max(topScore, self.score)
break
mainClock.tick(self.FPS)
self.drawText('You died.', windowSurface, 600, 30)
pygame.display.update()
while True:
next(eventHandler)
if __name__ == "__main__":
g = Game()
g.playGame()