-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
226 lines (207 loc) · 8.61 KB
/
Copy pathmain.py
File metadata and controls
226 lines (207 loc) · 8.61 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
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
import pygame
import random
from pygame.constants import K_ESCAPE
SCREEN_SIZE : tuple = (500, 1000) #screen size
COL_GRID = (120, 70, 100)
COL_TEXT = (120, 70, 100)
WORLD_SIZE : tuple = (10, 22) #game area size
WORLD_OFFSET : tuple = (50, 50) #visual offset of the game area relative to the game window
PIXEL_SIZE : int = 40
FPS : int = 60
BLOCKS : list[list[tuple]] = [[(0,0), (1,0), (2,0), (3,0)], # long
[(0,0), (1,0), (1,1), (0,1)], # block
[(0, 0), (1, 0), (2, 0), (0, 1)], # L
[(0, 0), (1, 0), (2, 0), (2, 1)], # L inverted
[(0, 0), (1, 0), (1, 1), (2, 1)], # Z
[(0, 1), (1, 1), (1, 0), (2, 0)]] # Z inverted
def setup_pygame(size: tuple, caption: str) -> tuple:
pygame.init()
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
pygame.display.set_caption(caption)
font = pygame.font.Font(None, 36)
score = 0
return screen, clock, score, font
def init_world() -> list:
_world = []
for x in range(WORLD_SIZE[0]):
_world.append([])
for y in range(WORLD_SIZE[1]):
_world[x].append(0)
return _world
def create_block(world : list) -> list[tuple]:
_block = random.choice(BLOCKS)
_val = random.randint(1, 8) * 32 # init new block with random hue
_new_block = []
_center = int(WORLD_SIZE[0] / 3)
for block in _block:
if world[block[0] + _center][block[1]] != 0:
return [] #return empty array if world is already occupied -> game over
_new_block.append((block[0] + _center, block[1], _val))
return _new_block
def rotate_active(active: list[tuple], world : list, dir : int) -> list[tuple]:
x, y, h = zip(*active)
px = int(sum(x) / len(x))
py = int(sum(y) / len(y))
rotated_active = []
for block in active:
tx, ty = block[0] - px, block[1] - py # translate to origin
if dir == 1:
rx, ry = ty, -tx # rotate cw
else:
rx, ry = -ty, tx # rotate ccw
_rotated_active_pos = (int(rx + px), int(ry + py))
if _rotated_active_pos[0] < 0 or _rotated_active_pos[0] >= WORLD_SIZE[0]:
return active
if _rotated_active_pos[1] < 0 or _rotated_active_pos[1] >= WORLD_SIZE[1]:
return active
if world[_rotated_active_pos[0]][_rotated_active_pos[1]] != 0:
return active
rotated_active.append((_rotated_active_pos[0], _rotated_active_pos[1], block[2])) # add pivot back
return rotated_active
def move_active(active : list[tuple], world: list, dir : tuple) -> list[tuple]:
_next_active = []
for block in active:
if block[1] + dir[1] >= WORLD_SIZE[1] or block[0] + dir[0] < 0 or block[0] + dir[0] >= WORLD_SIZE[0]: # boundary
return active
if world[block[0] + dir[0]][block[1] + dir[1]] > 0: # check block below
return active
_next_block = (block[0] + dir[0], block[1] + dir[1], block[2])
_next_active.append(_next_block)
return _next_active
def update_world(world : list, active : list[tuple]) -> list:
height_levels = []
if len(active) == 0:
return
for block in active: #add active blocks to the world
world[block[0]][block[1]] = block[2]
height_levels.append(block[1])
_delete_rows = []
for y in range(min(height_levels), max(height_levels) + 1):
for x in range(WORLD_SIZE[0]):
if world[x][y] == 0:
break
elif world[x][y] > 0 and x == WORLD_SIZE[0] -1:
_delete_rows.append(y)
if len(_delete_rows) == 0:
return world
for x in range(WORLD_SIZE[0]):
_new_col = []
for i in range(len(_delete_rows)):
_new_col.append(0)
for y in range(WORLD_SIZE[1]):
if y not in _delete_rows:
_new_col.append(world[x][y])
world[x] = _new_col
global score
score += len(_delete_rows) * 100
return world
def draw_grid(screen: pygame.Surface):
for x in range(WORLD_SIZE[0]):
for y in range(WORLD_SIZE[1]):
pygame.draw.rect(screen, COL_GRID, (x * PIXEL_SIZE + WORLD_OFFSET[0],
y * PIXEL_SIZE + WORLD_OFFSET[1],
PIXEL_SIZE,
PIXEL_SIZE), 1)
def draw_world(screen: pygame.Surface, world: list) -> None:
for x in range(WORLD_SIZE[0]):
for y in range(WORLD_SIZE[1]):
if world[x][y] > 0:
_val = world[x][y]
_color = pygame.Color(0, 0, 0)
_color.hsva = (_val, 75, 90, 100)
pygame.draw.rect(screen, _color, (x * PIXEL_SIZE + WORLD_OFFSET[0],
y * PIXEL_SIZE + WORLD_OFFSET[1],
PIXEL_SIZE,
PIXEL_SIZE))
def draw_active(screen: pygame.Surface, active: list[tuple]) -> None:
for block in active:
_val = block[2]
_color = pygame.Color(0, 0, 0)
_color.hsva = (_val, 75, 90, 100)
pygame.draw.rect(screen, _color, (block[0] * PIXEL_SIZE + WORLD_OFFSET[0],
block[1] * PIXEL_SIZE + WORLD_OFFSET[1],
PIXEL_SIZE,
PIXEL_SIZE))
def draw_ui(screen: pygame.Surface):
score_text = font.render(f"Score: {score}", True, COL_TEXT)
screen.blit(score_text, (10, 10))
def run_game() -> None:
# main simulation loop
global score, font
screen, clock, score, font = setup_pygame(SCREEN_SIZE, 'Pytris')
move_timer : int = 0 # auto movement timer
move_speed : int = 100 # auto movement speed
# Simulation variables
world = init_world()
active_element = create_block(world)
running = True
game_active = True
while running:
move_left, move_right, rotate_cw, rotate_ccw, move_fall = False, False, False, False, False
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
elif event.type == pygame.KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
return
if event.key == pygame.K_LEFT:
move_left = True
if event.key == pygame.K_RIGHT:
move_right = True
if event.key == pygame.K_UP:
rotate_cw = True
if event.key == pygame.K_DOWN:
rotate_ccw = True
if event.key == pygame.K_SPACE:
move_fall = True
# Game Logic
frame_time = clock.tick(FPS)
if game_active == False:
if move_fall:
world = init_world()
score = 0
game_active = True
continue
if move_left:
active_element = move_active(active_element, world ,(-1, 0))
elif move_right:
active_element = move_active(active_element, world,(1, 0))
elif rotate_cw:
active_element = rotate_active(active_element, world, 1)
elif rotate_ccw:
active_element = rotate_active(active_element, world, -1)
if move_fall:
fall = True
move_timer = 0
while fall:
_down_active = move_active(active_element, world, (0, 1))
if _down_active != active_element:
active_element = _down_active
else:
fall = False
active_element = move_active(active_element, world, (0,1))
# move active element down by one field if timer expires
if move_timer >= 0:
move_timer -= frame_time
else:
_down_active = move_active(active_element, world,(0,1))
if _down_active != active_element:
active_element = _down_active
else:
update_world(world, active_element) # add active element to the world
active_element = create_block(world) # create new active element if last is placed
if len(active_element) == 0:
game_active = False
move_timer = move_speed
screen.fill('black')
draw_grid(screen)
draw_world(screen, world)
draw_active(screen, active_element)
draw_ui(screen)
pygame.display.flip()
clock.tick(FPS)
pygame.quit()
if __name__ == '__main__':
run_game()