-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtetris_core.py
More file actions
60 lines (50 loc) · 2.17 KB
/
tetris_core.py
File metadata and controls
60 lines (50 loc) · 2.17 KB
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
from enum import Enum
class PxType(Enum):
free = 0
forrbidden = 1
placed = 2
moving = 3
class TetrisCore(object):
BOARD_MARGIN = 2
def __init__(self, x_size, y_size):
self.x_size = x_size
self.y_size = y_size
self.game_board = [[0 for x in range(self.y_size + self.BOARD_MARGIN * 2)] for x in range(self.x_size + self.BOARD_MARGIN * 2)]
self.forrbidden_pixels = 4 * ((self.BOARD_MARGIN * self.BOARD_MARGIN) + self.x_size + self.y_size)
self.init_game_board()
def init_game_board(self):
for y in xrange(self.y_size + self.BOARD_MARGIN * 2):
for x in range(self.x_size + self.BOARD_MARGIN * 2):
if x < self.BOARD_MARGIN:
self.game_board[x][y] = PxType.forrbidden
if x >= self.x_size + self.BOARD_MARGIN:
self.game_board[x][y] = PxType.forrbidden
if y < self.BOARD_MARGIN:
self.game_board[x][y] = PxType.forrbidden
if y >= self.y_size + self.BOARD_MARGIN:
self.game_board[x][y] = PxType.forrbidden
def show_raw_game_board(self):
show_string = ''
for y in xrange(self.y_size + self.BOARD_MARGIN * 2):
for x in xrange(self.x_size + self.BOARD_MARGIN * 2):
pixel_value = self.game_board[x][y]
if pixel_value == 0:
show_string += ' . '
elif pixel_value == PxType.forrbidden:
show_string += ' X '
elif pixel_value == PxType.placed:
show_string += ' O '
elif pixel_value == PxType.moving:
show_string += ' | '
show_string += '\n'
print(show_string)
def check_move(self):
forrbidden_pixels = 0
for y in xrange(self.y_size + self.BOARD_MARGIN * 2):
for x in xrange(self.x_size + self.BOARD_MARGIN * 2):
if self.game_board[x][y] == PxType.forrbidden:
forrbidden_pixels += 1
if forrbidden_pixels == self.forrbidden_pixels:
return True
my_tetris = TetrisCore(10, 22)
my_tetris.show_raw_game_board()