#28 Working on motion repeat functionality. /JL
This commit is contained in:
55
pman.py
55
pman.py
@@ -7,8 +7,16 @@ from actors.ghost_mode_controller import GhostModeController
|
||||
from hud import HUD
|
||||
from maze import Maze
|
||||
|
||||
__version__ = "0.3.3"
|
||||
__version__ = "0.3.4"
|
||||
|
||||
# Constants
|
||||
HAT_REPEAT_DELAY = 300 # milliseconds before first repeat
|
||||
HAT_REPEAT_INTERVAL = 100 # milliseconds between repeats
|
||||
|
||||
# State
|
||||
hat_direction = (0, 0)
|
||||
hat_timer = 0
|
||||
hat_first_press = True
|
||||
|
||||
def spawn_ghosts(center_position):
|
||||
"""
|
||||
@@ -29,9 +37,31 @@ def spawn_ghosts(center_position):
|
||||
|
||||
return pygame.sprite.Group(blinky, pinky, inky, clyde)
|
||||
|
||||
def main() -> None:
|
||||
pygame.init()
|
||||
def handle_hat_repeat():
|
||||
global hat_timer, hat_first_press
|
||||
now = pygame.time.get_ticks()
|
||||
if hat_direction != (0, 0):
|
||||
if hat_first_press:
|
||||
if now - hat_timer >= HAT_REPEAT_DELAY:
|
||||
hat_timer = now
|
||||
hat_first_press = False
|
||||
post_hat_repeat_event()
|
||||
else:
|
||||
if now - hat_timer >= HAT_REPEAT_INTERVAL:
|
||||
hat_timer = now
|
||||
post_hat_repeat_event()
|
||||
|
||||
def post_hat_repeat_event():
|
||||
pygame.event.post(pygame.event.Event(pygame.USEREVENT, {
|
||||
"type_name": "JOYHATREPEAT",
|
||||
"value": hat_direction
|
||||
}))
|
||||
|
||||
def main() -> None:
|
||||
global hat_direction, hat_timer, hat_first_press
|
||||
|
||||
pygame.init()
|
||||
pygame.key.set_repeat(200)
|
||||
joystick_count = pygame.joystick.get_count()
|
||||
|
||||
joysticks = {}
|
||||
@@ -53,7 +83,7 @@ def main() -> None:
|
||||
running = True
|
||||
while running:
|
||||
screen.fill(Colors.Black.value)
|
||||
|
||||
pygame.event.pump()
|
||||
for event in pygame.event.get():
|
||||
match event.type:
|
||||
case pygame.QUIT:
|
||||
@@ -81,7 +111,12 @@ def main() -> None:
|
||||
player.direction = PlayerDirection.DirectionDown
|
||||
player.move_down()
|
||||
case pygame.JOYHATMOTION:
|
||||
hat_x, hat_y = event.value
|
||||
hat_x, hat_y = hat_direction = event.value
|
||||
hat_timer = pygame.time.get_ticks()
|
||||
hat_first_press = True
|
||||
|
||||
if event.type == pygame.USEREVENT and event.dict.get("type_name") == "JOYHATREPEAT":
|
||||
print(f"Repeated hat: {event.dict['value']}")
|
||||
if hat_x == 1:
|
||||
if maze.is_wall(player.location[0] + 1, player.location[1]):
|
||||
break
|
||||
@@ -112,8 +147,15 @@ def main() -> None:
|
||||
case pygame.JOYDEVICEREMOVED:
|
||||
del joysticks[event.instance_id]
|
||||
print(f"Joystick {event.instance_id} disconnected")
|
||||
|
||||
match maze.collect_dot(player.location[0], player.location[1]):
|
||||
case "dot":
|
||||
hud.add_points(10)
|
||||
case "power":
|
||||
hud.add_points(50)
|
||||
|
||||
# In your main game loop:
|
||||
handle_hat_repeat()
|
||||
hud.draw(screen)
|
||||
maze.draw(screen)
|
||||
player.update()
|
||||
@@ -129,6 +171,9 @@ def main() -> None:
|
||||
pygame.display.flip()
|
||||
clock.tick(60)
|
||||
|
||||
if joysticks:
|
||||
joysticks[joy.get_instance_id()].controllers[0].close()
|
||||
|
||||
pygame.quit()
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
Reference in New Issue
Block a user