-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTETRIS.py
223 lines (186 loc) · 6.09 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
import turtle, time, random
wn = turtle.Screen()
wn.title("TETRIS")
wn.bgcolor("NavajoWhite2")
wn.setup(width=600, height=800)
wn.tracer(0)
delay = 0.1
score = 0
class Shape():
def __init__(self):
self.x = 5
self.y = 0
self.color = random.randint(1, 7)
# Block Shape
square = [[1,1],
[1,1]]
horizontal_line = [[1,1,1,1]]
vertical_line = [[1],
[1],
[1],
[1]]
left_l = [[1,0,0,0],
[1,1,1,1]]
right_l = [[0,0,0,1],
[1,1,1,1]]
left_s = [[1,1,0],
[0,1,1]]
right_s = [[0,1,1],
[1,1,0]]
t = [[0,1,0],
[1,1,1]]
shapes = [square, horizontal_line, vertical_line, left_l, right_l, left_s, right_s, t]
# Choose a random shape each time
self.shape = random.choice(shapes)
self.height = len(self.shape)
self.width = len(self.shape[0])
print(self.height, self.width)
def move_left(self, grid):
if self.x > 0:
if grid[self.y][self.x - 1] == 0:
self.erase_shape(grid)
self.x -= 1
def move_right(self, grid):
if self.x < 12 - self.width:
if grid[self.y][self.x + self.width] == 0:
self.erase_shape(grid)
self.x += 1
def draw_shape(self, grid):
for y in range(self.height):
for x in range(self.width):
if(self.shape[y][x]==1):
grid[self.y + y][self.x + x] = self.color
def erase_shape(self, grid):
for y in range(self.height):
for x in range(self.width):
if(self.shape[y][x]==1):
grid[self.y + y][self.x + x] = 0
def can_move(self, grid):
result = True
for x in range(self.width):
# Check if bottom is a 1
if(self.shape[self.height-1][x] == 1):
if(grid[self.y + self.height][self.x + x] != 0):
result = False
return result
def rotate(self, grid):
# First erase_shape
self.erase_shape(grid)
rotated_shape = []
for x in range(len(self.shape[0])):
new_row = []
for y in range(len(self.shape)-1, -1, -1):
new_row.append(self.shape[y][x])
rotated_shape.append(new_row)
right_side = self.x + len(rotated_shape[0])
if right_side < len(grid[0]):
self.shape = rotated_shape
# Update the height and width
self.height = len(self.shape)
self.width = len(self.shape[0])
grid = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
# Create the drawing pen
pen = turtle.Turtle()
pen.penup()
pen.speed(0)
pen.shape("square")
pen.setundobuffer(None)
def draw_grid(pen, grid):
pen.clear()
top = 230
left = -110
colors = ["black", "lightblue", "blue", "orange", "yellow", "green", "purple", "red"]
for y in range(len(grid)):
for x in range(len(grid[0])):
screen_x = left + (x * 20)
screen_y = top - (y * 20)
color_number = grid[y][x]
color = colors[color_number]
pen.color(color)
pen.goto(screen_x, screen_y)
pen.stamp()
def check_grid(grid):
# Check if each row is full
y = 23
while y > 0:
is_full = True
for x in range(0, 12):
if grid[y][x] == 0:
is_full = False
y -= 1
break
if is_full:
global score
score += 1
draw_score(pen, score)
for copy_y in range(y, 0, -1):
for copy_x in range(0, 12):
grid[copy_y][copy_x] = grid[copy_y-1][copy_x]
def draw_score(pen, score):
pen.color("blue")
pen.hideturtle()
pen.goto(-75, 350)
pen.write(f"Score: {score}", move=False, align="left", font=("commodore 64 pixelized", 24, "normal"))
# Create the basic shape for the start of the game
shape = Shape()
# Put the shape in the grid
grid[shape.y][shape.x] = shape.color
# Draw the initial grid
# draw_grid(pen, grid)
wn.listen()
wn.onkeypress(lambda: shape.move_left(grid), "Left")
wn.onkeypress(lambda: shape.move_right(grid), "Right")
wn.onkeypress(lambda: shape.rotate(grid), "Up")
# Set the score to 0
score = 0
draw_score(pen, score)
# Main game loop
while True:
wn.update()
# Move the shape
# Open Row
# Check for the bottom
if shape.y == 23 - shape.height + 1:
shape = Shape()
check_grid(grid)
# Check for collision with next row
elif shape.can_move(grid):
# Erase the current shape
shape.erase_shape(grid)
# Move the shape by 1
shape.y +=1
# Draw the shape again
shape.draw_shape(grid)
else:
shape = Shape()
check_grid(grid)
# Draw the screen
draw_grid(pen, grid)
draw_score(pen, score)
time.sleep(delay)
wn.mainloop()