-
Notifications
You must be signed in to change notification settings - Fork 0
/
tetris.py
240 lines (189 loc) · 7.6 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import pygame
import random
from random import randint
import time
random.seed(time.time())
from copy import deepcopy
NOT_RUNNING = 0
FALLING = 1
DROPPED = 2
GAME_OVER = 3
ROW_BUFFER = 23
ROW = 20
COL = 10
EMPTY_ROW = [0 for i in range(10)]
COLOR = [None, (251, 86, 90), (114, 55, 197), (45, 133, 222), (28, 196, 171), (241, 220, 27)]
from block import Block
class Tetris:
def __init__(self, size, pos, title, screen):
self.table = []
self.block_list = []
self.state = NOT_RUNNING
self.score = 0
self.combo = 0
self.width = size[0]
self.height = size[1]
self.x = pos[0]
self.y = pos[1]
self.title = title
self.screen = screen
self.drow = self.height/ROW
self.dcol = self.width/COL
self.yy = self.y + self.height - self.drow
for i in range(ROW_BUFFER):
self.table.append(EMPTY_ROW[:])
self.draw_title()
self.add_score(0)
self.new_block(True)
self.draw()
def __del__(self):
del self
def draw(self):
pygame.draw.rect(self.screen, (67, 95, 120), pygame.Rect(self.x, self.y, self.width, self.height))
for i in range(ROW):
for j in range(COL):
if self.table[i][j] != 0:
pygame.draw.rect(self.screen,
COLOR[self.table[i][j]],
pygame.Rect(self.x+self.dcol*j, self.yy-self.drow*i, self.dcol, self.drow))
elif self.block.x<=i and i<self.block.x+self.block.size and \
self.block.y<=j and j<self.block.y+self.block.size:
if self.block.block[i-self.block.x][j-self.block.y] != 0:
pygame.draw.rect(self.screen,
COLOR[self.block.block[i-self.block.x][j-self.block.y]],
pygame.Rect(self.x+self.dcol*j, self.yy-self.drow*i, self.dcol, self.drow))
for i in range(1, ROW):
y = self.y+self.drow*i
pygame.draw.line(self.screen, (197, 227, 236), (self.x, y), (self.x+self.width, y), 1)
for i in range(1, COL):
x = self.x+self.dcol*i
pygame.draw.line(self.screen, (197, 227, 236), (x, self.y), (x, self.y+self.height), 1)
pygame.draw.rect(self.screen, (197, 227, 236), pygame.Rect(self.x-2, self.y-2, self.width+4, self.height+4), 4)
pygame.display.flip()
def draw_title(self):
if self.title is not None:
font = pygame.font.SysFont(None, 72)
text = font.render(self.title, True, (197, 227, 236))
self.screen.blit(text, (self.x+(self.width-text.get_width())/2, self.y-text.get_height()-10))
def draw_next(self):
if self.title is None:
width = self.dcol*4 + 25*2
height = self.drow*4*3 + 25*4
x = self.x + self.width + 50
y = self.y + 50
pygame.draw.rect(self.screen, (67, 95, 120), pygame.Rect(self.x+self.width+25, self.y+25, width, height))
for k in range(3):
offset = 0
if self.block_list[k].size == 3:
offset = self.drow /2
elif self.block_list[k].size == 2:
offset = self.drow
for i in range(self.block_list[k].size):
for j in range(self.block_list[k].size):
if self.block_list[k].block[i][j] != 0:
pygame.draw.rect(self.screen,
COLOR[self.block_list[k].block[i][j]],
pygame.Rect(offset + x+self.dcol*j+1, offset + y+self.drow*(self.block_list[k].size-i-1)+1, self.dcol-2, self.drow-2))
y += self.drow*4 + 25
def new_block(self, init):
if init:
for i in range(3):
self.block_list.append(Block(randint(0, 6), randint(1, len(COLOR)-1)))
self.block = self.block_list[0]
self.block_list.pop(0)
self.block_list.append(Block(randint(0, 6), randint(1, len(COLOR)-1)))
self.block.setpos((ROW_BUFFER-self.block.size, randint(0, COL-1-self.block.size)))
self.state = FALLING
self.draw_next()
self.draw()
def fall(self):
pos = self.block.fall()
if self.check(self.block.block, pos):
self.block.setpos(pos)
self.draw()
else:
self.hit()
def drop(self):
x, y = self.block.getpos()
if not self.check(self.block.block, (x, y)):
pass
while self.check(self.block.block, (x-1, y)):
x -= 1
self.block.setpos((x, y))
self.hit()
def move(self, dir):
pos = self.block.move(dir)
if self.check(self.block.block, pos):
self.block.setpos(pos)
self.draw()
def turn(self):
block = self.block.turn()
x, y = self.block.getpos()
success = False
if self.check(block, (x, y)):
success = True
elif self.block.size > 2:
ycan = [y+1, y-1]
if self.block.size == 4:
ycan.append(y-2)
for yx in ycan:
if self.check(block, (x, yx)):
y = yx
success = True
if success:
self.block.setpos((x, y))
self.block.block = block
self.draw()
def check(self, block, pos):
x, y = pos
size = len(block)
for i in range(size):
for j in range(size):
if block[i][j] != 0:
if not (x+i>=0 and y+j>=0 and y+j<COL and self.table[x+i][y+j] == 0):
return False
return True
def game_over(self):
self.state = GAME_OVER
s = pygame.Surface((self.width, self.height))
s.set_alpha(172)
s.fill((59, 82, 78))
self.screen.blit(s, (self.x, self.y))
font = pygame.font.SysFont(None, 72)
text = font.render("Game Over", True, (197, 227, 236))
self.screen.blit(text, (self.x+(self.width-text.get_width())/2, self.y+(self.height-text.get_height())/2))
pygame.display.flip()
def add_score(self, score):
if self.title is None:
self.score += score * self.combo
font = pygame.font.SysFont(None, 72)
text = font.render(str(self.score), True, (197, 227, 236))
pygame.draw.rect(self.screen, (26, 50, 88), pygame.Rect(self.x, self.y-text.get_height()-10, self.width, text.get_height()))
self.screen.blit(text, (self.x+(self.width-text.get_width())/2, self.y-text.get_height()-10))
def hit(self):
for i in range(self.block.size):
for j in range(self.block.size):
if self.block.block[i][j] != 0:
self.table[self.block.x+i][self.block.y+j] = self.block.block[i][j]
i = 0
clear = 0
while i<ROW:
if 0 not in self.table[i]:
self.table.pop(i)
self.table.append(EMPTY_ROW[:])
i -= 1
clear += 1
i += 1
if clear:
self.combo += 1
self.add_score(clear*200-100)
else:
self.combo = 0
self.draw()
for i in range(ROW, ROW_BUFFER):
for j in range(COL):
if self.table[i][j] != 0:
self.game_over()
return None
self.state = DROPPED
self.new_block(False)