Hi,

To do this you would implement states. There are many ways to manage states, from organic to design pattern. For very simple games it's common to use a member attribute and some code (e.g. brick.state, and an if:elif:else block that's sensitive to brick.state) to handle the brick according to its state. (You can do more fancy state concepts, but they would take a much longer explanation.)

For example, store a number in brick.state:
0 = init
1 = falling
2 = collided with ground
3 = locked on ground

The brick would default to 0=init while waiting to be placed into play.

Then it would transition to 1=falling, where the player would have some control.

Next, collision with the ground would be detected, and trigger transition to 2=collide_with_ground; and a timer would be started to count elapsed time. I would store the elapsed time on the brick, but there are other ways to do it. Accumulate the elapsed time for every clock tick and check for expiration in code.

When the timer expires, then transition the brick to 3=locked_on_ground and do your win/end game checks.

Hope that helps.

On 5/9/2021 9:09 AM, Jan Gregorczyk wrote:
Hi!

I'm trying to create simple Tetris clone. I want to implement this: https://tetris.fandom.com/wiki/Lock_delay <https://tetris.fandom.com/wiki/Lock_delay> I need 0.5 seconds with player's control over the brick. I have no idea how to do that using pygame clocks.

You can find my full code here: https://github.com/yanazPL/Tetris/blob/main/main.py <https://github.com/yanazPL/Tetris/blob/main/main.py> (it's not finished)

Skeleton of my code is below:
classBrick:
"""Represents active brick which player can control"""
deftouches_ground(self):
"""Checks whether brick is touching last row of world"""
deftouches_tile(self):
"""Checks if brick touches any non-brick tile"""
deffreeze(self):
"""Ends control of player over the bricks"""
classGameState:
defupdate(self):
"""Non player controlled after-move actions are here"""
if(self.brick.touches_ground() or
self.brick.touches_tile()):
# I WANT TIME FOR MOVEMENT/ROTATION HERE
self.brick.freeze()
self._clear_lines()
self.respawn()
else:
self.move_bricks_down()
# check lines
defmove_bricks_down(self):
epoch_dividor = 20
brick = self.brick
ifself.epoch >= epoch_dividor:
brick.move(
(brick.position[0], brick.position[1] + 1)
)
self.epoch = 0
self.epoch += 1
classUserInterface():
""" user actions and game state. Uses pyGame"""
def__init__(self):
pygame.init()
# Game state
self.game_state = GameState()
#window management here ...
self.clock = pygame.time.Clock()
self.running = True
defreset(self):
self.game_state = GameState()
self.clock = pygame.time.Clock()
defprocess_input(self):
"""Processing inupt here ..."""
defupdate(self):
self.game_state.update()
ifself.game_state.game_lost():
self.running = False
defdraw(self):
"""Drawing tiles with approperiate colors ..."""
defrun(self):
"""Runs the game loop"""
whileTrue:
self.process_input()
ifself.running:
self.update()
self.draw()
self.clock.tick(UserInterface.FPS)
if__name__== "__main__":
UserInterface().run()

Reply via email to