-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.py
83 lines (63 loc) · 2.29 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
"""
This module is for the manipulation of the grid
"""
import pygame
import constants
class Grid:
"""Make the grid on the SCREEN"""
def __init__(self):
self.NUM_ROWS = 20
self.NUM_COLS = 10
self.CELL_SIZE = 30
self.grid = [[0 for _ in range(self.NUM_COLS)] for _ in range(self.NUM_ROWS)]
self.colors = constants.COLORS
def print_grid(self):
"""Print the grid on the console"""
return print('\n'.join(' '.join(map(str, ans)) for ans in self.grid))
def draw(self, SCREEN):
"""Draw the grid on window"""
for row in range(self.NUM_ROWS):
for col in range(self.NUM_COLS):
cell_value = self.grid[row][col]
cell_rect = pygame.Rect(
self.CELL_SIZE * col + 1,
self.CELL_SIZE * row + 1,
self.CELL_SIZE - 1,
self.CELL_SIZE - 1
)
pygame.draw.rect(SCREEN, self.colors[cell_value], cell_rect)
def is_empty(self, row, col):
"""check if a cell[r][c] is empty"""
if not self.grid[row][col]:
return True
return False
def is_inside(self, row, col):
"""Is the block inside the grid ?"""
if 0<=row<self.NUM_ROWS and 0<=col<self.NUM_COLS:
return True
return False
def is_row_full(self, row):
"""Check if one row is full"""
for col in range(self.NUM_COLS):
if self.grid[row][col]==0:
return False
return True
def clear_row(self, row):
"""clear a full row"""
for col in range(self.NUM_COLS):
self.grid[row][col] = 0
def move_row_down(self, row, num_rows):
"""move row down by the number of full rows"""
for col in range(self.NUM_COLS):
self.grid[row+num_rows][col] = self.grid[row][col]
self.grid[row][col] = 0
def clear_full_rows(self):
"""Combine check full rows, clear rows and move down"""
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