Commit 332833b4 authored by Dimitris Strovolidis's avatar Dimitris Strovolidis

Improvements

Change collision algorithm
Fix tetromino move down bug
parent 6cbe2431
......@@ -38,18 +38,14 @@ class GameArea:
:param tetromino_coords: list of coordinates (x, y)
:return: True if blocks collide
"""
rect_list = []
for coord in tetromino_coords:
rect_list.append(pygame.Rect(coord, (self.block_width, self.block_height)))
column_index = int((coord[0] - self.min_coord[0]) // self.block_width)
row_index = int((coord[1] + self.block_height - self.min_coord[1]) // self.block_height)
if row_index >= self.rows or self.grid[row_index][column_index] >= 0:
return True
for i in range(self.rows):
for j in range(self.columns):
if self.grid[i][j] >= 0:
coord = (self.min_coord[0] + j * self.block_width,
self.min_coord[1] + i * self.block_height)
rect = pygame.Rect(coord, (self.block_width, self.block_height))
if not rect.collidelist(rect_list) < 0:
return True
return False
def is_game_over(self):
......@@ -77,7 +73,8 @@ class GameArea:
indexes_list = self.get_grid_indexes(coords)
for row_index, column_index in indexes_list:
self.grid[row_index][column_index] = color_index
if row_index >= 0 and column_index >= 0:
self.grid[row_index][column_index] = color_index
# search for full rows
for row_index, column_index in indexes_list:
......
......@@ -123,8 +123,6 @@ def main():
accumulator = 0.0
while not game_over:
game_over = game_area.is_game_over()
# check keyboard input
for event in pygame.event.get():
if event.type == pygame.QUIT:
......@@ -189,7 +187,7 @@ def main():
score_surface = write(font, score_string, text_color)
accumulator += frame_time
while accumulator >= dt:
while accumulator >= dt and not game_over:
bottom = current_tetromino.move_down(dt)
collision = game_area.collides(current_tetromino.get_coords())
# tetromino collided with a block or reached bottom
......@@ -200,8 +198,8 @@ def main():
game_area.get_min_coord(), game_area.get_max_coord())
next_tetromino = Tetromino((block_width, block_height), color_blocks,
next_tetromino_coord, min_screen_coord, max_screen_coord)
accumulator -= dt
game_over = game_area.is_game_over()
# Draw
screen.blit(background, min_screen_coord)
......
......@@ -233,9 +233,10 @@ class Tetromino:
return True
# move all blocks
distance = round(self.speed * time)
for coord in self.blocks_coords:
coord[1] += self.speed * time
self.center_coord[1] += self.speed * time
coord[1] += distance
self.center_coord[1] += distance
return False
def move_left(self):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment