-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay.py
160 lines (120 loc) · 4.27 KB
/
play.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import random
from resources import *
from helpers import *
import readchar
def add_line_below(board):
line_size = len(board[0])
line = [blocks[random.randint(1, 5)] for i in range(line_size)]
board.insert(0, line)
return board
def remove_line_above(board):
del board[-1]
return board
def new_board(pattern=None):
new_board = [[white_block for x in range(6)] for y in range(10)]
if pattern:
for index, line in enumerate(reversed(pattern)):
new_board[index] = line
return new_board
def run_in_board(board, callback):
need_screen_update = False
for row_index, row in enumerate(board):
for cell_index, cell in enumerate(row):
if need_screen_update:
board = callback(
board, row, row_index, cell, cell_index)[1]
else:
need_screen_update, board = callback(
board, row, row_index, cell, cell_index)
return need_screen_update, board
def update_board(board):
need_screen_update, board = run_in_board(board, check_for_drop)
need_screen_update, board = run_in_board(board, check_for_match)
return board
def check_for_drop(board, row, row_index, cell, cell_index):
dropped = False
last_empty_space = row_index
if board[row_index][cell_index] != white_block:
def has_white_space_below(
): return board[last_empty_space - 1][cell_index] == white_block
dropped = has_white_space_below()
while has_white_space_below() and last_empty_space > 0:
last_empty_space -= 1
board[row_index][cell_index] = white_block
board[last_empty_space][cell_index] = cell
return dropped, board
def check_for_match(board, row, row_index, cell, cell_index):
matched = False
if cell != white_block:
h_matching = 1
v_matching = 1
matches = [[row_index, cell_index]]
while cell_index + h_matching < len(board[row_index]) and board[row_index][cell_index + h_matching] == cell:
matches.append([row_index, cell_index + h_matching])
h_matching += 1
while row_index + v_matching < len(board) and board[row_index + v_matching][cell_index] == cell:
matches.append([row_index + v_matching, cell_index])
v_matching += 1
if(h_matching > 2 or v_matching > 2):
for match in matches:
board[match[0]][match[1]] = white_block
board = update_board(board)
matched = True
return matched, board
def move_piece(board, piece, side=1):
x, y = 1, 0
if piece[x] + side >= 0 \
and piece[x] + side < len(board[0]):
move_to = board[piece[y]][piece[x] + side]
board[piece[y]][piece[x] + side] = board[piece[y]][piece[x]]
board[piece[y]][piece[x]] = move_to
board = update_board(board)
return board
def move_cursor(pressed_key, cursor):
x, y = 1, 0
if pressed_key == readchar.key.UP:
cursor[y] += 1
if pressed_key == readchar.key.DOWN:
cursor[y] -= 1
if pressed_key == readchar.key.LEFT:
cursor[x] -= 1
if pressed_key == readchar.key.RIGHT:
cursor[x] += 1
return cursor
def swap(board, pressed_key, cursor):
if pressed_key == readchar.key.SPACE:
move_piece(board, cursor)
def playable_game():
ticks = 0
cursor = [0, 0]
board = new_board()
for i in range(5):
board = add_line_below(board)
board = update_board(board)
while True:
if ticks % 20 == 0:
board = add_line_below(board)
board = remove_line_above(board)
pressed_key = readchar.readkey()
if(pressed_key == readchar.key.CTRL_C or pressed_key == readchar.key.CTRL_D):
break
cursor = move_cursor(pressed_key, cursor)
swap(board, pressed_key, cursor)
update_screen(board, cursor)
ticks += 1
clear_screen()
print("""
.----. .----. .----. .----. .----.
| () )| () )| (_ ( (__ ( (__
| .--' | .-. \| (__ .-._) ).-._) )
`-' `-' `-'`----'`----' `----'
.--. .-. .-..-. .-.
/ () \ | `| | \ \/ /
/ /\ \| |\ | ) (
`-' `-'`-' `-' `--'
.-. .-..----..-. .-.
| |/ / | (_ \ \/ /
| |\ \ | (__ ) (
`-' `-'`----' `--'
""")
playable_game()