-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtactris.py
342 lines (277 loc) · 10.2 KB
/
tactris.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
import pygame
import random
import copy
import time
# Define constants
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREY = (100, 100, 150)
LGREY = (215, 215, 230)
WIDTH = 40
HEIGHT = 40
MARGIN = 1
# Define all types of blocks
blocks = [
[[0, 0], [1, 0], [2, 0], [3, 0]], # I shape
[[0, 0], [0, 1], [0, 2], [0, 3]], # I horizontal shape
[[0, 0], [0, 1], [0, 2], [1, 2]], # L shape
[[0, 0], [1, 0], [2, 0], [0, 1]], # L horizontal shape
[[0, 0], [0, 1], [0, 2], [1, 0]], # J shape
[[0, 0], [1, 0], [2, 0], [2, 1]], # J horizontal shape
[[0, 0], [0, 1], [1, 1], [1, 0]], # O shape
[[0, 0], [0, 1], [1, 1], [1, 2]], # S shape
[[0, 1], [0, 2], [1, 0], [1, 1]], # Z shape
[[0, 0], [0, 1], [0, 2], [1, 1]], # T shape
]
def build_grid():
# Grid containing blocks the player has placed
# The function generates an empty grid
global grid, prev_grid
grid = []
for row in range(10):
grid.append([(255, 255, 255)] * 10)
prev_grid = copy.deepcopy(grid)
def build_ghost_grid():
# Grid showing where the current_block will be placed on mouse click
# The function generates an empty grid
global ghost_grid
ghost_grid = []
for row in range(10):
ghost_grid.append([(255, 255, 255)] * 10)
def generate_block():
# When called, returns a random block
return random.choice(blocks)
def can_place_block(block, position, grid):
# Check if a block can be placed at the mouse cursor
for b in block:
row = position[0] + b[0]
col = position[1] + b[1]
if row < 0 or row >= 10 or col < 0 or col >= 10:
return False
if grid[row][col] == 1:
return False
return True
def add_block(block, position, grid):
# Place current_block at the mouse cursor
global prev_grid, prev_score
prev_grid = copy.deepcopy(grid)
prev_score = copy.deepcopy(score)
for b in block:
row = position[0] + b[0]
col = position[1] + b[1]
grid[row][col] = 1
def upd_ghost_grid(current_block, position):
# Draw current_block at the mouse cursor
# This helps player seeing where the block will be place on mouse click
global ghost_grid
build_ghost_grid()
for b in current_block:
row = position[0] + b[0]
col = position[1] + b[1]
ghost_grid[row][col] = 1
def upd_screen():
# Refresh the screen to reflect any changes
for row in range(10):
for col in range(10):
color = WHITE
if ghost_grid[row][col] == 1:
color = LGREY
if grid[row][col] == 1:
color = GREY
board_cell = pygame.Rect((MARGIN + WIDTH) * col + MARGIN, \
(MARGIN + HEIGHT) * row + MARGIN, WIDTH, HEIGHT)
pygame.draw.rect(screen, color, board_cell)
def draw_ghost_block(block, position, grid, ghost_grid):
# Check if mouse cursor has been moved to another grid cell
# Moves the ghost_block when mouse is moved
global prev_position
try:
if position != prev_position:
upd_ghost_grid(current_block, position)
# Update the grid
upd_screen()
# Update the screen
pygame.display.flip()
prev_position = copy.deepcopy(position)
except IndexError:
pass
def clear_lines(grid):
# Clear lines when they are fully filled in
global score
full_rows = []
for row in range(len(grid)):
is_full = True
for col in range(len(grid)):
if grid[row][col] == (255, 255, 255):
is_full = False
break
if is_full:
full_rows.append(row)
full_cols = []
for col in range(len(grid[0])):
is_full = True
for row in range(len(grid)):
if grid[row][col] == (255, 255, 255):
is_full = False
break
if is_full:
full_cols.append(col)
for row in full_rows:
grid.pop(row)
grid.insert(0, [(255, 255, 255)] * len(grid[0]))
score += 10
for col in full_cols:
for row in range(len(grid)):
del grid[row][col]
for row in range(len(grid)):
grid[row].insert(0, (255, 255, 255))
score += 10
def reset_game():
# Reset the board, score and generate new blocks
global score, prev_score, grid, current_block, prev_block, next_block
print("Game over. Score: " + str(score))
current_block = generate_block()
prev_block = copy.deepcopy(current_block)
next_block = generate_block()
score = 0
prev_score = 0
build_grid()
pygame.display.flip()
def undo_step():
# Remove previously placed block
global prev_grid, grid, score, current_block, prev_block
grid = copy.deepcopy(prev_grid)
score = copy.deepcopy(prev_score)
current_block = copy.deepcopy(prev_block)
pygame.display.flip()
# Initialize Pygame
pygame.init()
# Set the height and width of the screen
WINDOW_SIZE = [(MARGIN + WIDTH) * 20 + MARGIN, (MARGIN + HEIGHT) * 10 + MARGIN]
screen = pygame.display.set_mode(WINDOW_SIZE)
# Set the title of the window
pygame.display.set_caption("Tactris")
# Initialize the grid
build_grid()
build_ghost_grid()
# Initialize the current block and position
current_block = generate_block()
prev_block = copy.deepcopy(current_block)
next_block = generate_block()
current_position = [1, 10]
next_position = [1, 15]
# Initialize the score
score = 0
prev_score = copy.deepcopy(score)
# Set up buttons
font = pygame.font.Font(None, 36)
font_size = 24
font = pygame.font.SysFont(None, font_size)
text_color = (255, 255, 255)
button_width = 140
button_height = 60
button_color = (0, 255, 0)
# geme reset button
reset_button_x = WINDOW_SIZE[0] - 200
reset_button_y = WINDOW_SIZE[1] - 80
reset_button_text = "RESET"
reset_button_rect = pygame.Rect(
reset_button_x, reset_button_y, button_width, button_height)
# undo step button
undo_button_x = WINDOW_SIZE[0] - 350
undo_button_y = WINDOW_SIZE[1] - 80
undo_button_text = "UNDO"
undo_button_rect = pygame.Rect(
undo_button_x, undo_button_y, button_width, button_height)
# Loop until the user clicks the close button
done = False
# Set the clock
clock = pygame.time.Clock()
# Previous cursor position
prev_position = []
# Loop until the game is over
while not done:
# Draw the grid
screen.fill(BLACK)
for row in range(10):
for col in range(10):
color = WHITE
if ghost_grid[row][col] == 1:
color = LGREY
if grid[row][col] == 1:
color = GREY
board_cell = pygame.Rect((MARGIN + WIDTH) * col + MARGIN, \
(MARGIN + HEIGHT) * row + MARGIN, WIDTH, HEIGHT)
pygame.draw.rect(screen, color, board_cell)
# Draw the current block
cur_text = font.render("Current: ", True, WHITE)
screen.blit(cur_text, [WINDOW_SIZE[0] - 400, WINDOW_SIZE[1] - 400])
for b in current_block:
row = current_position[0] + b[0]
col = current_position[1] + b[1]
pygame.draw.rect(screen, WHITE, [(MARGIN + WIDTH) * col + MARGIN + 5, \
(MARGIN + HEIGHT) * row + MARGIN, WIDTH, HEIGHT])
# Draw the next block
next_text = font.render("Next: " , True, WHITE)
screen.blit(next_text, [WINDOW_SIZE[0] - 200, WINDOW_SIZE[1] - 400])
for b in next_block:
row = next_position[0] + b[0]
col = next_position[1] + b[1]
pygame.draw.rect(screen, WHITE, [(MARGIN + WIDTH) * col + MARGIN + 5, \
(MARGIN + HEIGHT) * row + MARGIN, WIDTH, HEIGHT])
# Draw the score
score_text = font.render("Score: " + str(score), True, WHITE)
screen.blit(score_text, [WINDOW_SIZE[0] - 400, WINDOW_SIZE[1] - 120])
# Draw the game reset button
pygame.draw.rect(screen, button_color, reset_button_rect)
pygame.draw.rect(screen, BLACK, reset_button_rect.inflate(10, 10))
pygame.draw.rect(screen, WHITE, reset_button_rect, 2)
text_surface = font.render(reset_button_text, True, text_color)
text_rect = text_surface.get_rect(center=reset_button_rect.center)
reset_text = font.render("RESET", True, WHITE)
reset_button = screen.blit(reset_text, text_rect)
# Draw undo step button
pygame.draw.rect(screen, button_color, undo_button_rect)
pygame.draw.rect(screen, BLACK, undo_button_rect.inflate(10, 10))
pygame.draw.rect(screen, WHITE, undo_button_rect, 2)
text_surface = font.render(undo_button_text, True, text_color)
text_rect = text_surface.get_rect(center=undo_button_rect.center)
reset_text = font.render("UNDO", True, WHITE)
reset_button = screen.blit(reset_text, text_rect)
# Update the screen
pygame.display.flip()
# Wait for the next frame
clock.tick(60)
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# Hanle mouse cursor movement across the game grid
mouse_position = pygame.mouse.get_pos()
mx, my = pygame.mouse.get_pos()
if mx <= 410 and my <= 410:
row = mouse_position[1] // (HEIGHT + MARGIN)
col = mouse_position[0] // (WIDTH + MARGIN)
draw_ghost_block(current_block, [row, col], grid, ghost_grid)
# Handle mouse button clicks
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
mouse_position = pygame.mouse.get_pos()
# Check if reset button was clicked
if reset_button_rect.collidepoint(event.pos):
reset_game()
# Check if undo button was clicked
if undo_button_rect.collidepoint(event.pos):
undo_step()
# Calculate the grid position of the mouse click
row = mouse_position[1] // (HEIGHT + MARGIN)
col = mouse_position[0] // (WIDTH + MARGIN)
# Check if the current block can be placed at the clicked position
if can_place_block(current_block, [row, col], grid):
add_block(current_block, [row, col], grid)
clear_lines(grid)
prev_block = copy.deepcopy(current_block)
current_block = next_block
next_block = generate_block()
score += 4
# Quit Pygame
pygame.quit()