-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
344 lines (299 loc) · 11.9 KB
/
main.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import pygame
import random
import time
import copy
Tetromino = [
[[0]],
[[0,0,0,0],
[1,1,1,1],
[0,0,0,0],
[0,0,0,0]],
[[1,1],
[1,1]],
[[0,1,0],
[1,1,1],
[0,0,0]],
[[0,0,1],
[1,1,1],
[0,0,0]],
[[1,0,0],
[1,1,1],
[0,0,0]],
[[1,1,0],
[0,1,1],
[0,0,0]],
[[0,1,1],
[1,1,0],
[0,0,0]]
]
n = 7
cl = [(40,40,40), (30, 255, 255), (255, 255, 30), (80, 30, 180), (255, 135, 30), (30, 150, 255), (255, 30, 30), (30, 255, 135)]
def createTetromino(map, c):
Tetromino.append(map)
cl.append(c)
n += 1
def removeTetromino(i):
if 7 < i < len(Tetromino):
Tetromino.pop(i)
cl.pop(i)
n -= 1
class Tetris():
def __init__(self, sp=10, code=random.randint(1,n)):
self.speed = max(sp,0)
self.code = code
self.pos = [4,-1]
self.rotation = 0
self.graceTime = 4
def get_image(self):
while self.rotation < 0:
self.rotation += 4
self.rotation %= 4
positions = {}
bk = Tetromino[self.code]
for _ in range(self.rotation):
bk = [[i[j] for i in bk[::-1]] for j in range(len(bk))]
for dy in range(len(bk)):
for dx in range(len(bk[0])):
adjX = self.pos[0] + dx
adjY = self.pos[1] + dy
positions[adjY*100 + adjX] = bk[dy][dx]
return positions
class Game():
def __init__(self, lvl=0):
self.lvl = lvl
self.board = [[0 for _ in range(10)] for _ in range(20)]
self.active = Tetris(10-lvl)
self.next = [random.randint(1,n) for _ in range(4)]
self.store = 0
self.points = 0
self.clrT = 0
self.canSwitch = True
self.play = True
def draw(self, screen):
start = 30,100
size = 30
blk = self.active.get_image()
for x in range(10):
for y in range(20):
dispX = start[0] + x * (size+2)
dispY = start[1] + y * (size+2)
if self.board[y][x] == 0 and y == 0:
pygame.draw.rect(screen, (80,40,40), pygame.Rect(dispX, dispY, size, size),0,5)
else:
pygame.draw.rect(screen, cl[self.board[y][x]], pygame.Rect(dispX, dispY, size, size),0,5)
if y*100 + x in blk:
if blk[y*100 + x] == 1:
pygame.draw.rect(screen, cl[self.active.code], pygame.Rect(dispX, dispY, size, size),5,5)
def update(self, tick):
if tick % self.active.speed == 0 and self.play:
if self.forecast(0,1):
self.active.pos[1] += 1
else:
if self.active.graceTime != 0:
self.active.graceTime -= 1
else:
build = self.active.get_image()
for cords in build:
if build[cords] == 1:
x = cords % 100
y = cords // 100
if y >= 0:
self.board[y][x] = self.active.code
self.active = Tetris(10-self.lvl, self.next.pop())
self.next.insert(0,random.randint(1,n))
self.canSwitch = True
self.clearRows()
def switch(self):
if self.canSwitch == True:
temp = self.active.code
if self.store == 0:
self.active = Tetris(10-self.lvl, self.next.pop())
self.next.insert(0,random.randint(1,n))
self.store = temp
else:
self.active = Tetris(10-self.lvl, self.store)
self.store = temp
self.canSwitch = False
def clearRows(self):
global highScore
clr = 0
for y in range(20):
f = True
for j in self.board[y]:
if j == 0:
f = False
if f == True:
clr += 1
self.board.pop(y)
self.board.insert(0,[0 for _ in range(10)])
pnt = 0
pnt = 40 if clr == 1 else pnt
pnt = 100 if clr == 2 else pnt
pnt = 300 if clr == 3 else pnt
pnt = 1200 if clr == 4 else pnt
self.points += pnt * (self.lvl+1)
self.clrT += clr
if self.clrT // 10 > 0:
self.clrT %= 10
self.lvl += 1
self.active.speed -= 1
for k in self.board[0]:
if k in [l for l in range(1,n+1)]:
self.play = False
if self.points > highScore:
highScore = self.points
def forecast(self, dx,dy, r=0):
blk = copy.deepcopy(self.active)
blk.pos[0] += dx
blk.pos[1] += dy
blk.rotation += 4 if game.active.rotation < 0 else 0
blk.rotation = (blk.rotation + r) % 4
fork = blk.get_image()
for key in fork:
Fx = key%100
Fy = key//100
if fork[key] == 1 and Fy >= 0:
if 0 <= Fx < 10 and Fy < 20:
if self.board[Fy][Fx] in range(1,n+1):
return False
else:
return False
return True
def drawTet(screen,sx,sy,code, s=16):
grid = Tetromino[code]
c = cl[code]
sz = len(grid)
for x in range(sz):
for y in range(sz):
if grid[y][x] == 1:
pygame.draw.rect(screen, c, pygame.Rect(sx+x*(s+2),sy+y*(s+2),s,s), 0, 3)
SCREEN_HEIGHT = 800
SCREEN_WIDTH = 600
pygame.init()
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
pygame.display.set_caption("Sydris")
game = Game()
highScore = 0
game.play = False
editor = False
st, et = 0, 0
tk = 0
rate = 20
font = pygame.font.Font('freesansbold.ttf', 32)
btn = pygame.Rect(200, 450, 200, 100)
running = True
while running:
if game.play:
st = time.time()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game.play = False
running = False
k = pygame.key.get_pressed()
if k[pygame.K_DOWN] or k[pygame.K_c]:
tk = game.active.speed
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
if game.forecast(-1,0):
game.active.pos[0] -= 1
if event.key == pygame.K_RIGHT:
if game.forecast(1,0):
game.active.pos[0] += 1
if event.key == pygame.K_UP or event.key == pygame.K_z:
game.switch()
if event.key == pygame.K_s:
tst = [(0,0),(-1,0),(-1,1),(0,-2),(-1,-2)] if game.active.code in [3,2] else [(0,0),(1,0),(1,-1),(0,2),(1,2)]
if game.active.code == 1:
tst = [(0,0),(-2,0),(1,0),(-2,-1),(1,2)] if game.active.rotation == 3 else tst
tst = [(0,0),(2,0),(-1,0),(2,1),(-1,-2)] if game.active.rotation == 1 else tst
tst = [(0,0),(-1,0),(2,0),(-1,2),(2,-1)] if game.active.rotation == 0 else tst
tst = [(0,0),(1,0),(-2,0),(1,-2),(-2,1)] if game.active.rotation == 2 else tst
for D in tst:
if game.forecast(D[0],-D[1],-1):
game.active.rotation -= 1
game.active.rotation += 4 if game.active.rotation < 0 else 0
game.active.pos[0] += D[0]
game.active.pos[1] -= D[1]
break
if event.key == pygame.K_d:
tst = [(0,0),(-1,0),(-1,1),(0,-2),(-1,-2)] if game.active.code in [3,0] else [(0,0),(1,0),(1,-1),(0,2),(1,2)]
if game.active.code == 1:
tst = [(0,0),(-2,0),(1,0),(-2,-1),(1,2)] if game.active.rotation == 0 else tst
tst = [(0,0),(2,0),(-1,0,),(2,1),(-1,-2)] if game.active.rotation == 2 else tst
tst = [(0,0),(-1,0),(2,0),(-1,2),(2,-1)] if game.active.rotation == 1 else tst
tst = [(0,0),(1,0),(-2,0),(1,-2),(-2,1)] if game.active.rotation == 3 else tst
for D in tst:
if game.forecast(D[0],-D[1],1):
game.active.rotation = (game.active.rotation+1)%4
game.active.pos[0] += D[0]
game.active.pos[1] -= D[1]
break
if event.key == pygame.K_q:
ps = True
while ps:
for event in pygame.event.get():
if event.type == pygame.QUIT:
ps = False
game.play = False
running = False
if pygame.key.get_pressed()[pygame.K_q]:
ps = False
screen.fill((70,70,70))
game.update(tk)
game.draw(screen)
pygame.draw.rect(screen, (100,100,100), pygame.Rect(150, 20, 150, 50), 0,15)
pygame.draw.rect(screen, (100,100,100), pygame.Rect(400, 100, 100, 100), 0,15)
pygame.draw.rect(screen, (100,100,100), pygame.Rect(400, 230, 100, 400), 0,15)
t1 = font.render('Score: ', True, (255,255,255))
screen.blit(t1, (20, 30))
t2 = font.render(str(int(game.points)), True, (255,255,255))
screen.blit(t2, (170, 30))
t5 = font.render('level: ' + str(game.lvl), True, (255,255,255))
screen.blit(t5, (400, 30))
drawTet(screen,420,120,game.store)
drawTet(screen,420,260,game.next[3])
drawTet(screen,420,340,game.next[2])
drawTet(screen,420,420,game.next[1])
drawTet(screen,420,500,game.next[0])
pygame.display.flip()
et = time.time()
dt = et-st
tk += 1
time.sleep(max(1/rate - 1/rate*dt,0))
elif editor:
screen.fill((70,70,70))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
if btn.collidepoint(event.pos):
game = Game()
if btn.collidepoint(pygame.mouse.get_pos()):
pygame.draw.rect(screen, (80,80,80), btn, 0,15)
else:
pygame.draw.rect(screen, (150,50,50), btn, 0,15)
t0 = font.render('Play', True, (255,255,255))
screen.blit(t0, (270, 485))
else:
screen.fill((70,70,70))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
if btn.collidepoint(event.pos):
game = Game()
t0 = font.render('SYDtris', True, (255,255,255))
screen.blit(t0, (250, 235))
if btn.collidepoint(pygame.mouse.get_pos()):
pygame.draw.rect(screen, (80,80,80), btn, 0,15)
else:
pygame.draw.rect(screen, (150,50,50), btn, 0,15)
t0 = font.render('Play', True, (255,255,255))
screen.blit(t0, (270, 485))
pygame.draw.rect(screen, (100,100,100), pygame.Rect(180, 300, 300, 100), 0,15)
t0 = font.render('highScore: ' + str(highScore), False, (255,255,255))
screen.blit(t0, (210, 315))
t0 = font.render('score: ' + str(game.points), False, (255,255,255))
screen.blit(t0, (210, 345))
pygame.display.flip()
pygame.quit()