generated from TH-Activities/saturday-hack-night-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
69 lines (60 loc) · 2.19 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
from settings import *
from tetris import Tetris, Text
import sys
import pathlib
class App:
def __init__(self):
pg.init()
pg.mixer.init()
pg.mixer.music.load("./assets/music/song.mp3")
pg.mixer.music.play(loops=-1)
pg.display.set_caption('Tetris')
self.screen = pg.display.set_mode(WIN_RES)
self.clock = pg.time.Clock()
self.set_timer()
self.images = self.load_images()
self.tetris = Tetris(self)
self.text = Text(self)
def load_images(self):
files = [item for item in pathlib.Path(SPRITE_DIR_PATH).rglob('*.png') if item.is_file()]
images = [pg.image.load(file).convert_alpha() for file in files]
images = [pg.transform.scale(image, (TILE_SIZE, TILE_SIZE)) for image in images]
return images
def set_timer(self):
self.user_event = pg.USEREVENT + 0
self.fast_user_event = pg.USEREVENT + 1
self.anim_trigger = False
self.fast_anim_trigger = False
pg.time.set_timer(self.user_event, ANIM_TIME_INTERVAL)
pg.time.set_timer(self.fast_user_event, FAST_ANIM_TIME_INTERVAL)
def update(self):
self.tetris.update()
self.clock.tick(FPS)
def draw(self):
self.screen.fill(color=BG_COLOR)
self.screen.fill(color=FIELD_COLOR, rect=(0, 0, *FIELD_RES))
self.tetris.draw()
self.text.draw()
pg.display.flip()
def check_events(self):
self.anim_trigger = False
self.fast_anim_trigger = False
for event in pg.event.get():
if event.type == pg.QUIT or (event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE):
pg.quit()
sys.exit()
elif event.type == pg.KEYDOWN:
self.tetris.control(pressed_key=event.key)
elif event.type == self.user_event:
self.anim_trigger = True
elif event.type == self.fast_user_event:
self.fast_anim_trigger = True
def run(self):
while True:
# screen.blit(background,(0,0))
self.check_events()
self.update()
self.draw()
if __name__ == '__main__':
app = App()
app.run()