-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtetris.py
More file actions
247 lines (218 loc) · 7.84 KB
/
tetris.py
File metadata and controls
247 lines (218 loc) · 7.84 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
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
from random import randrange as rand
from copy import deepcopy
rows = 21
cols = 22
WALL = 10
SPACE = 0
tetris_shapes = [
[[1, 1, 1, 1],
],
[[2, 0],
[2, 0],
[2, 2]],
[[0, 3],
[0, 3],
[3, 3]],
[[0, 4],
[4, 4],
[4, 0]],
[[5, 5],
[5, 5]],
]
class Tetris:
def __init__(self):
temp = []
for j in range(rows - 1):
temp1 = []
for i in range(cols):
if i == 0 or i == cols - 1:
temp1.append(WALL)
else:
temp1.append(SPACE)
temp.append(temp1)
temp1 = []
for i in range(cols):
temp1.append(WALL)
temp.append(temp1)
self.arr = temp
self.next_stone = tetris_shapes[rand(len(tetris_shapes))]
self.stone_pos_y = rand(len(range(1, cols - 1 - (len(self.next_stone[0]) - 1))))
self.stone_pos_x = 0
self.game_status_finish = False
# self.piece
def display_mat(self, array):
fin = []
temp = []
for row in array:
temp = []
for unit in row:
if unit == SPACE:
temp.append(" ")
else:
temp.append("*")
fin.append(temp)
for row in fin:
print (''.join(row))
def new_stone(self, board):
self.next_stone = tetris_shapes[rand(len(tetris_shapes))]
self.stone_pos_y = rand(len(range(1, cols - 1 - (len(self.next_stone[0])))))
self.stone_pos_x = 0
if self.check_collision(board, self.stone_pos_x, self.stone_pos_y, self.next_stone):
self.game_status_finish = True
def check_collision(self, board, sx, sy, stone):
for x, row in enumerate(stone):
for y, unit in enumerate(row):
if unit != SPACE:
if board[sx + x][sy + y] != SPACE:
return True
return False
def validate(self, button):
if button == 'a':
return 'MOVE_LEFT'
elif button == 'd':
return 'MOVE_RIGHT'
elif button == 'w':
return 'COUNTER_CLOCKWISE'
elif button == 's':
return 'CLOCKWISE'
return False
def update_board(self, sx, sy, stone, board):
for x, row in enumerate(board):
for y, unit in enumerate(row):
if board[x][y] != SPACE and board[x][y] != WALL:
board[x][y] = SPACE
for x, row in enumerate(stone):
for y, unit in enumerate(row):
if board[sx + x][sy + y] == SPACE:
board[sx + x][sy + y] = stone[x][y]
else:
board[sx + x][sy + y] = WALL
return (sy, board)
def stone_board(self, board):
for x, row in enumerate(board):
for y, unit in enumerate(row):
if board[x][y] != SPACE and board[x][y] != WALL:
self.arr[x][y] = WALL
board[x][y] = WALL
def turn(self, clock=False):
board = deepcopy(self.arr)
stone = deepcopy(self.next_stone)
sx = self.stone_pos_x
sy = self.stone_pos_y
flag = 0
for x, row in enumerate(stone):
for y, unit in enumerate(row):
if stone[x][y] != SPACE:
if sx + x + 1 <rows and sy + y <cols:
if board[sx + x + 1][sy + y] == WALL:
print ('New stone !')
self.stone_board(board)
self.new_stone(board)
return stone
else:
self.stone_board(board)
self.new_stone(board)
return stone
if clock:
stone_chg = zip(*stone[::-1])
else:
stone_chg = zip(*stone)[::-1]
tempboard = deepcopy(board)
board = self.clear(board)
for x, row in enumerate(stone_chg):
for y, unit in enumerate(row):
try:
if stone_chg[x][y] != SPACE:
if board[sx + x + 1][sy + y] != SPACE:
flag = 1
except IndexError:
stone_chg = stone
if flag == 1:
board = deepcopy(tempboard)
for x, row in enumerate(stone):
for y, unit in enumerate(row):
if stone[x][y] != SPACE:
if board[sx + x + 1][sy + y] == WALL:
print ('New stone !')
self.stone_board(board)
self.new_stone(board)
return stone
self.stone_pos_x += 1
sx = self.stone_pos_x
(self.stone_pos_y, self.arr) = self.update_board(sx, sy, stone, board)
else:
self.stone_pos_x += 1
sx = self.stone_pos_x
(self.stone_pos_y, self.arr) = self.update_board(sx, sy, stone_chg, board)
return stone_chg
return stone
def clear(self, board):
for x, row in enumerate(board):
for y, unit in enumerate(row):
if board[x][y] != SPACE and board[x][y] != WALL:
board[x][y] = SPACE
return board
def move(self, pos):
board = deepcopy(self.arr)
stone = deepcopy(self.next_stone)
sx = self.stone_pos_x
sy = self.stone_pos_y
flag = 0
f1 = 0
tempboard = deepcopy(board)
board = self.clear(board)
for x, row in enumerate(stone):
for y, unit in enumerate(row):
if stone[x][y] != SPACE:
if board[sx + x + 1][sy + y + pos] != SPACE:
flag = 1
if flag == 1: # cannot perfrom the given action specifically
# if cant move down
board = deepcopy(tempboard)
for x, row in enumerate(stone):
for y, unit in enumerate(row):
if stone[x][y] != SPACE:
if board[sx + x + 1][sy + y] == WALL :
print ('New stone !')
self.stone_board(board)
self.new_stone(board)
f1 = 1
break
if f1 == 1:
break
# come down if not possible to move
if f1 != 1:
self.stone_pos_x += 1
sx = self.stone_pos_x
(self.stone_pos_y, self.arr) = self.update_board(sx, sy, stone, board)
else:
self.stone_pos_x += 1
sx = self.stone_pos_x
(self.stone_pos_y, self.arr) = self.update_board(sx, sy + pos, stone, board)
return self.next_stone
def run_game(self):
self.keys = {
'MOVE_LEFT': -1,
'MOVE_RIGHT': 1,
'CLOCKWISE': 2,
'COUNTER_CLOCKWISE': -2,
}
while self.game_status_finish is False:
(self.stone_pos_y, self.arr) = self.update_board(self.stone_pos_x, self.stone_pos_y, self.next_stone,
self.arr)
self.display_mat(self.arr)
button = raw_input("Press key:")
while self.validate(button) is False:
button = raw_input("Press key:")
p = self.keys[self.validate(button)]
if p == 1 or p == -1:
self.move(p)
elif p == 2:
self.next_stone = self.turn(clock=True)
else:
self.next_stone = self.turn(clock=False)
# should clear screen here
print ("GAME OVER ! Thank you for playing !")
if __name__ == '__main__':
game = Tetris()
game.run_game()