This repository was archived by the owner on Aug 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcuatris.py
More file actions
459 lines (393 loc) · 14.8 KB
/
Copy pathcuatris.py
File metadata and controls
459 lines (393 loc) · 14.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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# -*- coding: utf-8 -*-
import pygame
import random
class Piece:
"""The model of a piece"""
def __init__(self, matrix, display, square_side):
self.matrix = matrix
self.rows = 0
self.columns = 0
self.display = display
self.square_side = square_side
self.x = 0
self.y = 0
self.compute_size()
def compute_size(self):
"""Computes the dimensions of the piece, in rows and columns"""
self.rows = len(self.matrix)
self.columns = len(self.matrix[0])
def rotate90(self):
"""Turns a piece 90 degrees, clockwise"""
self.matrix = list(zip(*self.matrix[::-1]))
self.compute_size()
def rotate_minus_90(self):
"""Turns a piece 90 degrees, counterclockwise"""
self.matrix = list(zip(*self.matrix))[::-1]
self.compute_size()
def _draw_square(self, x, y, color):
"""Draws a square in the given (row, columns -- not pixels)
coordinates"""
square = (
x * self.square_side,
y * self.square_side,
self.square_side,
self.square_side,
)
inner_square = (
x * self.square_side + 8,
y * self.square_side + 8,
self.square_side - 16,
self.square_side - 16,
)
pygame.draw.rect(
self.display,
(int(color[0] / 2), int(color[1] / 2), int(color[2] / 2)),
square,
0,
)
pygame.draw.rect(self.display, color, inner_square, 0)
def _delete_rectangle(self, rectangle):
"""Deletes the specified area"""
pygame.draw.rect(self.display, Cuatris.colors[0], rectangle, 0)
def draw(self, x_ini, y_ini):
"""Draws the specified piece from the coordinates (rows, columns
--not pixels) x_ini, y_ini"""
# update the position of the piece
self.x = x_ini
self.y = y_ini
for i in range(0, len(self.matrix)):
for j in range(0, len(self.matrix[i])):
if self.matrix[i][j] != 0:
self._draw_square(
x_ini + j, y_ini + i, Cuatris.colors[self.matrix[i][j]]
)
def delete(self, x_ini, y_ini):
"""Deletes the specified piece from the coordinates (rows, columns
--not pixels) x_ini, y_ini"""
for i in range(0, len(self.matrix)):
for j in range(0, len(self.matrix[i])):
if self.matrix[i][j] != 0:
self._draw_square(x_ini + j, y_ini + i, Cuatris.colors[0])
# Piece movement
def move(self, delta_x, delta_y):
"""Moves the piece the given delta, expressed in rows and columns
--not pixels"""
self.x = self.x + delta_x
self.y = self.y + delta_y
# Piece status
def self_draw(self):
"""Draws the piece in its coordinates"""
self.draw(self.x, self.y)
def self_delete(self):
"""Deletes the piece from its coordinates"""
self.delete(self.x, self.y)
def is_colliding_with(self, other):
"""Checks for the collision of this piece with other"""
for i in range(0, len(self.matrix)):
for j in range(0, len(self.matrix[i])):
if self.matrix[i][j] != 0:
if other.matrix[self.y + i][self.x + j] != 0:
return True
return False
class Container(Piece):
"""Container where the game takes place"""
def _remove_row(self, row):
"""removes the given row, adding a new one on the top of the
Container"""
for row in range(row, 0, -1):
self.matrix[row] = self.matrix[row - 1]
self.matrix[0] = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
def count_lines(self, other_piece):
"""checks how many lines are completed with a new piece"""
lines = 0
for row in range(other_piece.y, other_piece.y + other_piece.rows):
product = 1
for value in self.matrix[row]:
product = product * value
if product == 0:
break
if product > 0:
lines = lines + 1
self._remove_row(row)
return lines
def incorporate(self, other_piece):
"""incorporates the other piece\'s matrix to the Container, returning
the number of completed rows --this is to be used when a piece
collides with the bottom of the container"""
for i in range(0, len(other_piece.matrix)):
for j in range(0, len(other_piece.matrix[i])):
if other_piece.matrix[i][j] != 0:
self.matrix[other_piece.y + i][
other_piece.x + j
] = other_piece.matrix[i][j]
return self.count_lines(other_piece)
class Cuatris:
"""Cuatris Object: implements game logic"""
# Color definitions
WHITE = (255, 255, 255)
GRAY = (127, 127, 127)
BLACK = (0, 0, 0)
PURPLE = (200, 0, 200)
GREEN = (0, 200, 0)
RED = (200, 0, 0)
BLUE = (0, 0, 200)
YELLOW = (200, 200, 0)
CYAN = (0, 200, 200)
ORANGE = (255, 102, 16)
colors = [BLACK, GRAY, CYAN, BLUE, ORANGE, GREEN, RED, PURPLE, YELLOW]
# These are the matrix for the different pieces of the game. The numbers
# will index the color in the previous array, so that then it is easy for
# the game to draw it.
matrix_L = [(7, 0), (7, 0), (7, 7)]
matrix_J = [(0, 8), (0, 8), (8, 8)]
matrix_I = [(6, 0), (6, 0), (6, 0), (6, 0)]
matrix_S = [(0, 2, 2), (2, 2, 0)]
matrix_Z = [(4, 4, 0), (0, 4, 4)]
matrix_T = [(5, 5, 5), (0, 5, 0)]
matrix_O = [(3, 3), (3, 3)]
matrices = [
0,
0,
matrix_L,
matrix_J,
matrix_I,
matrix_S,
matrix_Z,
matrix_T,
matrix_O,
]
next_matrix = [
[1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1],
]
# Frames per Second
FPS = 60
# Gravity Event
GRAVITY = pygame.USEREVENT + 1
# Initial coordinates for the pieces
x_ini = 4
y_ini = 0
def __init__(self, square_side):
try:
self.slot = int(square_side)
except ValueError:
# Default value
self.slot = 40
self.lines = 0
self.lines_for_next_level = 20
self.level = 0
self.width = 19 * self.slot
self.height = 22 * self.slot
pygame.init()
# display definition
self.display = pygame.display.set_mode((self.width, self.height))
self.display.fill(Cuatris.BLACK)
matrix_container = [
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
]
self.next = Container(Cuatris.next_matrix, self.display, self.slot)
self.next.draw(13, 0)
self.container = Container(matrix_container, self.display, self.slot)
self.container.draw(0, 0)
self.piece = self.get_next_piece()
self.piece.draw(4, 0)
self.next_piece = self.get_next_piece()
self.next_piece.draw(15, 1)
pygame.display.update()
# Game clock
self.clock = pygame.time.Clock()
# Gravity Event
self.t_FALL = 70
self.t_GRAVITY = 2000 # milliseconds
pygame.time.set_timer(Cuatris.GRAVITY, self.t_GRAVITY)
def get_next_piece(self):
"""Creates a random next piece"""
n_piece = random.randint(2, 8)
piece = Piece(self.matrices[n_piece], self.display, self.slot)
return piece
def _print(self, font, string, left, top, color, background):
"""Prints a string"""
text = font.render(string, True, color, background)
text_rectangle = text.get_rect()
text_rectangle.left = left
text_rectangle.top = top
self.display.blit(text, text_rectangle)
def score(self):
"""Prints the score"""
basic_font = pygame.font.SysFont(None, self.slot)
self._print(
basic_font,
"Lines: " + str(self.lines),
13 * self.slot,
8 * self.slot,
Cuatris.WHITE,
Cuatris.BLACK,
)
self._print(
basic_font,
"Level: " + str(self.level),
13 * self.slot,
9 * self.slot,
Cuatris.GRAY,
Cuatris.BLACK,
)
self._print(
basic_font,
"Next: " + str(self.lines_for_next_level),
13 * self.slot,
10 * self.slot,
Cuatris.GRAY,
Cuatris.BLACK,
)
def game_over(self):
"""Prompts the user for a next game / quit"""
basic_font = pygame.font.SysFont(None, self.slot) # to refactor
left = self.display.get_rect().centerx - len("GAME O") * self.slot
top = self.display.get_rect().centery
self._print(
basic_font, "GAME OVER", left, top, Cuatris.RED, Cuatris.GRAY
)
self._print(
basic_font,
"RETURN TO START OVER",
left,
top + self.slot,
Cuatris.RED,
Cuatris.GRAY,
)
@staticmethod
def turn(counterclockwise, piece, container):
"""Turns a piece"""
piece.self_delete()
if counterclockwise:
piece.rotate_minus_90()
else:
piece.rotate90()
while piece.is_colliding_with(container):
piece.move(-1, 0)
piece.self_draw()
@staticmethod
def move_right(units, piece, container):
"""Moves a piece the given units (squares) to the right"""
piece.self_delete()
piece.move(units, 0)
if piece.is_colliding_with(container):
piece.move(-units, 0)
piece.self_draw()
def _process_gravity(self):
"""This method makes the current piece fall 1 square, and checks for
the consequences of it"""
game_should_continue = True
# Move a piece one square downwards
self.piece.self_delete()
self.piece.move(0, 1)
if self.piece.is_colliding_with(self.container):
# Reset gravity in case user was pressing down arrow
pygame.time.set_timer(Cuatris.GRAVITY, self.t_GRAVITY)
# Back one square (collision implies overlapping)
self.piece.move(0, -1)
# Redraw container adding the colliding piece
self.container.self_delete()
self.lines = self.lines + self.container.incorporate(self.piece)
# Check if the user advanced levels
if self.lines >= self.lines_for_next_level:
self.level = self.level + 1
self.lines_for_next_level = self.lines_for_next_level + 20
if self.t_GRAVITY > 100:
self.t_GRAVITY = self.t_GRAVITY - 1000
# Update score, spawn a new piece and calculate and redraw next
# piece
self.score()
self.container.self_draw()
self.next_piece.self_delete()
self.piece = self.next_piece
self.piece.draw(4, 0)
self.next_piece = self.get_next_piece()
self.next_piece.draw(15, 1)
# If the new piece is colliding upon spawn, game over
if self.piece.is_colliding_with(self.container):
game_should_continue = False
else:
self.piece.self_draw()
return game_should_continue
def _play_again(self):
"""Process the user response to the question of whether he/she wants to
play again. Return returns True to play again; any other key returns
False to exit the game."""
while True:
# Pause until next tick
self.clock.tick(Cuatris.FPS)
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
return True
else:
return False
def run_game(self):
"""Game and Event loop"""
is_continue = True
self.score()
# Game loop
while is_continue:
# Pause until next tick
self.clock.tick(Cuatris.FPS)
# Event Loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
return False
else:
# Process gravity
if event.type == Cuatris.GRAVITY:
is_continue = self._process_gravity()
if event.type == pygame.KEYDOWN:
# Piece turning can cause collisions to the right
if event.key == pygame.K_z:
self.turn(True, self.piece, self.container)
if event.key == pygame.K_x:
self.turn(False, self.piece, self.container)
if event.key == pygame.K_LEFT:
self.move_right(-1, self.piece, self.container)
if event.key == pygame.K_RIGHT:
self.move_right(1, self.piece, self.container)
# accelerate gravity
if event.key == pygame.K_DOWN:
pygame.time.set_timer(Cuatris.GRAVITY, self.t_FALL)
# Release any key means fall speed back to gravity
if event.type == pygame.KEYUP:
pygame.time.set_timer(Cuatris.GRAVITY, self.t_GRAVITY)
if not is_continue:
self.game_over()
pygame.display.update()
return self._play_again()
# This code executes the game.
play = True
while play:
game = Cuatris(30)
play = game.run_game()
print("Thanks for playing!")
pygame.quit()