feat: implement gravity on current piece
This commit is contained in:
@@ -40,9 +40,11 @@ class Tetris:
|
||||
# gets called from the games main loop
|
||||
def update(self) -> None:
|
||||
# TODO write not initialized exception
|
||||
elapsed_time = self.clock.tick(self.fps)
|
||||
|
||||
if self.current_piece:
|
||||
self.current_piece.update(elapsed_time, self.well, self.stack)
|
||||
|
||||
self.clock.tick(self.fps)
|
||||
|
||||
# TODO create control utility class
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
|
||||
@@ -8,9 +8,10 @@ class Entity:
|
||||
self.points = points
|
||||
self.color = color
|
||||
self.border_color = border_color
|
||||
self.elapsed_time = 0
|
||||
|
||||
def update(self) -> None:
|
||||
pass
|
||||
def update(self, elapsed_time) -> None:
|
||||
self.elapsed_time += elapsed_time
|
||||
|
||||
def draw(self, surface: pygame.Surface) -> None:
|
||||
tile_size = ConfigurationManager.configuration["engine"]["tile-size"]
|
||||
@@ -20,7 +21,7 @@ class Entity:
|
||||
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: # TODO figure out how to do type hint for entity param of type Entity
|
||||
for square_one in self.points:
|
||||
for square_two in entity.points:
|
||||
for i in range(4): # 4 vertices
|
||||
|
||||
@@ -9,7 +9,9 @@ from util.ConfigurationManager import ConfigurationManager
|
||||
https://tetris.fandom.com/wiki/Tetromino
|
||||
'''
|
||||
class Piece(Entity):
|
||||
|
||||
|
||||
GRAVITY = 800 # A move down every 800 ms
|
||||
|
||||
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)
|
||||
@@ -17,7 +19,24 @@ class Piece(Entity):
|
||||
self.previous_points = None
|
||||
self.previous_center = None
|
||||
|
||||
def update(self, elapsed_time: int, well: Entity, stack: Entity):
|
||||
super().update(elapsed_time)
|
||||
|
||||
tile_size = ConfigurationManager.configuration["engine"]["tile-size"]
|
||||
if self.elapsed_time >= Piece.GRAVITY:
|
||||
self.elapsed_time -= Piece.GRAVITY
|
||||
|
||||
self.move((0, tile_size))
|
||||
if well and self.collide(well):
|
||||
self.revert()
|
||||
if stack and self.collide(stack):
|
||||
self.revert()
|
||||
|
||||
def move(self, vector: Tuple) -> None:
|
||||
# reset elapsed time if user moves down to stall gravity
|
||||
if vector[1] > 0:
|
||||
self.elapsed_time = 0
|
||||
|
||||
self.previous_points = copy.deepcopy(self.points)
|
||||
self.previous_center = copy.deepcopy(self.center)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user