-
Notifications
You must be signed in to change notification settings - Fork 1
/
grid.py
70 lines (60 loc) · 2.54 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
import pygame
from colors import Colors
#create grid size and format for game pieces
class Grid:
def __init__(self):
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)]
#call from the Colors class
self.colors = Colors.get_cell_colors()
#used to print grid and values
def print_grid(self):
for row in range(self.num_cols):
for column in range(self.num_cols):
print(self.grid[row][column], end = " ")
print()
def is_inside(self, row, column):
if row >= 0 and row < self.num_rows and column >= 0 and column < self.num_cols:
return True
return False
def is_empty(self, row, column):
if self.grid[row][column] == 0:
return True
return False
#checks if row is complete to delete it
def is_row_full(self, row):
for column in range(self.num_cols):
if self.grid[row][column] == 0:
return False
return True
def clear_row(self, row):
for column in range(self.num_cols):
self.grid[row][column] = 0
#moves incomplete rows down when completed rows are deleted
def move_row_down(self, row, num_rows):
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):
completed = 0
for row in range(self.num_rows-1, 0, -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):
for row in range(self.num_rows):
for column in range(self.num_cols):
self.grid[row][column] = 0
#drawing grid/ assigning values
def draw(self, screen):
for row in range(self.num_rows):
for column in range(self.num_cols):
cell_value = self.grid[row][column]
#creating cell of the grid (x, y, w, h), edit cells to have game be 29 pixels
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)