-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamnesia.py
344 lines (285 loc) · 11.5 KB
/
amnesia.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import random, pygame, sys
from pygame.locals import *
FPS = 30 # frames per second, the general speed of the program
WINDOWWIDTH = 800 # size of window's width in pixels
WINDOWHEIGHT = 500 # size of windows' height in pixels
REVEALSPEED = 8 # speed boxes' sliding reveals and covers
BOXSIZE = 40 # size of box height & width in pixels
GAPSIZE = 10 # size of gap between boxes in pixels
BOARDWIDTH = 8 # number of columns of icons
BOARDHEIGHT = 8 # number of rows of icons
XMARGIN = int((WINDOWWIDTH - (BOARDWIDTH * (BOXSIZE + GAPSIZE))) / 2) - 100
YMARGIN = int((WINDOWHEIGHT - (BOARDHEIGHT * (BOXSIZE + GAPSIZE))) / 2)
# R G B
BLUE = ( 0, 0, 255)
CYAN = ( 0, 255, 255)
GRAY = (100, 100, 100)
GREEN = ( 0, 255, 0)
NAVYBLUE = ( 60, 60, 100)
ORANGE = (255, 128, 0)
PURPLE = (255, 0, 255)
RED = (255, 0, 0)
WHITE = (255, 255, 255)
YELLOW = (255, 255, 0)
box_colour = (255, 255, 255)
BGCOLOUR = NAVYBLUE
LIGHTBGCOLOUR = GRAY
HIGHLIGHTCOLOUR = BLUE
OVAL = 'oval'
ALLCOLORS = (RED, GREEN, BLUE, YELLOW)
ALLSHAPES = (OVAL)
MATCHSCORES = (2, 4, 8, 16, 32, 64, 128)
LEVELSCORES = (0, 20, 40, 160, 640, 2560, 10240)
LOTTO = (0, 0, 0, 0.04, 0.10, 0.18, 0.28)
MAXLEVEL = 7
curr_score = 0
curr_lv = 1
available_flips = 32
#EACH TIME COMPLETE A MATCH, AVAILFLIP += min(2^LV, 15)
revealed = []
pattern = 2
def main():
global FPSCLOCK, DISPLAYSURF, revealed, curr_score, curr_lv, pattern, box_colour, available_flips
pygame.init()
FPSCLOCK = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
mousex = 0 # x coordinate of mouse
mousey = 0 # y coordinate of mouse
pygame.display.set_caption('Memory Overflow')
mainBoard = getRandomizedBoard()
revealedBoxes = generateRevealedBoxesData(False)
firstSelection = None # stores the (x, y) of the first box clicked.
DISPLAYSURF.fill(BGCOLOUR)
while True: # main game loop
mouseClicked = False
DISPLAYSURF.fill(BGCOLOUR) # drawing the window
drawStatus(curr_score, curr_lv, available_flips)
drawBoard(mainBoard, revealedBoxes)
for event in pygame.event.get(): # event handling loop
if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE):
pygame.quit()
sys.exit()
elif event.type == MOUSEMOTION:
mousex, mousey = event.pos
elif event.type == MOUSEBUTTONUP:
mousex, mousey = event.pos
mouseClicked = True
# my secret cheat key before flash!
elif event.type == KEYDOWN:
key = pygame.key.get_pressed()
if key[pygame.K_h]:
#remember to print screen...
hint(mainBoard, 5000)
elif key[pygame.K_9]:
available_flips += 9
#
boxx, boxy = getBoxAtPixel(mousex, mousey)
if boxx != None and boxy != None:
# The mouse is currently over a box.
if not revealedBoxes[boxx][boxy]:
drawHighlightBox(boxx, boxy)
if not revealedBoxes[boxx][boxy] and mouseClicked:
revealBoxesAnimation(mainBoard, [(boxx, boxy)])
revealedBoxes[boxx][boxy] = True # set the box as "revealed"
available_flips -= 1
if available_flips <= 0:
gameLostAnimation(available_flips)
pygame.quit()
sys.exit()
if firstSelection == None: # the current box was the first box clicked
firstSelection = (boxx, boxy)
revealed.append(firstSelection)
else: # the current box was the second box clicked
# Check if there is a match between the two icons.
colour1 = getColour(mainBoard, firstSelection[0], firstSelection[1])
colour2 = getColour(mainBoard, boxx, boxy)
# icons don't match
if colour1 != colour2:
pygame.time.wait(1000)
revealed.append((boxx, boxy))
coverBoxesAnimation(mainBoard, revealed)
for box in revealed:
revealedBoxes[box[0]][box[1]] = False
revealed = []
firstSelection = None
# not fulfilling pattern
elif len(revealed)+1 < pattern:
revealed.append((boxx, boxy))
# has fulfilled pattern
else:
revealed.append((boxx,boxy))
completeMatch()
for box in revealed:
revealedBoxes[box[0]][box[1]] = False
refillBox(mainBoard)
firstSelection = None
# Redraw the screen and wait a clock tick.
pygame.display.update()
FPSCLOCK.tick(FPS)
def generateRevealedBoxesData(val):
revealedBoxes = []
for i in range(BOARDWIDTH):
revealedBoxes.append([val] * BOARDHEIGHT)
return revealedBoxes
def getRandomizedBoard():
icons = []
for colour in ALLCOLORS:
icons.append(colour)
# Create the board data structure, with randomly placed icons.
board = []
for x in range(BOARDWIDTH):
column = []
for y in range(BOARDHEIGHT):
imax = len(icons) - 1
i = random.randint(0, imax)
column.append(icons[i])
board.append(column)
return board
def leftTopCoordsOfBox(boxx, boxy):
# Convert board coordinates to pixel coordinates
left = boxx * (BOXSIZE + GAPSIZE) + XMARGIN
top = boxy * (BOXSIZE + GAPSIZE) + YMARGIN
return (left, top)
def getBoxAtPixel(x, y):
for boxx in range(BOARDWIDTH):
for boxy in range(BOARDHEIGHT):
left, top = leftTopCoordsOfBox(boxx, boxy)
boxRect = pygame.Rect(left, top, BOXSIZE, BOXSIZE)
if boxRect.collidepoint(x, y):
return (boxx, boxy)
return (None, None)
def drawIcon(colour, boxx, boxy):
quarter = int(BOXSIZE * 0.25)
half = int(BOXSIZE * 0.5)
left, top = leftTopCoordsOfBox(boxx, boxy) # get pixel coords from board coords
pygame.draw.ellipse(DISPLAYSURF, colour, (left, top + quarter, BOXSIZE, half))
def getColour(board, boxx, boxy):
return board[boxx][boxy]
def drawBoxCovers(board, boxes, coverage):
# Draws boxes being covered/revealed. "boxes" is a list
# of two-item lists, which have the x & y spot of the box.
for box in boxes:
left, top = leftTopCoordsOfBox(box[0], box[1])
pygame.draw.rect(DISPLAYSURF, BGCOLOUR, (left, top, BOXSIZE, BOXSIZE))
colour = getColour(board, box[0], box[1])
drawIcon(colour, box[0], box[1])
if coverage > 0: # only draw the cover if there is an coverage
pygame.draw.rect(DISPLAYSURF, box_colour, (left, top, coverage, BOXSIZE))
pygame.display.update()
FPSCLOCK.tick(FPS)
def revealBoxesAnimation(board, boxesToReveal):
# Do the "box reveal" animation.
for coverage in range(BOXSIZE, (-REVEALSPEED) - 1, - REVEALSPEED):
drawBoxCovers(board, boxesToReveal, coverage)
def coverBoxesAnimation(board, boxesToCover):
# Do the "box cover" animation.
for coverage in range(0, BOXSIZE + REVEALSPEED, REVEALSPEED):
drawBoxCovers(board, boxesToCover, coverage)
def drawBoard(board, revealed):
# Draws all of the boxes in their covered or revealed state.
for boxx in range(BOARDWIDTH):
for boxy in range(BOARDHEIGHT):
left, top = leftTopCoordsOfBox(boxx, boxy)
if not revealed[boxx][boxy]:
# Draw a covered box.
pygame.draw.rect(DISPLAYSURF, box_colour, (left, top, BOXSIZE, BOXSIZE))
else:
# Draw the (revealed) icon.
colour = getColour(board, boxx, boxy)
drawIcon(colour, boxx, boxy)
def drawHighlightBox(boxx, boxy):
left, top = leftTopCoordsOfBox(boxx, boxy)
pygame.draw.rect(DISPLAYSURF, HIGHLIGHTCOLOUR, (left - 5, top - 5, BOXSIZE + 10, BOXSIZE + 10), 4)
def refillBox(board):
global revealed
coverBoxesAnimation(board, revealed)
icons = []
for colour in ALLCOLORS:
icons.append(colour)
for box in revealed:
imax = len(icons) - 1
i = random.randint(0, imax)
x = box[0]
y = box[1]
board[x][y] = icons[i]
revealed = []
def levelUp():
global curr_lv, pattern, box_colour, available_flips
curr_lv += 1
pattern += 1
available_flips += 32
b = 255-40*(curr_lv-1)
g = 255-40*(curr_lv-1)
box_colour = (255, b, g)
#reveal all squares for 1 sec, only possible after level 6 (5 same flips)
def hint(board, time):
boxes = []
for boxx in range(BOARDWIDTH):
for boxy in range(BOARDHEIGHT):
boxes.append((boxx, boxy))
revealBoxesAnimation(board, boxes)
pygame.time.wait(time)
coverBoxesAnimation(board, boxes)
def completeMatch():
global curr_score, available_flips
curr_score += MATCHSCORES[curr_lv - 1]
if (curr_lv != MAXLEVEL) and (curr_score >= LEVELSCORES[curr_lv]):
levelUp()
new_flips = min(2**curr_lv, 25)
available_flips += new_flips
def drawStatus(score, level, flipsLeft):
global DISPLAYSURF
status_font = pygame.font.Font('freesansbold.ttf', 18)
# draw the score text
scoreSurf = status_font.render('Score: %s' % score, True, WHITE)
scoreRect = scoreSurf.get_rect()
scoreRect.topleft = (WINDOWWIDTH - 150, 20)
DISPLAYSURF.blit(scoreSurf, scoreRect)
# draw the level text
levelSurf = status_font.render('Level: %s' % level, True, WHITE)
levelRect = levelSurf.get_rect()
levelRect.topleft = (WINDOWWIDTH - 150, 50)
DISPLAYSURF.blit(levelSurf, levelRect)
# draw the flips-left text
levelSurf = status_font.render('Flips left: %s' % flipsLeft, True, WHITE)
levelRect = levelSurf.get_rect()
levelRect.topleft = (WINDOWWIDTH - 150, 80)
DISPLAYSURF.blit(levelSurf, levelRect)
def gameLostAnimation(flipsLeft):
global DISPLAYSURF
status_font = pygame.font.Font('freesansbold.ttf', 18)
# draw the flips-left text
i = 3
while (i>0):
levelSurf = status_font.render('Flips left: %s' % flipsLeft, True, RED)
levelRect = levelSurf.get_rect()
levelRect.topleft = (WINDOWWIDTH - 150, 80)
DISPLAYSURF.blit(levelSurf, levelRect)
pygame.display.update()
FPSCLOCK.tick(FPS)
pygame.time.wait(300)
levelSurf = status_font.render('Flips left: %s' % flipsLeft, True, WHITE)
levelRect = levelSurf.get_rect()
levelRect.topleft = (WINDOWWIDTH - 150, 80)
DISPLAYSURF.blit(levelSurf, levelRect)
pygame.display.update()
FPSCLOCK.tick(FPS)
pygame.time.wait(300)
i -= 1
pygame.time.wait(1000)
def winFlips(numberOfFlips):
global available_flips
available_flips += numberOfFlips
def drawLotto():
ticket = random.randint(1, 100) * LOTTO[curr_lv-1]
winning = random.randint(1, 100)
if (ticket > winning):
winType = random.randint(1, 3)
if (winType == 1):
hint(5000)
elif (winType == 2):
hint(3000)
elif (winType == 3):
winFlips(10+curr_lv*2)
if __name__ == '__main__':
main()