-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
219 lines (191 loc) · 7.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
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
import pygame
import blocks
import settings as sett
import draw
from tetrion import Tetrion
def main_menu():
colors = sett.COLORS[2:]
current_color = colors.pop()
time = 0
level = 1
instructions_surface = draw.get_instruction_surface()
menu_screen = draw.get_menu_surface(level, current_color)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return False, level
if event.type == 26:
time += 100
if time == 1000:
current_color = colors.pop()
if colors == []:
colors = sett.COLORS[2:]
menu_screen = draw.get_menu_surface(level, current_color)
time = 0
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
return True, level
if event.key == pygame.K_w:
if level < 10:
level += 1
menu_screen = draw.get_menu_surface(
level, current_color)
if event.key == pygame.K_s:
if level > 1:
level -= 1
menu_screen = draw.get_menu_surface(
level, current_color)
draw.draw_surface(screen, menu_screen, 0, 0)
draw.draw_surface(screen, instructions_surface, 12, 2)
draw.draw_rectangle(screen, 1, 1, 10, 20)
pygame.display.update()
clock.tick(30)
def pause():
pause_screen = draw.get_pause_surface()
instructions_surface = draw.get_instruction_surface()
draw.draw_surface(screen, pause_screen, 0, 0)
draw.draw_surface(screen, instructions_surface, 12, 2)
draw.draw_rectangle(screen, 1, 1, 10, 20)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
return True
clock.tick(30)
def game_over(score):
game_over_screen = draw.get_game_over_surface(score)
ladderboard_screen = draw.get_ladderboard_surface(score)
draw.draw_surface(screen, game_over_screen, 0, 0)
draw.draw_surface(screen, ladderboard_screen, 12, 2)
draw.draw_rectangle(screen, 1, 1, 10, 20)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
return True
clock.tick(30)
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode(
(sett.WIDTH, sett.HEIGHT))
def main():
running = True
main_menu_active = True
pygame.time.set_timer(26, 100)
while running:
if main_menu_active:
running, level = main_menu()
tetrion = Tetrion()
block = blocks.get_random_block()
next_block = blocks.get_random_block()
prepare_next = False
score = 0
time = 0
move_time = 0
move_time_limit = 1000 - (level-1)*100
try_move_down = False
try_move_left = False
try_move_right = False
try_rotate = False
drop = False
main_menu_active = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == 26:
time += 100
move_time += 100
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
try_move_left = True
elif event.key == pygame.K_d:
try_move_right = True
elif event.key == pygame.K_s:
try_move_down = True
elif event.key == pygame.K_p:
running = pause()
elif event.key == pygame.K_r:
try_rotate = True
elif event.key == pygame.K_SPACE:
drop = True
if move_time == move_time_limit:
try_move_down = True
if try_move_right:
if block.check_if_on_edge('right'):
x, y = block.get_coords()
part = tetrion.get_part(x+1, y)
if block.can_move(part):
block.move_right()
print(block.get_coords())
try_move_right = False
if try_move_left:
if block.check_if_on_edge('left'):
x, y = block.get_coords()
part = tetrion.get_part(x-1, y)
if block.can_move(part):
block.move_left()
print(block.get_coords())
try_move_left = False
if drop:
while not prepare_next:
if block.check_if_on_edge('bottom'):
x, y = block.get_coords()
part = tetrion.get_part(x, y+1)
if block.can_move(part):
block.move_down()
else:
prepare_next = True
else:
prepare_next = True
drop = False
move_time = 0
if try_move_down:
if block.check_if_on_edge('bottom'):
x, y = block.get_coords()
part = tetrion.get_part(x, y+1)
if block.can_move(part):
block.move_down()
else:
prepare_next = True
else:
prepare_next = True
try_move_down = False
move_time = 0
if try_rotate:
x, y = block.get_coords()
part = tetrion.get_part(x, y)
if block.can_rotate(part):
block.rotate()
try_rotate = False
if prepare_next:
tetrion.update_board(block.state, block.y, block.x)
score += tetrion.remove_rows() ** 2 * 1000
score += 100
block = next_block
next_block = blocks.get_random_block()
prepare_next = False
score_surface = draw.get_text_surface(f'SCORE: {score}')
time_surface = draw.get_text_surface(f'TIME: {time // 1000}')
level_surface = draw.get_text_surface(f'LEVEL: {level}')
next_block_surface = draw.get_text_surface('next block:')
draw.fill_surface(screen, sett.COLORS[1])
draw.draw_squares(tetrion.board, screen)
draw.draw_squares(block.state, screen, block.x, block.y)
draw.draw_squares(next_block.state, screen, 17, 5)
draw.draw_rectangle(screen, 1, 1, 10, 20)
draw.draw_surface(screen, next_block_surface, 12, 4)
draw.draw_surface(screen, score_surface, 12, 8)
draw.draw_surface(screen, level_surface, 12, 9)
draw.draw_surface(screen, time_surface, 12, 10)
if tetrion.is_lost():
running = game_over(score)
main_menu_active = True
pygame.display.update()
clock.tick(30)
main()