Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
365 lines (325 loc) · 12.8 KB
/
Copy pathmain.py
File metadata and controls
365 lines (325 loc) · 12.8 KB
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
import pygame, sys
import random
from constructor import Builder
pygame.init()
bdr = Builder()
display = Builder.Display()
Builder.all_figures(bdr.xgridB, bdr.ygridB)
frameSurf = pygame.Surface(bdr.frameSize)
upc_Blocks_Surf = pygame.Surface((160, 520))
score_Surf = pygame.Surface((150, 250))
tetrisLogo = pygame.image.load('tetris-logo.png')
tetrisLogo = pygame.transform.scale(tetrisLogo, (220, 90))
# Global variables
color_ind = 0
block_color = bdr.fig_colors[color_ind]
font = pygame.font.SysFont('halkduster.ttf', 28)
figures = [Builder.ele, Builder.eleInv, Builder.te, Builder.square, Builder.zed, Builder.zedInv, Builder.line]
upc_shapes = []
upc_shapes_rev = []
upc_shapes_rots = []
# Initializing figures
def gen_upc_shapes():
global upc_shapes, rot_choices, figures, upc_shapes_rots
while len(upc_shapes) < 4:
choice = random.choice(range(len(figures)))
fig_choice = figures[choice]
rotation = random.choice([*fig_choice])
fig_shape = fig_choice[rotation]
upc_shapes.insert(0, [rotation, fig_shape])
upc_shapes_rev.append([rotation, fig_shape])
upc_shapes_rots.append(fig_choice)
def select_from_upc(upc_shapes):
global figure
figure = {}
if upc_shapes:
next_fig = upc_shapes.pop()
rotation = next_fig[0]
shape = next_fig[1]
figure[rotation] = shape
gen_upc_shapes()
return rotation, shape
else:
gen_upc_shapes()
return select_from_upc(upc_shapes)
rotation, shape = select_from_upc(upc_shapes)
fig_choice = upc_shapes_rots.pop(0)
# figure = figures[1]
# rotation = 'eleInvRot0'
# print(rotation)
# shape = figure[rotation]
def rotations(fig_choice, rotation):
index = int(rotation[-1])
if fig_choice.keys() in [Builder.zed.keys(), Builder.zedInv.keys(), Builder.line.keys()]:
if index == 0:
new_index = index + 1
else:
new_index = 0
else:
if index < 3:
new_index = index + 1
else:
new_index = 0
new_rotation = rotation[:-1] + str(new_index)
new_shape = fig_choice[new_rotation]
return new_shape, new_rotation
def blocks_plot(shape):
xpos = []
ypos = []
figBlocks = []
for coords in shape:
blockID = bdr.gen_FigID()
blockID = pygame.Surface(bdr.blockSize)
xpos.append(bdr.invxgrid[coords[0]])
ypos.append(bdr.invygrid[coords[1]])
figBlocks.append(blockID)
bdr.block_count = 0
return figBlocks, xpos, ypos
def find_bounds():
bounds = {}
for k, v in bdr.main_shape.items():
bounds[k] = []
for i in range(len(v)):
if v[i] == 1:
bounds[k] += [i]
return bounds
figBlocks, xpos, ypos = blocks_plot(shape)
clock = pygame.time.Clock()
blocksCount, bubbleDown, speedDown, holdKeyLeft, holdKeyRight, time, currenttime = 0, 0, .96, 0, 0, 0, 2
xspan = 0
running = True
bubble = True
strikeLineTimer = 0
# Starting main game loop _______________________________________
score = font.render("Score: ", True, bdr.light_green, 16)
lines = font.render("Lines: ", True, bdr.light_green, 16)
time_since = font.render("Timer: ", True, bdr.light_green, 16)
timer_counter = 0
score_value = 250
score_update = [250*x for x in range(9,300) if x in [i**2 for i in range(200)]]
while running:
# Timer
milli = clock.tick()
seconds = milli / 1000.
time += seconds
timer_counter += round(seconds, 2)
timer_counter = round(timer_counter, 2)
if bdr.lines * score_value in score_update:
score_value += score_value
score_n = font.render(str(bdr.lines * score_value), True, bdr.light_blue, 16)
lines_n = font.render(str(bdr.lines), True, bdr.light_blue, 16)
time_since_n = font.render(str(timer_counter), True, bdr.light_blue, 16)
yspan = min(ypos)
# Display refesh
display.fill(bdr.disp_color)
display.blit(frameSurf, (170, 100))
display.blit(tetrisLogo, (250, 10))
frameSurf.fill(bdr.main_color)
display.blit(upc_Blocks_Surf, (520, 200))
upc_Blocks_Surf.fill(bdr.main_color)
display.blit(score_Surf, (10, 300))
display.blit(score, (13, 310))
display.blit(score_n, (70, 340))
display.blit(lines, (13, 400))
display.blit(lines_n, (70, 430))
display.blit(time_since, (13, 480))
display.blit(time_since_n, (70, 510))
# Plotting upcoming shapes
upc_xgrid = {272: 540, 306: 570, 340: 600, 374: 625, 408: 670}
upc_ygrid = {100: 210, 135: 240, 170: 270, 205: 300}
y_increment = 0
count = 0
for i in range(len(upc_shapes)):
ind = len(upc_shapes) - 1
ind -= i
ls = upc_shapes[ind]
for coords in ls[1:]:
if count >= 1:
y_increment += len(coords) * (30)
count += 1
for x, y in coords:
y = upc_ygrid[y] + y_increment
x = upc_xgrid[x]
upcShape = pygame.Surface((30, 30))
upcShape.fill(bdr.orange)
display.blit(upcShape, [x, y])
y_increment += 10
# Colors for saved blocks
saved_blocks_coors = {}
for key, values in bdr.main_shape.items():
if int(key) <= 19:
for i in range(len(values)):
if bdr.colored_blocks[key][i] != 0:
color = bdr.fig_colors_dic[bdr.colored_blocks[key][i]]
x_color = bdr.xgridB[i]
y_color = bdr.ygridB[int(key)]
saved_blocks_coors[x_color, y_color] = color
for item, color in saved_blocks_coors.items():
x_color = item[0]
y_color = item[1]
active_block = pygame.Surface(bdr.blockSize)
active_block.fill(color)
display.blit(active_block, [x_color, y_color])
# print(saved_blocks_coors.values())
# Plotting active figure
for block, x, y in zip(figBlocks, xpos, ypos):
display.blit(block, [bdr.xgridB[x], bdr.ygridB[y]])
display.blit(block, [bdr.xgridB[x], bdr.ygridB[y]])
block_color = bdr.fig_colors[color_ind]
block.fill(block_color)
# Find all side bounds
bounds = find_bounds()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# Keystrokes
if event.type == pygame.KEYDOWN and not any([bdr.pressed_keys["left"], bdr.pressed_keys["right"]]):
if event.key == pygame.K_LEFT and min(xpos) >= 1:
bdr.pressed_keys["left"] = True
move_left = []
if x + 1 > 0:
left_xpos = [x - 1 for x in xpos]
for j, x in enumerate(left_xpos):
if x in bounds[str(ypos[j])]:
move_left.append(False)
else:
move_left.append(True)
if all(move_left):
xpos = left_xpos
xspan -= 1
if event.key == pygame.K_RIGHT and max(xpos) <= 8:
move_right = []
if x + 1 <= 9:
right_xpos = [x + 1 for x in xpos]
for j, x in enumerate(right_xpos):
if x in bounds[str(ypos[j])]:
move_right.append(False)
else:
move_right.append(True)
if all(move_right):
xpos = right_xpos
xspan += 1
bdr.pressed_keys["right"] = True
if event.key == pygame.K_DOWN:
bdr.pressed_keys["down"] = True
if event.key == pygame.K_UP:
if 'squareRot0' not in figure.keys():
#Rotating the figure
shape, rotation = rotations(fig_choice, rotation)
figBlocks, xposTemp, yposTemp = blocks_plot(shape)
bounds = find_bounds()
canRotate = []
new_xpos = [x + xspan for x in xposTemp]
new_ypos = [y + yspan for y in yposTemp]
for x, y in zip(new_xpos, new_ypos):
if x in bounds[str(y)]:
canRotate.append(False)
else:
canRotate.append(True)
if all(canRotate):
if max(new_xpos) <= 9 and min(new_xpos) >= 1:
xpos = new_xpos
ypos = new_ypos
elif max(new_xpos) >= 9:
diff = max(new_xpos) - 9
xpos = [x - diff for x in new_xpos]
ypos = new_ypos
elif min(new_xpos) < 1:
diff = abs(min(new_xpos))
xpos = [x + diff for x in new_xpos]
ypos = new_ypos
else:
bdr.pressed_keys["right"] = False
bdr.pressed_keys["left"] = False
bdr.pressed_keys["down"] = False
# Timer in between side move keystroke and holding key
holdKeyRight = -5
holdKeyLeft = -5
# Holding key
if bdr.pressed_keys["left"] and min(xpos) >= 1:
holdKeyLeft += .3
move_left = []
if holdKeyLeft >= 5:
if x + 1 > 0:
left_xpos = [x - 1 for x in xpos]
for j, x in enumerate(left_xpos):
if x in bounds[str(ypos[j])]:
move_left.append(False)
else:
move_left.append(True)
if all(move_left):
xpos = left_xpos
xspan -= 1
holdKeyLeft = 0
if bdr.pressed_keys["right"] and max(xpos) <= 8:
holdKeyRight += .3
move_right = []
if holdKeyRight >= 5:
if x + 1 <= 9:
right_xpos = [x + 1 for x in xpos]
for j, x in enumerate(right_xpos):
if x in bounds[str(ypos[j])]:
move_right.append(False)
else:
move_right.append(True)
if all(move_right):
xpos = right_xpos
xspan += 1
holdKeyRight = 0
if bdr.pressed_keys["down"]:
bubbleDown += speedDown
if bubbleDown >= 20:
ypos = [y + 1 for y in ypos]
bubbleDown = 0
# Adding flash color to filled line strike
# if len(bdr.strike) > 0:®
# ('ACTIVE')
# strikeLineTimer = 0
# for i, num in enumerate(bdr.comp):
# while strikeLineTimer < num:
# for line_strike in bdr.strike:
# strike_block = pygame.Surface(bdr.blockSize)
# strike_block.fill(bdr.white)
# for x in range(10):
# display.blit(strike_block, [bdr.xgridB[x], bdr.ygridB[line_strike]])
# strikeLineTimer += 1
# bdr.strike = []
for x, y in zip(xpos, ypos):
if bdr.main_shape[str(y+1)][x] == 1:
#Saving current color and its coords
curr_color = [color for color, color_code in bdr.fig_colors_dic.items() if color_code == block_color]
curr_color = '.'.join(map(str, curr_color))
bdr.blocks_meta(xpos,ypos, curr_color)
rotation, shape = select_from_upc(upc_shapes)
fig_choice = upc_shapes_rots.pop(0)
if color_ind < len(bdr.fig_colors)-1:
color_ind += 1
block_color = bdr.fig_colors[color_ind]
else:
color_ind = 0
figBlocks, xpos, ypos = blocks_plot(shape)
xspan = 0
break
if currenttime <= time:
# print(bdr.settled_blocks)
currenttime += .5
ypos = [y + 1 for y in ypos]
bounds = find_bounds()
# Create Grid
for i in range(20):
hline = i
if i <= 10:
vline = i
pygame.draw.line(display, bdr.gridc, (bdr.wideGrid[vline], 100), (bdr.wideGrid[vline], 800), 2)
pygame.draw.line(display, bdr.gridc, (170, bdr.tallGrid[hline]), (510, bdr.tallGrid[hline]), 2)
# Bound lines for game play
pygame.draw.line(display, bdr.surf_outline, (170, 100 ), (510, 100), 3)
pygame.draw.line(display, bdr.surf_outline, (170, 800), (510, 800), 3)
pygame.draw.line(display, bdr.surf_outline, (170, 100), (170, 800), 3)
pygame.draw.line(display, bdr.surf_outline, (510, 100), (510, 800), 3)
# Bound lines for upc_shapes
pygame.draw.line(display, bdr.surf_outline, (520, 200), (520, 720), 3)
pygame.draw.line(display, bdr.surf_outline, (680, 200), (680, 720), 3)
pygame.draw.line(display, bdr.surf_outline, (520, 200), (680, 200), 3)
pygame.draw.line(display, bdr.surf_outline, (520, 720), (680, 720), 3)
pygame.display.update()