-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.py
132 lines (111 loc) · 3.79 KB
/
grid.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
import pygame
from colors import Colors
class Grid:
"""
Class representing the game grid.
Attributes:
num_rows (int): Number of rows in the grid.
num_cols (int): Number of columns in the grid.
cell_size (int): Size of each cell in the grid.
grid (list): 2D list representing the grid.
colors (list): List of colors for the cells.
"""
def __init__(self):
"""
Initialize a Grid instance.
"""
self.num_rows = 20
self.num_cols = 10
self.cell_size = 30
self.grid = [[0 for j in range(self.num_cols)] for i in range(self.num_rows)]
self.colors = Colors.get_cell_colors()
def print_grid(self):
"""
Print the grid to the console.
"""
for row in range(self.num_rows):
for column in range(self.num_cols):
print(self.grid[row][column], end=" ")
print()
def is_inside(self, row, column):
"""
Check if a position is inside the grid.
Args:
row (int): Row position.
column (int): Column position.
Returns:
bool: True if inside the grid, False otherwise.
"""
return 0 <= row < self.num_rows and 0 <= column < self.num_cols
def is_empty(self, row, column):
"""
Check if a cell is empty.
Args:
row (int): Row position.
column (int): Column position.
Returns:
bool: True if the cell is empty, False otherwise.
"""
return self.grid[row][column] == 0
def is_row_full(self, row):
"""
Check if a row is full.
Args:
row (int): Row index.
Returns:
bool: True if the row is full, False otherwise.
"""
return all(self.grid[row][column] != 0 for column in range(self.num_cols))
def clear_row(self, row):
"""
Clear a row by setting all its cells to 0.
Args:
row (int): Row index.
"""
for column in range(self.num_cols):
self.grid[row][column] = 0
def move_row_down(self, row, num_rows):
"""
Move a row down by a given number of rows.
Args:
row (int): Row index.
num_rows (int): Number of rows to move down.
"""
for column in range(self.num_cols):
self.grid[row + num_rows][column] = self.grid[row][column]
self.grid[row][column] = 0
def clear_full_rows(self):
"""
Clear all full rows and move the above rows down.
Returns:
int: Number of rows cleared.
"""
completed = 0
for row in range(self.num_rows - 1, -1, -1):
if self.is_row_full(row):
self.clear_row(row)
completed += 1
elif completed > 0:
self.move_row_down(row, completed)
return completed
def reset(self):
"""
Reset the grid by clearing all cells.
"""
for row in range(self.num_rows):
for column in range(self.num_cols):
self.grid[row][column] = 0
def draw(self, screen):
"""
Draw the grid on the screen.
Args:
screen (pygame.Surface): The surface to draw on.
"""
for row in range(self.num_rows):
for column in range(self.num_cols):
cell_value = self.grid[row][column]
cell_rect = pygame.Rect(column * self.cell_size + 11, row * self.cell_size + 11,
self.cell_size - 1, self.cell_size - 1)
pygame.draw.rect(screen, self.colors[cell_value], cell_rect)
# Draw the grid lines
pygame.draw.rect(screen, Colors.black, cell_rect, 1)