-
Notifications
You must be signed in to change notification settings - Fork 1
/
tetris.py
executable file
·354 lines (300 loc) · 11.2 KB
/
tetris.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/python
import curses
import time
import random
class Board(object):
def __init__(self, stdscr):
self.stdscr = stdscr
curses.curs_set(0)
curses.init_pair(curses.COLOR_WHITE, curses.COLOR_BLACK,
curses.COLOR_WHITE)
curses.init_pair(curses.COLOR_CYAN, curses.COLOR_BLACK,
curses.COLOR_CYAN)
curses.init_pair(curses.COLOR_BLUE, curses.COLOR_BLACK,
curses.COLOR_BLUE)
curses.init_pair(curses.COLOR_YELLOW, curses.COLOR_BLACK,
curses.COLOR_YELLOW)
curses.init_pair(curses.COLOR_GREEN, curses.COLOR_BLACK,
curses.COLOR_GREEN)
curses.init_pair(curses.COLOR_MAGENTA, curses.COLOR_BLACK,
curses.COLOR_MAGENTA)
curses.init_pair(curses.COLOR_RED, curses.COLOR_BLACK,
curses.COLOR_RED)
b_y = 1
b_x = 1
self.height = 20
self.width = 10
self.y_limit = self.height - 1
self.x_limit = self.width - 1
self.speed = 1
self.delay = 1
self.state = []
self.total_rows_cleared = 0
self.score = 0
self.next_piece = self.get_random_piece()
for row in range(self.height):
self.state.append([curses.COLOR_BLACK] * self.width)
self.board = curses.newwin(self.height, self.width * 2, b_y, b_x)
self.stdscr.addstr(b_y-1, b_x-1, " ",
curses.color_pair(curses.COLOR_WHITE))
for i in range(1, self.height + 1):
self.stdscr.addstr(i, 0, " ",
curses.color_pair(curses.COLOR_WHITE))
self.stdscr.addstr(i, (self.width * 2) + 1, " ",
curses.color_pair(curses.COLOR_WHITE))
self.stdscr.addstr(self.height + 1, 0, " ",
curses.color_pair(curses.COLOR_WHITE))
self.stdscr.refresh()
self.info_win = curses.newwin(3, 30, 0, self.width*2 + 5)
self.info_win.addstr(0, 0, "SCORE: ")
self.info_win.addstr(1, 0, "SPEED: " + str(self.speed).rjust(7))
self.info_win.addstr(2, 0,
" ROWS: " + str(self.total_rows_cleared).rjust(7))
self.increase_score(0)
self.preview = curses.newwin(3, 8, 4, self.width*2 + 5)
self.preview.addstr(0, 1, "NEXT:")
self.preview.refresh()
def increase_total_rows_cleared(self):
self.total_rows_cleared += 1
# Increase speed by 1 for every divisor number of rows cleared
self.speed = 1 + self.total_rows_cleared/10
self.delay = 1.0/self.speed
self.info_win.addstr(1, 7, str(self.speed).rjust(7))
self.info_win.addstr(2, 7, str(self.total_rows_cleared).rjust(7))
self.info_win.refresh()
def increase_score(self, new_points):
self.score += new_points
self.info_win.addstr(0, 7, str(self.score).rjust(7))
self.info_win.refresh()
def clear_blocks(self, cords):
for cord in cords:
row = cord[0]
col = cord[1]
self.state[row][col] = curses.COLOR_BLACK
self.draw_board()
def update_blocks(self, color, new_cords, old_cords=[]):
for cord in set(new_cords) - set(old_cords):
row = cord[0]
col = cord[1]
if self.state[row][col] != curses.COLOR_BLACK:
return False
self.clear_blocks(old_cords)
for cord in new_cords:
row = cord[0]
col = cord[1]
self.state[row][col] = color
self.draw_board()
return True
def draw_preview(self):
for row in xrange(1, 3):
self.preview.insstr(row, 0, " ",
curses.color_pair(curses.COLOR_BLACK))
for cord in self.next_piece.get_preview_cords():
y = 2 + cord[0]
x = 2 + cord[1] * 2
color = curses.color_pair(self.next_piece.color)
self.preview.insstr(y, x, " ", color)
self.preview.refresh()
def draw_board(self):
for row in range(self.height):
for col in range(self.width):
pass
self.board.insstr(row, col * 2, " ",
curses.color_pair(self.state[row][col]))
self.board.refresh()
def clear_full_rows(self):
rows_cleared = 0
for row in xrange(self.height):
if curses.COLOR_BLACK not in self.state[row]:
self.increase_total_rows_cleared()
rows_cleared += 1
self.state[row] = [curses.COLOR_WHITE] * self.width
self.draw_board()
time.sleep(0.1)
self.increase_score(1000 * rows_cleared * rows_cleared * self.speed)
for row in xrange(self.height):
if curses.COLOR_BLACK not in self.state[row]:
del self.state[row]
self.state.insert(0, [curses.COLOR_BLACK] * self.width)
self.draw_board()
def get_random_piece(self):
r = random.randint(0, 6)
if r == 0:
return Piece_T(self)
elif r == 1:
return Piece_I(self)
elif r == 2:
return Piece_S(self)
elif r == 3:
return Piece_Z(self)
elif r == 4:
return Piece_O(self)
elif r == 5:
return Piece_L(self)
elif r == 6:
return Piece_J(self)
def get_piece(self):
piece = self.next_piece
self.next_piece = self.get_random_piece()
self.draw_preview()
return piece
def main(self):
p = self.get_piece()
p.draw()
curses.halfdelay(1)
t1 = time.time()
while True:
c = self.stdscr.getch()
t2 = time.time()
if t2 - t1 > self.delay:
t1 = t2
if not p.move_down():
self.clear_full_rows()
p = self.get_piece()
if not p.draw():
break
elif c == curses.KEY_DOWN:
if not p.move_down():
self.clear_full_rows()
p = self.get_piece()
if not p.draw():
break
if c == ord('q'):
break
if c == curses.KEY_UP:
p.rotate()
if c == curses.KEY_LEFT:
p.move_left()
if c == curses.KEY_RIGHT:
p.move_right()
if c == ord(' '):
while p.move_down():
pass
self.clear_full_rows()
p = self.get_piece()
if not p.draw():
break
self.stdscr.addstr(2, 6, "GAME OVER!")
self.stdscr.addstr(4, 3, "Press Q to QUIT")
c = self.stdscr.getch()
while c != ord('q') and c != ord('Q'):
c = self.stdscr.getch()
class Piece(object):
def __init__(self, board):
self.board = board
self.x = 5
self.y = 1
self.orientation = 0
def get_cords(self):
return [(self.y + y, self.x + x) for (y, x)
in self.layouts[self.orientation]]
def get_new_cords(self, y_delta, x_delta, orientation):
return [(self.y + y + y_delta, self.x + x + x_delta) for (y, x)
in self.layouts[orientation]]
def get_preview_cords(self):
return self.layouts[0]
def bounds_valid(self, y_delta, x_delta, orientation):
for cord in self.get_new_cords(y_delta, x_delta, orientation):
row = cord[0]
col = cord[1]
# (( is the only way to silence pep8 :(
# https://github.com/PyCQA/pycodestyle/issues/126
if ((row < 0 or row > self.board.y_limit or
col < 0 or col > self.board.x_limit)):
return False
return True
def draw(self, old_cords=[]):
return self.board.update_blocks(self.color, self.get_cords(),
old_cords)
def rotate(self):
old_cords = self.get_cords()
new_orientation = (self.orientation + 1) % len(self.layouts)
if self.bounds_valid(0, 0, new_orientation):
self.orientation = new_orientation
if self.draw(old_cords):
return True
else:
self.orientation = (self.orientation - 1) % len(self.layouts)
return False
def move(self, y_delta, x_delta):
old_cords = self.get_cords()
if self.bounds_valid(y_delta, x_delta, self.orientation):
self.y += y_delta
self.x += x_delta
if self.draw(old_cords):
return True
else:
self.y -= y_delta
self.x -= x_delta
return False
def move_left(self):
return self.move(0, -1)
def move_right(self):
return self.move(0, 1)
def move_down(self):
return self.move(1, 0)
class Piece_T(Piece):
def __init__(self, board):
super(Piece_T, self).__init__(board)
self.layouts = [
[(-1, 0), (0, -1), (0, 0), (0, 1)],
[(-1, 0), (0, 0), (0, 1), (1, 0)],
[(0, -1), (0, 0), (0, 1), (1, 0)],
[(-1, 0), (0, -1), (0, 0), (1, 0)],
]
self.color = curses.COLOR_MAGENTA
class Piece_I(Piece):
def __init__(self, board):
super(Piece_I, self).__init__(board)
self.layouts = [
[(0, -1), (0, 0), (0, 1), (0, 2)],
[(-1, 0), (0, 0), (1, 0), (2, 0)],
]
self.color = curses.COLOR_CYAN
class Piece_S(Piece):
def __init__(self, board):
super(Piece_S, self).__init__(board)
self.layouts = [
[(-1, 0), (-1, 1), (0, -1), (0, 0)],
[(-1, 0), (0, 0), (0, 1), (1, 1)],
]
self.color = curses.COLOR_GREEN
class Piece_Z(Piece):
def __init__(self, board):
super(Piece_Z, self).__init__(board)
self.layouts = [
[(-1, -1), (-1, 0), (0, 0), (0, 1)],
[(-1, 1), (0, 0), (0, 1), (1, 0)],
]
self.color = curses.COLOR_RED
class Piece_O(Piece):
def __init__(self, board):
super(Piece_O, self).__init__(board)
self.layouts = [
[(-1, -1), (-1, 0), (0, -1), (0, 0)]
]
self.color = curses.COLOR_WHITE
class Piece_L(Piece):
def __init__(self, board):
super(Piece_L, self).__init__(board)
self.layouts = [
[(-1, 1), (0, -1), (0, 0), (0, 1)],
[(-1, 0), (0, 0), (1, 0), (1, 1)],
[(0, -1), (0, 0), (0, 1), (1, -1)],
[(-1, -1), (-1, 0), (0, 0), (1, 0)],
]
self.color = curses.COLOR_YELLOW
class Piece_J(Piece):
def __init__(self, board):
super(Piece_J, self).__init__(board)
self.layouts = [
[(-1, -1), (0, -1), (0, 0), (0, 1)],
[(-1, 0), (-1, 1), (0, 0), (1, 0)],
[(0, -1), (0, 0), (0, 1), (1, 1)],
[(-1, 0), (0, 0), (1, -1), (1, 0)],
]
self.color = curses.COLOR_BLUE
def tetris_main(stdscr):
b = Board(stdscr)
b.main()
curses.wrapper(tetris_main)