feat: add collision logic between entities

This commit is contained in:
2021-06-08 14:02:23 -04:00
parent 673c4774f2
commit 776170fb9b
5 changed files with 45 additions and 42 deletions

View File

@@ -28,7 +28,7 @@ class Tetris:
self.tile_size = ConfigurationManager.configuration["engine"]["tile-size"]
self.screen = pygame.display.set_mode((win_width, win_height))
self.clock = pygame.time.Clock()
self.well = Well((280, 80)) # TODO calculate position later
self.well = Well((280, 80), ConfigurationManager.configuration["color"]["border"]) # TODO calculate position later and redo color config for well
loaded_icon = pygame.image.load(win_icon)
pygame.display.set_caption(win_title)
@@ -49,18 +49,26 @@ class Tetris:
if self.current_piece:
if event.key == pygame.K_SPACE:
self.current_piece.rotate()
if self.well and self.current_piece.collide(self.well):
self.current_piece.revert()
if event.key == pygame.K_LEFT:
self.current_piece.move((-self.tile_size, 0))
if self.well and self.current_piece.collide(self.well):
self.current_piece.revert()
if event.key == pygame.K_RIGHT:
self.current_piece.move((self.tile_size, 0))
if self.well and self.current_piece.collide(self.well):
self.current_piece.revert()
if event.key == pygame.K_UP:
self.current_piece.move((0, -self.tile_size))
if self.well and self.current_piece.collide(self.well):
self.current_piece.revert()
if event.key == pygame.K_DOWN:
self.current_piece.move((0, self.tile_size))
if self.well and self.current_piece.collide(self.well):
self.current_piece.revert()
if event.key == pygame.K_z:
self.__generate_piece((300, 100))
if event.key == pygame.K_x:
self.current_piece.revert()
def draw(self) -> None:
# TODO write not initialized exception
@@ -71,9 +79,9 @@ class Tetris:
# draw all game objects
if self.well:
self.well.draw(self.screen)
self.well.draw(self.screen, self.tile_size) # TODO Should it be doing taking in tile_size?
if self.current_piece:
self.current_piece.draw(self.screen)
self.current_piece.draw(self.screen, self.tile_size)
# update display
pygame.display.update()

View File

@@ -1,14 +1,25 @@
from typing import Tuple
import pygame
class Entity:
def __init__(self):
pass
def __init__(self, points: Tuple, color: str, border_color: str = None):
self.points = points
self.color = color
self.border_color = border_color
def update(self) -> None:
pass
def draw(self, surface: pygame.Surface) -> None:
pass
def draw(self, surface: pygame.Surface, tile_size) -> None:
for square in self.points:
pygame.draw.polygon(surface, pygame.Color(self.color), square, 0)
if self.border_color:
pygame.draw.polygon(surface, pygame.Color(self.border_color), square, max(tile_size // 6, 1))
def collide(self, entity) -> bool:
pass
for square_one in self.points:
for square_two in entity.points:
for i in range(4): # 4 vertices
if square_one[i][0] == square_two[i][0] and square_one[i][1] == square_two[i][1]:
return True
return False

View File

@@ -11,27 +11,13 @@ from util.ConfigurationManager import ConfigurationManager
'''
class Piece(Entity):
def __init__(self, shape: Tuple, position: Tuple, color: str):
super().__init__()
self.color = color
self.points = self.__get_points(shape, position)
def __init__(self, shape: Tuple, position: Tuple, color: str, border_color: str):
super().__init__(self.__get_points(shape, position), color, border_color)
self.center = self.__get_center(shape, position)
self.previous_points = None
self.previous_center = None
def draw(self, surface: pygame.Surface) -> None:
tile_size = ConfigurationManager.configuration["engine"]["tile-size"]
hex_color = ConfigurationManager.configuration["color"]["border"] # TODO should abstract out color call?
base_color = pygame.Color(self.color)
border_color = pygame.Color(hex_color)
for sub_points in self.points:
pygame.draw.polygon(surface, base_color, sub_points, 0)
pygame.draw.polygon(surface, border_color, sub_points, max(tile_size // 6, 1))
def move(self, vector: Tuple) -> None:
self.previous_points = copy.deepcopy(self.points)
self.previous_center = copy.deepcopy(self.center)
@@ -52,13 +38,16 @@ class Piece(Entity):
self.previous_points = copy.deepcopy(self.points)
self.previous_center = copy.deepcopy(self.center)
for sub_points in self.points:
for point in sub_points:
h = point[0] - self.center[0]
k = point[1] - self.center[1]
new_points = []
for square in self.points:
for vertex in square:
h = vertex[0] - self.center[0]
k = vertex[1] - self.center[1]
point[0] = (k * -1) + self.center[0]
point[1] = h + self.center[1]
vertex[0] = (k * -1) + self.center[0]
vertex[1] = h + self.center[1]
new_points.append([square[-1]] + square[0:-1])
self.points = new_points
def revert(self) -> None:
if self.previous_points and self.previous_center:

View File

@@ -9,15 +9,8 @@ class Well(Entity):
WIDTH = 10 # standard tetris well width, should not be changed
HEIGHT = 20 # standard tetris well height, should not be changed
def __init__(self, position: Tuple):
self.points = self.__get_points(position)
def draw(self, surface: pygame.Surface) -> None:
well_color_hex = ConfigurationManager.configuration["color"]["border"] # TODO Should abstract out color call?
well_color = pygame.Color(well_color_hex)
for sub_points in self.points:
pygame.draw.polygon(surface, well_color, sub_points, 0)
def __init__(self, position: Tuple, color):
super().__init__(self.__get_points(position), color)
def __get_points(self, position: Tuple) -> List:
tile_size = ConfigurationManager.configuration["engine"]["tile-size"]

View File

@@ -16,7 +16,9 @@ class PieceGenerator:
if len(cls.__bucket) == 0:
cls.__generate_bucket()
return Piece(cls.__get_piece_shape(cls.__bucket.pop()), position, cls.__get_piece_color())
border_color = ConfigurationManager.configuration["color"]["border"] # TODO abstract color call to config out?
return Piece(cls.__get_piece_shape(cls.__bucket.pop()), position, cls.__get_piece_color(), border_color)
@classmethod
def __generate_bucket(cls) -> None: