generated from TH-Activities/saturday-hack-night-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtetris.py
131 lines (98 loc) · 4.18 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
from settings import *
import math
from tetromino import Tetromino
import pygame.freetype as ft
class Text:
def __init__(self, app):
self.app = app
self.font = ft.Font(FONT_PATH)
def get_color(self):
time = pg.time.get_ticks() * 0.001
n_sin = lambda t: (math.sin(t) * 0.5 + 0.5) * 255
return n_sin(time * 0.5), n_sin(time * 0.2), n_sin(time * 0.9)
def draw(self):
self.font.render_to(self.app.screen, (WIN_W * 0.600, WIN_H * 0.02),
text='TETRIS', fgcolor=self.get_color(),
size=TILE_SIZE * 1.65, bgcolor=BG_COLOR)
self.font.render_to(self.app.screen, (WIN_W * 0.65, WIN_H * 0.22),
text='next', fgcolor=(155,188,15),
size=TILE_SIZE * 1.4, bgcolor=BG_COLOR)
self.font.render_to(self.app.screen, (WIN_W * 0.64, WIN_H * 0.67),
text='score', fgcolor=(155,188,15),
size=TILE_SIZE * 1.4, bgcolor=BG_COLOR)
self.font.render_to(self.app.screen, (WIN_W * 0.64, WIN_H * 0.8),
text=f'{self.app.tetris.score}', fgcolor=(170,170,170),
size=TILE_SIZE * 1.8)
class Tetris:
def __init__(self, app):
self.app = app
self.sprite_group = pg.sprite.Group()
self.field_array = self.get_field_array()
self.tetromino = Tetromino(self)
self.next_tetromino = Tetromino(self, current=False)
self.speed_up = False
self.score = 0
self.full_lines = 0
self.points_per_lines = {0: 0, 1: 100, 2: 300, 3: 700, 4: 1500}
def get_score(self):
self.score += self.points_per_lines[self.full_lines]
self.full_lines = 0
def check_full_lines(self):
row = FIELD_H - 1
for y in range(FIELD_H - 1, -1, -1):
for x in range(FIELD_W):
self.field_array[row][x] = self.field_array[y][x]
if self.field_array[y][x]:
self.field_array[row][x].pos = vec(x, y)
if sum(map(bool, self.field_array[y])) < FIELD_W:
row -= 1
else:
for x in range(FIELD_W):
self.field_array[row][x].alive = False
self.field_array[row][x] = 0
self.full_lines += 1
def put_tetromino_blocks_in_array(self):
for block in self.tetromino.blocks:
x, y = int(block.pos.x), int(block.pos.y)
self.field_array[y][x] = block
def get_field_array(self):
return [[0 for x in range(FIELD_W)] for y in range(FIELD_H)]
def is_game_over(self):
if self.tetromino.blocks[0].pos.y == INIT_POS_OFFSET[1]:
pg.time.wait(300)
return True
def check_tetromino_landing(self):
if self.tetromino.landing:
if self.is_game_over():
self.__init__(self.app)
else:
self.speed_up = False
self.put_tetromino_blocks_in_array()
self.next_tetromino.current = True
self.tetromino = self.next_tetromino
self.next_tetromino = Tetromino(self, current=False)
def control(self, pressed_key):
if pressed_key == pg.K_LEFT:
self.tetromino.move(direction='left')
elif pressed_key == pg.K_RIGHT:
self.tetromino.move(direction='right')
elif pressed_key == pg.K_UP:
self.tetromino.rotate()
elif pressed_key == pg.K_DOWN:
self.speed_up = True
def draw_grid(self):
for x in range(FIELD_W):
for y in range(FIELD_H):
pg.draw.rect(self.app.screen, (52, 52, 52),
(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE), 1)
def update(self):
trigger = [self.app.anim_trigger, self.app.fast_anim_trigger][self.speed_up]
if trigger:
self.check_full_lines()
self.tetromino.update()
self.check_tetromino_landing()
self.get_score()
self.sprite_group.update()
def draw(self):
self.draw_grid()
self.sprite_group.draw(self.app.screen)