-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtetris.py
205 lines (159 loc) · 5.39 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
import sys
from random import randint
import pyautogui
def build_block(block):
return "*" if block == 1 else " "
def join(generator):
return ''.join(list(generator))
def transpose(matrix):
return [list(row) for row in zip(*matrix)]
def reverse(matrix):
return [list(reversed(row)) for row in matrix]
class Block(object):
"""
Block object
"""
def __init__(self, top=None, left=None):
"""
name_of_the_block = block,width and height
"""
block1=[[[1,1,1,1]],4,1]
block2=[[[0,1],[0,1],[1,1]],2,3]
block3=[[[1,0],[1,0],[1,1]],2,3]
block4=[[[0,1],[1,1],[1,0]],2,3]
block5=[[[1,1],[1,1]],2,2]
blocks = [block1,block2,block3,block4,block5]
index = randint(0,100000)%5
print index
chosen_one = blocks[index]
self.top = top
self.left = left
self.height =chosen_one[2]
self.width = chosen_one[1]
self.matrix = chosen_one[0]
def rotate_clockwise(self):
self.matrix = reverse(transpose(self.matrix)) #rotating clock-wise
def rotate_counter_clockwise(self):
self.matrix = transpose(reverse(self.matrix)) #rotate counter-clock-wise
class Screen(object):
"""
Screen Object
"""
def __init__(self, height=20, width=20):
self.height = height
self.width = width
self.matrix = [
[1] + [0 for _ in range(width)] + [1]
for __ in range(height)] + [[1 for _ in range(width + 2)]]
def __str__(self):
"""
Defining what to draw on screen
"""
string = join(build_block(block) for block in self.matrix[0])
for row in self.matrix[1:-1]:
string += "\n"
string += join(build_block(block) for block in row)
string += "\n"
string += join(build_block(block) for block in self.matrix[-1])
return string
def does_fit(self, block, row, col):
"""
Checks the availability of the move
"""
try:
return all(c + self.matrix[row + row_index][col + col_index] in (0, 1)
for row_index, new_row in enumerate(block.matrix)
for col_index, c in enumerate(new_row))
except IndexError:
return False
def place(self, block, y, x):
for row_index, new_row in enumerate(block.matrix):
for col_index, c in enumerate(new_row):
self.matrix[y + row_index][x + col_index] += c
block.top = y
block.left = x
def delete(self, block, y, x):
for row_index, new_row in enumerate(block.matrix):
for col_index, c in enumerate(new_row):
self.matrix[y + row_index][x + col_index] -= c
block.top = y
block.left = x
def new_block_coming(screen, block):
top = 0
left = randint(1, screen.width-block.width)
if screen.does_fit(block, top, left):
screen.place(block, top, left)
else:
raise Exception
def right(screen, block):
screen.delete(block, block.top, block.left)
if screen.does_fit(block, block.top, block.left+1):
screen.place(block, block.top, block.left+1)
else:
screen.place(block, block.top, block.left)
print "Invalid Move!"
def left(screen, block):
screen.delete(block, block.top, block.left)
if screen.does_fit(block, block.top, block.left-1):
screen.place(block, block.top, block.left-1)
else:
screen.place(block, block.top, block.left)
print "Invalid Move!"
def down(screen, block):
screen.delete(block, block.top, block.left)
if screen.does_fit(block, block.top+1, block.left):
screen.place(block, block.top+1, block.left)
else:
screen.place(block, block.top, block.left)
raise Exception
def rotate_clockwise(screen, block):
screen.delete(block, block.top, block.left)
block.rotate_clockwise()
if screen.does_fit(block, block.top, block.left):
screen.place(block, block.top, block.left)
else:
block.rotate_counter_clockwise()
screen.place(block, block.top, block.left)
print "Invalid Move!"
def rotate_counter_clockwise(screen, block):
screen.delete(block, block.top, block.left)
block.rotate_counter_clockwise()
if screen.does_fit(block, block.top, block.left):
screen.place(block, block.top, block.left)
else:
block.rotate_clockwise()
screen.place(block, block.top, block.left)
print "Invalid Move!"
moves = {
'a': left,
'd': right,
'w': rotate_counter_clockwise,
's': rotate_clockwise
}
if __name__ == '__main__':
height = 20
width = 20
screen = Screen(height, width)
block = Block()
new_block_coming(screen, block)
print screen
pyautogui.press('return')
commands = raw_input()
pyautogui.keyDown('enter')
while commands != 'EOF':
try:
down(screen, block)
except:
block = Block()
try:
new_block_coming(screen, block)
except:
print "GAME OVER"
pyautogui.hotkey('ctrl', 'c')
for command in commands:
if command in moves:
moves[command](screen,block)
print screen
commands = raw_input()
pyautogui.PAUSE = 0.5
pyautogui.press('return')