-
Notifications
You must be signed in to change notification settings - Fork 0
/
tetris.py
163 lines (145 loc) · 6.69 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
import curses
import block_data
from block import Block
import threading
import time
import copy
import random
BLOCK_STATES = [block_data.BLOCK_I_STATE, block_data.BLOCK_L1_STATE, block_data.BLOCK_L2_STATE, block_data.BLOCK_O_STATE, block_data.BLOCK_T_STATE, block_data.BLOCK_Z1_STATE, block_data.BLOCK_Z2_STATE]
BLOCK_INFOS = [block_data.BLOCK_I_INFO, block_data.BLOCK_L1_INFO, block_data.BLOCK_L2_INFO, block_data.BLOCK_O_INFO, block_data.BLOCK_T_INFO, block_data.BLOCK_Z1_INFO, block_data.BLOCK_Z2_INFO]
BLOCK_TYPES = [block_data.TYPE_I, block_data.TYPE_L1, block_data.TYPE_L2, block_data.TYPE_O, block_data.TYPE_T, block_data.TYPE_Z1, block_data.TYPE_Z2]
class Game(object):
def __init__(self, stdscr):
self.game_area = copy.deepcopy(block_data.GAME_AREA)
self.game_area_nlines = 22
self.game_area_ncols = 12
self.win = curses.newwin(self.game_area_nlines, self.game_area_ncols * 2, 0, 0)
self.current_block_type = random.randint(0, 6)
self.current_block = Block(self.win, 3, 3, 1, 5, BLOCK_STATES[self.current_block_type], BLOCK_INFOS[self.current_block_type], BLOCK_TYPES[self.current_block_type])
self.timer = None
self.game_over = False
self.stdscr = stdscr
def content(self):
self.win.clear()
for line in range(self.game_area_nlines):
for col in range(self.game_area_ncols):
try:
if self.game_area[line][col] == 1 or self.game_area[line][col] == 2:
self.win.chgat(line, col * 2, 2, curses.A_REVERSE)
except curses.error:
pass
def map_block(self):
b1, b2, b3, b4 = self.current_block.square_pos()
self.game_area[b1[0]][b1[1]] = 2
self.game_area[b2[0]][b2[1]] = 2
self.game_area[b3[0]][b3[1]] = 2
self.game_area[b4[0]][b4[1]] = 2
def move_left(self):
b1, b2, b3, b4 = self.current_block.square_pos()
if self.game_area[b1[0]][b1[1] - 1] != 1 and self.game_area[b2[0]][b2[1] - 1] != 1 and self.game_area[b3[0]][b3[1] - 1] != 1 and self.game_area[b4[0]][b4[1] - 1] != 1:
self.game_area[b1[0]][b1[1]] = 0
self.game_area[b2[0]][b2[1]] = 0
self.game_area[b3[0]][b3[1]] = 0
self.game_area[b4[0]][b4[1]] = 0
self.current_block.move_left()
def move_right(self):
b1, b2, b3, b4 = self.current_block.square_pos()
if self.game_area[b1[0]][b1[1] + 1] != 1 and self.game_area[b2[0]][b2[1] + 1] != 1 and self.game_area[b3[0]][b3[1] + 1] != 1 and self.game_area[b4[0]][b4[1] + 1] != 1:
self.game_area[b1[0]][b1[1]] = 0
self.game_area[b2[0]][b2[1]] = 0
self.game_area[b3[0]][b3[1]] = 0
self.game_area[b4[0]][b4[1]] = 0
self.current_block.move_right()
def move_down(self):
b1, b2, b3, b4 = self.current_block.square_pos()
if self.game_area[b1[0] + 1][b1[1]] != 1 and self.game_area[b2[0] + 1][b2[1]] != 1 and self.game_area[b3[0] + 1][b3[1]] != 1 and self.game_area[b4[0] + 1][b4[1]] != 1:
self.game_area[b1[0]][b1[1]] = 0
self.game_area[b2[0]][b2[1]] = 0
self.game_area[b3[0]][b3[1]] = 0
self.game_area[b4[0]][b4[1]] = 0
self.current_block.move_down()
else:
self.game_area[b1[0]][b1[1]] = 1
self.game_area[b2[0]][b2[1]] = 1
self.game_area[b3[0]][b3[1]] = 1
self.game_area[b4[0]][b4[1]] = 1
line = 20
while line != 0:
if self.game_area[line] == [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]:
for index in range(line, 1, -1):
self.game_area[index] = self.game_area[index - 1]
self.game_area[1] = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
else:
line = line - 1
self.current_block_type = random.randint(0, 6)
self.current_block = Block(self.win, 3, 3, 1, 5, BLOCK_STATES[self.current_block_type], BLOCK_INFOS[self.current_block_type], BLOCK_TYPES[self.current_block_type])
c1, c2, c3, c4 = self.current_block.square_pos()
if self.game_area[c1[0]][c1[1]] == 1 or self.game_area[c2[0]][c2[1]] == 1 or self.game_area[c3[0]][c3[1]] == 1 or self.game_area[c4[0]][b4[1]] == 1:
self.game_over = True
def change_shape(self):
self.current_block.change_state()
b1, b2, b3, b4 = self.current_block.square_pos()
if self.game_area[b1[0] ][b1[1]] != 1 and self.game_area[b2[0]][b2[1]] != 1 and self.game_area[b3[0]][b3[1]] != 1 and self.game_area[b4[0]][b4[1]] != 1:
self.current_block.change_state(-1)
c1, c2, c3, c4 = self.current_block.square_pos()
self.game_area[c1[0]][c1[1]] = 0
self.game_area[c2[0]][c2[1]] = 0
self.game_area[c3[0]][c3[1]] = 0
self.game_area[c4[0]][c4[1]] = 0
self.current_block.change_state()
else:
self.current_block.change_state(-1)
def down(self):
self.move_down()
if not self.game_over:
self.map_block()
self.content()
self.refresh()
self.timer = threading.Timer(0.5, self.down)
self.timer.start()
else:
self.game_area = block_data.GAME_AREA.copy()
self.content()
self.win.addstr(10, 8, "GAME OVER")
self.refresh()
self.current_block = None
def run(self):
self.timer = threading.Timer(0.1, self.down)
self.timer.start()
while True:
ch = self.win.getch()
if self.current_block:
if ch == ord('a'):
self.move_left()
self.map_block()
self.content()
self.refresh()
elif ch == ord('d'):
self.move_right()
self.map_block()
self.content()
self.refresh()
elif ch == ord('s'):
self.move_down()
self.map_block()
self.content()
self.refresh()
elif ch == ord('w'):
self.change_shape()
self.map_block()
self.content()
self.refresh()
def refresh(self):
self.win.refresh()
def clear(self):
self.win.clear()
def main(stdscr):
curses.curs_set(0)
stdscr.refresh()
game = Game(stdscr)
game.map_block()
game.content()
game.refresh()
game.run()
if __name__ == "__main__":
curses.wrapper(main)