-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.py
48 lines (41 loc) · 1.4 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
import numpy as np
import cv2
class Board(object):
"""window or page or board game"""
def __init__(self, size=[(0, 0), (60, 80)], title='Simple Snake', bg=0, fg=1):
# board size
self.size = size
# board title
self.title = title
# background color
self.bg = bg
# forground color
self.fg = fg
# - create board array(numpy zeros) (60, 80)
self.board = np.zeros(self.size[1], np.uint8)
# background set
self.clear()
# clear board with background
def clear(self):
# - Clear all items with bg
self.board[:] = self.bg
# show board with title (scale x)
def show(self, scale=10):
h , w = self.size[1]
# show board
cv2.namedWindow(self.title, cv2.WINDOW_NORMAL)
cv2.resizeWindow(self.title, w*scale, h*scale)
cv2.imshow(self.title, self.board)
# draw a point in board
def draw(self, positions, is_draw):
# chenge color to gray scale
color = self.fg if is_draw else self.bg
if type(positions) == list:
for position in positions:
self.board[position] = color
else:
self.board[positions] = color
# return free sections in board
def free(self, revert=False):
free_block = self.fg if revert else self.bg
return list(zip(*np.where(self.board == free_block)))