-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.py
101 lines (71 loc) · 2.83 KB
/
board.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
import data
import pygame
class Board:
def __init__(self, dimensions=(10,20)):
self.width, self.height = dimensions
self.data = None
self.clear()
def is_open(self, xi, yi):
return self.is_within(xi, yi) and (self.get_cell(xi, yi) == None)
def is_within(self, xi, yi):
return (0 <= xi < self.width) and (0 <= yi < self.height)
def does_hitbox_collide(self, hitbox):
for xi, yi in hitbox:
if not self.is_open(xi, yi):
return True
return False
def get_cell(self, xi, yi):
return self.data[yi][xi]
def set_cell(self, xi, yi, value):
self.data[yi][xi] = value
def clear_complete(self):
# remove all rows that are full,
# while also keeping track of that count
replace_count = 0
for i, row in reversed(list(enumerate(self.data))):
if all(row):
self.data.pop(i)
replace_count += 1
# add the blank rows back to the top
for _ in range(replace_count):
self.data.insert(0, [None for _ in range(self.width)])
# return the amount of lines cleared
return replace_count
def lock_piece(self, piece):
for xi, yi in piece.get_hitbox():
self.set_cell(xi, yi, piece.color)
def draw_grid(self, window, location, dimensions):
x, y = location
w, h = dimensions
cw = w / self.width
ch = h / self.height
# verticals
for xi in range(1,self.width):
cx = x + xi * cw
pygame.draw.line(window, data.middleground_color, (cx,y), (cx,y+h))
# horizontals
for yi in range(1,self.height):
cy = y + yi * ch
pygame.draw.line(window, data.middleground_color, (x,cy), (x+w,cy))
def draw_border(self, window, location, dimensions):
x, y = location
w, h = dimensions
pygame.draw.rect(window, data.foreground_color, pygame.Rect(x,y,w,h), data.border_width)
def draw(self, window, location, dimensions):
argument_tuple = (window, location, dimensions)
x, y = location
w, h = dimensions
cw = w / self.width
ch = h / self.height
# draw locked pieces
for xi in range(self.width):
for yi in range(self.height):
# get cell color
cell_color = self.get_cell(xi, yi)
# if the piece exists (ie. there is a color present)
if cell_color != None:
cx = x + xi * cw
cy = y + yi * ch
pygame.draw.rect(window, cell_color, pygame.Rect(cx, cy, cw+1, ch+1))
def clear(self):
self.data = [[None for _ in range(self.width)] for _ in range(self.height)]