-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtetris_one_block.py
180 lines (149 loc) · 4.38 KB
/
tetris_one_block.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
# Tetris running blocks through grid
# Turtle only stamping the colors based on the number in the grid
# Blocks falling as number in the grid
import turtle
import time
delay = 0.1 # For time/sleep
win = turtle.Screen()
win.title('TETRIS ONE BLOCKS by @DimaGutierrez')
win.bgcolor('black')
win.setup(400, 700)
win.tracer(0)
win.listen()
score_count = 0
score = turtle.Turtle()
score.color('red')
score.up()
score.hideturtle()
score.goto(60,-300)
score.write('Score: 0', align='center', font=('Courier', 24, 'normal'))
class Shape():
def __init__(self, grid):
self.x = 5
self.y = 0
self.color = 4
self.grid = grid
self.move = 'go'
def move_right(self):
if self.x < 11 and self.move == 'go':
if grid[self.y][self.x+1]==0:
grid[self.y][self.x]=0
self.x += 1
grid[self.y][self.x] = self.color
def move_left(self):
if self.x > 0 and self.move == 'go':
if grid[self.y][self.x-1]==0:
grid[self.y][self.x]=0
self.x -= 1
grid[self.y][self.x] = self.color
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],
[4,3,3,1,0,1,0,3,3,4,0,1],
[5,1,6,5,3,2,1,0,5,5,0,3],
[1,2,3,4,5,2,0,4,2,3,0,4],
[1,3,1,2,4,2,0,3,1,2,4,3],
[1,1,2,3,0,2,2,2,0,3,0,1],
[0,1,1,2,3,0,0,0,4,0,2,0],
[2,0,1,2,3,0,6,5,5,5,0,2]
]
# Draw outline
border = turtle.Turtle()
border.pensize(10)
border.up()
border.hideturtle()
border.goto(-130,240)
border.down()
border.color('white')
border.rt(90)
border.fd(490) # Down
border.lt(90)
border.fd(260) # Right
border.lt(90)
border.fd(490) # Up
border.up()
border.goto(0,260)
border.write("TETRIS", align='center', font=('Courier', 36, 'normal'))
pen = turtle.Turtle()
pen.up()
pen.speed(0)
pen.shape('square')
pen.shapesize(0.9, 0.9)
pen.setundobuffer(None)
def draw_grid(pen, grid):
pen.clear()
top = 230
left = -110
colors = ['black', 'red', 'lightblue', 'blue', 'orange', 'yellow', 'green',
'purple']
for y in range(len(grid)): # 24 rows
for x in range(len(grid[0])): # 12 columns
screen_x = left + (x*20) # each turtle 20x20 pixels
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():
global score_count
# Check if each row is full:
for y in range(0,24):
is_full = True
y_erase = y
for x in range(0,12):
if grid[y][x] == 0:
is_full = False
break
# Remove row and shift down
if is_full:
score_count += 1
score.clear()
score.write(f'Score: {score_count}', align='center', font=('Courier', 24, 'normal'))
for y in range(y_erase-1, -1, -1):
for x in range(0,12):
grid[y+1][x] = grid[y][x]
# Put shape in the grid
shape = Shape(grid)
grid[shape.y][shape.x] = shape.color
# Draw grid
draw_grid(pen,grid)
# Main game loop
while True:
win.update()
# Move shape
# Stop if at the bottom
if shape.y == 23:
shape.move = 'stop'
check_grid()
shape = Shape(grid)
# Drop down one space if empty below
elif grid[shape.y+1][shape.x] == 0:
grid[shape.y][shape.x]=0
shape.y += 1
grid[shape.y][shape.x] = shape.color
# Stop if above another block
else:
shape.move = 'stop'
shape = Shape(grid)
check_grid()
# Had to place it here for upcoming shapes...
win.onkey(shape.move_right, 'Right')
win.onkey(shape.move_left, 'Left')
draw_grid(pen,grid)
time.sleep(delay)