forked from rschwa6308/Arithma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.py
392 lines (329 loc) · 15 KB
/
Game.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# math puzzler
import pygame
from pygame import *
from random import *
from Classes import *
# from Funcs import *
from Check import *
from Levels import *
from Menu import *
# takes a list of pieces and grid coordinates and returns piece instance
def get_piece(pieces, coords):
for piece in pieces:
if piece.grid == coords:
return piece
return False
# takes a level number and returns a list of pieces
def load_level(level_num):
pieces = []
for p in Levels[level_num]: # load in level
if isinstance(p[0], str) and p[0][0] == "N":
size = int(p[0][1:])
pieces.append(NestedPiece(size, p[1], locked=p[1][0] < 10))
else:
pieces.append(Piece(p[0], p[1], locked=p[1][0] < 10))
return pieces
def overlay(screen, buttons, level):
# box
box = pygame.Surface((300, 400))
box.fill(white)
# border
pygame.draw.rect(box, grid_color, pygame.Rect(0, 0, 299, 399), 2)
# heading
head_img = piece_font.render("Correct!", True, grid_color)
box.blit(head_img,
(box.get_width() / 2 - head_img.get_width() / 2, box.get_height() / 2 - head_img.get_height() / 2 - 150))
# message
mess_img_0 = credits_font.render("You beat level " + str(level + 1) + ".", True, grid_color)
mess_img_1 = credits_font.render("Great work!", True, grid_color)
box.blit(mess_img_0, (
box.get_width() / 2 - mess_img_0.get_width() / 2, box.get_height() / 2 - mess_img_0.get_height() / 2 - 75))
box.blit(mess_img_1, (
box.get_width() / 2 - mess_img_1.get_width() / 2, box.get_height() / 2 - mess_img_1.get_height() / 2 - 50))
# buttons
for button in buttons:
box.blit(button.get_image(), (button.x, button.y))
# blit box to screen
screen.blit(box, (250, 100))
# flip display
pygame.display.update()
def pop_up(screen, buttons, level):
###pop up window###
buttons = [
Button(20, 190, 260, 50, grid_color, piece_color, button_font, "Next Level", "N"),
Button(20, 260, 260, 50, grid_color, piece_color, button_font, "Replay", "R"),
Button(20, 330, 260, 50, grid_color, piece_color, button_font, "Home", "H")
]
overlay(screen, buttons, level)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
# down button
elif event.type == pygame.MOUSEBUTTONDOWN:
for button in buttons:
if button.rect.collidepoint(
(event.pos[0] - 250, event.pos[1] - 100)): # compensate for offset origin
button.hover = 3
overlay(screen, buttons, level)
# up button
elif event.type == pygame.MOUSEBUTTONUP:
for button in buttons:
if button.hover == 3: # include 'button.rect.collidepoint(event.pos)' if cursor must be over button to activate
button.hover = 0
overlay(screen, buttons, level)
# programmatic control
if button.key == "N":
main(screen, level + 1)
return
elif button.key == "R":
# print("restart")
main(screen, level)
return
elif button.key == "H":
return
# hover shading
pos = (pygame.mouse.get_pos()[0] - 250,
pygame.mouse.get_pos()[1] - 100) # compensate for offset origin
for button in buttons:
if button.rect.collidepoint(pos) and button.hover == 0:
button.hover = 1
overlay(screen, buttons, level)
elif not button.rect.collidepoint(
pos) and button.hover == 1: # will only update screen when necessary
button.hover = 0
overlay(screen, buttons, level)
def reset_pieces(screen, pieces, selected, buttons, level):
for piece in pieces:
if isinstance(piece, NestedPiece):
explode(pieces, piece)
display(screen, pieces, selected, buttons)
for piece in pieces:
delta_x = piece.starting_pos[0] - piece.pos[0]
delta_y = piece.starting_pos[1] - piece.pos[1]
steps = int((delta_x ** 2 + delta_y ** 2) ** 0.5 / 10)
for _ in range(steps):
piece.pos = (piece.pos[0] + delta_x / steps, piece.pos[1] + delta_y / steps)
display(screen, pieces, selected, buttons)
pygame.time.wait(1)
piece.grid = piece.starting_grid
display(screen, pieces, selected, buttons)
def flash_red(screen, pieces, selected, buttons, button):
# flash red
for i in range(2):
button.color = red
button.txt_color = red
button.bg = background_color
display(screen, pieces, selected, buttons)
pygame.time.wait(100)
button.color = grid_color
button.txt_color = piece_color
button.bg = red
display(screen, pieces, selected, buttons)
pygame.time.wait(100)
button.bg = white
button.color = grid_color
button.txt_color = piece_color
display(screen, pieces, selected, buttons)
def explode(pieces, nested):
for piece in nested.contents:
if piece is not None:
nested.remove(piece)
pieces.append(piece)
attempt_place(pieces, nested.grid, piece)
nested.contents = [None for _ in range(nested.size)]
def attempt_place(pieces, pos, piece):
# print(pos, piece)
delta_x = 1
delta_y = 0
tries = 0
while get_piece(pieces, (pos[0] + delta_x, pos[1] + delta_y)) != False or (
pos[0] + delta_x > 12 or pos[1] + delta_y > 9 or (
pos[0] + delta_x >= 10 and pos[1] + delta_y >= 8)):
delta_x = randint(-1, 1)
delta_y = randint(-1, 1)
tries += 1
if tries > 100:
print("could not find a spot!")
break
if tries < 100:
piece.grid = (pos[0] + delta_x, pos[1] + delta_y)
piece.pos = piece.get_pos()
def display(screen, pieces, selected, buttons):
width = screen.get_width()
height = screen.get_height()
g_width = (width - 200) / 10
g_height = height / 10
# clear screen
screen.fill(background_color)
grid_colors_hor = [
(175, 175, 175),
(175, 175, 175),
(50, 50, 50)
]
grid_colors_vert = [
(125, 125, 125),
(150, 150, 150),
(0, 0, 0)
]
# draw grid
for y in range(10):
pygame.draw.line(screen, grid_colors_hor[0], (0, y * g_height - 1), (width - 200, y * g_height - 1))
pygame.draw.line(screen, grid_colors_hor[2], (0, y * g_height + 1), (width - 200, y * g_height + 1))
for x in range(10):
pygame.draw.line(screen, grid_colors_vert[0], (x * g_width - 1, 0), (x * g_width - 1, height))
pygame.draw.line(screen, grid_colors_vert[2], (x * g_width + 1, 0), (x * g_width + 1, height))
for y in range(10):
pygame.draw.line(screen, grid_colors_hor[1], (0, y * g_height), (width - 200, y * g_height))
for x in range(10):
pygame.draw.line(screen, grid_colors_vert[1], (x * g_width, 0), (x * g_width, height))
# draw border and partitions
pygame.draw.rect(screen, grid_color, pygame.Rect(0, 0, width - 1, height - 1), 4)
pygame.draw.line(screen, grid_color, (width - 200, 0), (width - 200, height), 2)
# pygame.draw.line(screen, grid_color, (600,480), (800,480), 2)
# sort pieces for proper rendering order
pieces.sort(key=lambda p: 10 * p.grid[1] + p.grid[0])
if selected:
pieces.remove(selected) # move selected to end of pieces list so it is rendered last (on top)
pieces.append(selected)
positions_used = []
# draw pieces
for piece in pieces:
# print piece.get_pos()
n = positions_used.count(piece.grid)
if n == 0:
screen.blit(piece.image, piece.pos)
else:
offset_x = 5 # the ratio of these two values determines the slope at which the stack renders
offset_y = 5 # slope should match that of tile source image
draw_x, draw_y = piece.pos[0] - offset_x * n, piece.pos[1] - offset_y * n
screen.blit(piece.image, (draw_x, draw_y))
# # draw lines to separate pieces (only *necessary* if offset < tile height)
pygame.draw.line(screen, (220, 220, 220), (draw_x + offset_x, draw_y + 50), (draw_x + 50, draw_y + 50), 1)
pygame.draw.line(screen, (140, 140, 140), (draw_x + 50, draw_y + 50), (draw_x + 50, draw_y + offset_y), 1)
positions_used.append(piece.grid)
# draw buttons
for button in buttons:
screen.blit(button.get_image(), (button.x, button.y))
# flip display
pygame.display.update()
# main() takes screen and a level index
def main(screen, level):
# set up screen
s_w = 800
s_h = 600
# set window caption
pygame.display.set_caption("Level " + str(level + 1))
# init pieces (build from level)
# init buttons
buttons = [
Button(615, 545, 50, 50, grid_color, piece_color, button_font, "", "C", check_image),
Button(675, 545, 50, 50, grid_color, piece_color, button_font, "", "R", reset_image),
Button(735, 545, 50, 50, grid_color, piece_color, button_font, "", "H", home_image)
]
pieces = load_level(level)
# init selected var
selected = False
# show board
display(screen, pieces, selected, buttons)
# set up clock
clock = pygame.time.Clock()
# game loop
done = False
while not done:
clock.tick(60)
# user input
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
done = True
# run check algorithm
if event.type == KEYDOWN:
if event.key == K_RETURN:
if check_scrabble(pieces):
# print("success!!!")
pop_up(screen, buttons, level)
return
else:
# print("failure!!!")
pass
# select (pick up) piece
if event.type == MOUSEBUTTONDOWN:
pos = mouse.get_pos()
# decide grid location based on whether on board or rack
if pos[0] > 600:
pos = (int((pos[0] - 10) / 60), int(pos[1] / 60))
else:
pos = (int(pos[0] / 60), int(pos[1] / 60))
clicked = get_piece(pieces, pos)
if event.button == 1:
if clicked and not clicked.locked: # if clicked is not None and piece is not locked
selected = clicked
# detect button press
for button in buttons:
if button.rect.collidepoint(event.pos):
button.hover = 3
display(screen, pieces, selected, buttons)
elif event.button == 3:
if clicked and not clicked.locked:
if isinstance(clicked, NestedPiece):
explode(pieces, clicked)
display(screen, pieces, selected, buttons)
else:
pass
# deselect (place) piece
if event.type == MOUSEBUTTONUP:
if event.button == 1:
pos = mouse.get_pos()
pos = (int(pos[0] / 60), int(pos[1] / 60))
if selected:
if pos[0] > 12 or pos[1] > 9 or (
pos[0] >= 10 and pos[1] >= 9): # check if outside acceptable range
pass
elif get_piece(pieces, pos) == False or get_piece(pieces, pos) == selected:
selected.grid = pos
elif get_piece(pieces, pos).data == selected.data and pos[0] >= 10: # tile stacking in tray
selected.grid = pos
# insert into nested piece
elif isinstance(get_piece(pieces, pos), NestedPiece) and \
None in get_piece(pieces, pos).contents:
# print("nesting yay!")
get_piece(pieces, pos).insert(selected)
pieces.remove(selected) # temporary
else:
attempt_place(pieces, pos, selected)
selected.pos = selected.get_pos()
selected = False
display(screen, pieces, selected, buttons)
# button execution
for button in buttons:
if button.hover == 3: # include 'button.rect.collidepoint(event.pos)' if cursor must be over button to activate
button.hover = 0
display(screen, pieces, selected, buttons)
if button.key == "C":
if check_scrabble(pieces):
# print("success!!!")
pop_up(screen, buttons, level)
return
else:
# print("failure!!!")
flash_red(screen, pieces, selected, buttons, button)
elif button.key == "R":
reset_pieces(screen, pieces, selected, buttons, level)
pygame.time.wait(100) # allow for reset animation to finish (asynchronous?)
pieces = load_level(level)
elif button.key == "H":
return
# update pos of selected piece
if selected != False:
selected.pos = (mouse.get_pos()[0] - 25, mouse.get_pos()[1] - 25)
display(screen, pieces, selected, buttons)
# hover shading
pos = pygame.mouse.get_pos()
for button in buttons:
if button.rect.collidepoint(pos) and button.hover == 0:
button.hover = 1
display(screen, pieces, selected, buttons)
elif not button.rect.collidepoint(pos) and button.hover == 1: # will only update screen when necessary
button.hover = 0
display(screen, pieces, selected, buttons)