#28 Working on motion repeat functionality. /JL
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -168,3 +168,4 @@ cython_debug/
|
|||||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||||
#.idea/
|
#.idea/
|
||||||
|
|
||||||
|
highscore.txt
|
||||||
|
55
pman.py
55
pman.py
@@ -7,8 +7,16 @@ from actors.ghost_mode_controller import GhostModeController
|
|||||||
from hud import HUD
|
from hud import HUD
|
||||||
from maze import Maze
|
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):
|
def spawn_ghosts(center_position):
|
||||||
"""
|
"""
|
||||||
@@ -29,9 +37,31 @@ def spawn_ghosts(center_position):
|
|||||||
|
|
||||||
return pygame.sprite.Group(blinky, pinky, inky, clyde)
|
return pygame.sprite.Group(blinky, pinky, inky, clyde)
|
||||||
|
|
||||||
def main() -> None:
|
def handle_hat_repeat():
|
||||||
pygame.init()
|
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()
|
joystick_count = pygame.joystick.get_count()
|
||||||
|
|
||||||
joysticks = {}
|
joysticks = {}
|
||||||
@@ -53,7 +83,7 @@ def main() -> None:
|
|||||||
running = True
|
running = True
|
||||||
while running:
|
while running:
|
||||||
screen.fill(Colors.Black.value)
|
screen.fill(Colors.Black.value)
|
||||||
|
pygame.event.pump()
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
match event.type:
|
match event.type:
|
||||||
case pygame.QUIT:
|
case pygame.QUIT:
|
||||||
@@ -81,7 +111,12 @@ def main() -> None:
|
|||||||
player.direction = PlayerDirection.DirectionDown
|
player.direction = PlayerDirection.DirectionDown
|
||||||
player.move_down()
|
player.move_down()
|
||||||
case pygame.JOYHATMOTION:
|
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 hat_x == 1:
|
||||||
if maze.is_wall(player.location[0] + 1, player.location[1]):
|
if maze.is_wall(player.location[0] + 1, player.location[1]):
|
||||||
break
|
break
|
||||||
@@ -112,8 +147,15 @@ def main() -> None:
|
|||||||
case pygame.JOYDEVICEREMOVED:
|
case pygame.JOYDEVICEREMOVED:
|
||||||
del joysticks[event.instance_id]
|
del joysticks[event.instance_id]
|
||||||
print(f"Joystick {event.instance_id} disconnected")
|
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:
|
# In your main game loop:
|
||||||
|
handle_hat_repeat()
|
||||||
hud.draw(screen)
|
hud.draw(screen)
|
||||||
maze.draw(screen)
|
maze.draw(screen)
|
||||||
player.update()
|
player.update()
|
||||||
@@ -129,6 +171,9 @@ def main() -> None:
|
|||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
clock.tick(60)
|
clock.tick(60)
|
||||||
|
|
||||||
|
if joysticks:
|
||||||
|
joysticks[joy.get_instance_id()].controllers[0].close()
|
||||||
|
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Reference in New Issue
Block a user