-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
146 lines (110 loc) · 3.99 KB
/
main.py
File metadata and controls
146 lines (110 loc) · 3.99 KB
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
import sys
import time
import pygame
from const import *
from board import Board
from game import Game
# TODO:
# Add wall kicks, improve rotation
# Find suitable colors for UI/Pieces and new Fonts
class Main:
def __init__(self):
pygame.init()
pygame.display.set_caption('Tetris')
self.board = Board()
self.game = Game(self.board)
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
self.clock = pygame.time.Clock()
self.record = self.get_record()
def mainloop(self, replay=False):
if not replay:
if self.start_screen():
self.game_loop()
else:
self.game_loop()
def start_screen(self):
self.screen.fill(BG_COLOR)
self.game.show_start_screen(self.screen)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
return True
def game_over_screen(self):
self.screen.fill(BG_COLOR)
self.game.show_end_screen(self.screen)
self.set_record(self.board.score)
self.record = self.get_record()
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
# create new game
self.board = Board()
self.game = Game(self.board)
self.mainloop(True)
def game_loop(self):
game = self.game
board = self.board
screen = self.screen
clock = self.clock
board.add_piece()
move_map = {
pygame.K_s: DOWN,
pygame.K_a: LEFT,
pygame.K_d: RIGHT,
}
pygame.time.set_timer(DROP, LEVEL_SPEED.get(board.level))
start_time = pygame.time.get_ticks()
while not board.game_over:
screen.fill(BG_COLOR)
game.show_pieces(screen)
game.show_border(screen)
game.show_piece_trace(screen)
game.show_game_info(self.record, screen)
if board.level_up:
print(f'Level: {board.level}')
print(f'Corresponding drop speed in ms: {LEVEL_SPEED.get(board.level)}')
pygame.time.set_timer(DROP, LEVEL_SPEED.get(board.level))
board.level_up = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == DROP:
board.update_piece(DOWN)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
board.fast_drop()
if event.key == pygame.K_r:
board.update_piece(ROTATE)
pressed = pygame.key.get_pressed()
move = [move_map[key] for key in move_map if pressed[key]]
if move and start_time + 90 < pygame.time.get_ticks():
board.update_piece(move[0])
start_time = pygame.time.get_ticks()
pygame.display.update()
clock.tick(FPS)
self.set_record(board.score)
self.game_over_screen()
def get_record(self):
try:
with open('record') as file:
return file.readline()
except FileNotFoundError:
with open('record', 'w') as file:
file.write('0')
def set_record(self, score):
rec = max(int(self.record), score)
with open('record', 'w') as file:
file.write(str(rec))
main = Main()
main.mainloop()