feat: add collision logic between entities
This commit is contained in:
18
Tetris.py
18
Tetris.py
@@ -28,7 +28,7 @@ class Tetris:
|
|||||||
self.tile_size = ConfigurationManager.configuration["engine"]["tile-size"]
|
self.tile_size = ConfigurationManager.configuration["engine"]["tile-size"]
|
||||||
self.screen = pygame.display.set_mode((win_width, win_height))
|
self.screen = pygame.display.set_mode((win_width, win_height))
|
||||||
self.clock = pygame.time.Clock()
|
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)
|
loaded_icon = pygame.image.load(win_icon)
|
||||||
pygame.display.set_caption(win_title)
|
pygame.display.set_caption(win_title)
|
||||||
@@ -49,18 +49,26 @@ class Tetris:
|
|||||||
if self.current_piece:
|
if self.current_piece:
|
||||||
if event.key == pygame.K_SPACE:
|
if event.key == pygame.K_SPACE:
|
||||||
self.current_piece.rotate()
|
self.current_piece.rotate()
|
||||||
|
if self.well and self.current_piece.collide(self.well):
|
||||||
|
self.current_piece.revert()
|
||||||
if event.key == pygame.K_LEFT:
|
if event.key == pygame.K_LEFT:
|
||||||
self.current_piece.move((-self.tile_size, 0))
|
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:
|
if event.key == pygame.K_RIGHT:
|
||||||
self.current_piece.move((self.tile_size, 0))
|
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:
|
if event.key == pygame.K_UP:
|
||||||
self.current_piece.move((0, -self.tile_size))
|
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:
|
if event.key == pygame.K_DOWN:
|
||||||
self.current_piece.move((0, self.tile_size))
|
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:
|
if event.key == pygame.K_z:
|
||||||
self.__generate_piece((300, 100))
|
self.__generate_piece((300, 100))
|
||||||
if event.key == pygame.K_x:
|
|
||||||
self.current_piece.revert()
|
|
||||||
|
|
||||||
def draw(self) -> None:
|
def draw(self) -> None:
|
||||||
# TODO write not initialized exception
|
# TODO write not initialized exception
|
||||||
@@ -71,9 +79,9 @@ class Tetris:
|
|||||||
|
|
||||||
# draw all game objects
|
# draw all game objects
|
||||||
if self.well:
|
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:
|
if self.current_piece:
|
||||||
self.current_piece.draw(self.screen)
|
self.current_piece.draw(self.screen, self.tile_size)
|
||||||
|
|
||||||
# update display
|
# update display
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
|
|||||||
@@ -1,14 +1,25 @@
|
|||||||
|
from typing import Tuple
|
||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
class Entity:
|
class Entity:
|
||||||
def __init__(self):
|
def __init__(self, points: Tuple, color: str, border_color: str = None):
|
||||||
pass
|
self.points = points
|
||||||
|
self.color = color
|
||||||
|
self.border_color = border_color
|
||||||
|
|
||||||
def update(self) -> None:
|
def update(self) -> None:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def draw(self, surface: pygame.Surface) -> None:
|
def draw(self, surface: pygame.Surface, tile_size) -> None:
|
||||||
pass
|
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:
|
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
|
||||||
|
|||||||
@@ -11,27 +11,13 @@ from util.ConfigurationManager import ConfigurationManager
|
|||||||
'''
|
'''
|
||||||
class Piece(Entity):
|
class Piece(Entity):
|
||||||
|
|
||||||
def __init__(self, shape: Tuple, position: Tuple, color: str):
|
def __init__(self, shape: Tuple, position: Tuple, color: str, border_color: str):
|
||||||
super().__init__()
|
super().__init__(self.__get_points(shape, position), color, border_color)
|
||||||
|
|
||||||
self.color = color
|
|
||||||
self.points = self.__get_points(shape, position)
|
|
||||||
self.center = self.__get_center(shape, position)
|
self.center = self.__get_center(shape, position)
|
||||||
|
|
||||||
self.previous_points = None
|
self.previous_points = None
|
||||||
self.previous_center = 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:
|
def move(self, vector: Tuple) -> None:
|
||||||
self.previous_points = copy.deepcopy(self.points)
|
self.previous_points = copy.deepcopy(self.points)
|
||||||
self.previous_center = copy.deepcopy(self.center)
|
self.previous_center = copy.deepcopy(self.center)
|
||||||
@@ -52,13 +38,16 @@ class Piece(Entity):
|
|||||||
self.previous_points = copy.deepcopy(self.points)
|
self.previous_points = copy.deepcopy(self.points)
|
||||||
self.previous_center = copy.deepcopy(self.center)
|
self.previous_center = copy.deepcopy(self.center)
|
||||||
|
|
||||||
for sub_points in self.points:
|
new_points = []
|
||||||
for point in sub_points:
|
for square in self.points:
|
||||||
h = point[0] - self.center[0]
|
for vertex in square:
|
||||||
k = point[1] - self.center[1]
|
h = vertex[0] - self.center[0]
|
||||||
|
k = vertex[1] - self.center[1]
|
||||||
|
|
||||||
point[0] = (k * -1) + self.center[0]
|
vertex[0] = (k * -1) + self.center[0]
|
||||||
point[1] = h + self.center[1]
|
vertex[1] = h + self.center[1]
|
||||||
|
new_points.append([square[-1]] + square[0:-1])
|
||||||
|
self.points = new_points
|
||||||
|
|
||||||
def revert(self) -> None:
|
def revert(self) -> None:
|
||||||
if self.previous_points and self.previous_center:
|
if self.previous_points and self.previous_center:
|
||||||
|
|||||||
@@ -9,15 +9,8 @@ class Well(Entity):
|
|||||||
WIDTH = 10 # standard tetris well width, should not be changed
|
WIDTH = 10 # standard tetris well width, should not be changed
|
||||||
HEIGHT = 20 # standard tetris well height, should not be changed
|
HEIGHT = 20 # standard tetris well height, should not be changed
|
||||||
|
|
||||||
def __init__(self, position: Tuple):
|
def __init__(self, position: Tuple, color):
|
||||||
self.points = self.__get_points(position)
|
super().__init__(self.__get_points(position), color)
|
||||||
|
|
||||||
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 __get_points(self, position: Tuple) -> List:
|
def __get_points(self, position: Tuple) -> List:
|
||||||
tile_size = ConfigurationManager.configuration["engine"]["tile-size"]
|
tile_size = ConfigurationManager.configuration["engine"]["tile-size"]
|
||||||
|
|||||||
@@ -16,7 +16,9 @@ class PieceGenerator:
|
|||||||
if len(cls.__bucket) == 0:
|
if len(cls.__bucket) == 0:
|
||||||
cls.__generate_bucket()
|
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
|
@classmethod
|
||||||
def __generate_bucket(cls) -> None:
|
def __generate_bucket(cls) -> None:
|
||||||
|
|||||||
Reference in New Issue
Block a user