integrated dev menu into game with secret code

This commit is contained in:
Nicholas Dyer 2023-05-14 19:26:09 -04:00
parent 8a34ed7c01
commit 1c467371a8
3 changed files with 25 additions and 3 deletions

View File

@ -30,6 +30,7 @@ class GameSurface(pygame.Surface):
self.game_fps = game_fps
self._input_handler = InputHandler(self._clock)
self.additional_args = {}
self.dev_override = False
self.bg = pygame.image.load(self.resource_dir + '/images/bg.png').convert_alpha()
self.sprites = pygame.sprite.Group()
@ -45,3 +46,7 @@ class GameSurface(pygame.Surface):
self.sprites.draw(self)
self._input_handler.update()
if self._input_handler.dev_found:
self.next_surface = 'dev_menu'
self.dev_override = True
self.running = False

View File

@ -4,6 +4,7 @@ import pocket_friends
import importlib
valid_surfaces = [
'dev_menu',
'title',
'egg_select',
'selection_info',
@ -13,6 +14,7 @@ valid_surfaces = [
# Add all the surface modules to a dictionary for easy switching
surface_modules = {'dev_menu': importlib.import_module('pocket_friends.development.{0}'.format('dev_menu'))}
for module in valid_surfaces:
if module != 'dev_menu':
surface_modules[module] = importlib.import_module('pocket_friends.surfaces.{0}'.format(module))
starting_surface = 'title'
@ -62,9 +64,13 @@ def start_game(resolution=240):
window.blit(frame, frame.get_rect())
if not surface.running:
if surface.dev_override:
next_surface = 'dev_menu'
else:
next_surface = surface.next_surface
additional_args = surface.additional_args
if next_surface not in valid_surfaces:
print(next)
next_surface = 'error_screen'
surface = surface_modules.get(next_surface).Surface((game_res, game_res), resources_dir,
game_fps, **additional_args)

View File

@ -1,5 +1,5 @@
import pygame
from collections import deque
class InputHandler:
"""
@ -24,6 +24,12 @@ class InputHandler:
self.clock = pygame_clock
self.tick_check = tick_check
self.last_input_tick = 0
self.dev_check = deque()
self.dev_code = deque()
for button in [pygame.K_DOWN, pygame.K_DOWN, pygame.K_UP, pygame.K_UP, pygame.K_DOWN, pygame.K_LEFT,
pygame.K_RIGHT, pygame.K_LEFT, pygame.K_a]:
self.dev_code.append(button)
self.dev_found = False
def create_event(self, pressed_button):
"""
@ -37,6 +43,9 @@ class InputHandler:
if pygame.time.get_ticks() - self.last_input_tick > self.clock.get_time() * 2:
pygame.event.post(pygame.event.Event(pygame.KEYDOWN, {'key': pressed_button}))
pygame.event.post(pygame.event.Event(pygame.KEYUP, {'key': pressed_button}))
self.dev_check.append(pressed_button)
if len(self.dev_check) > len(self.dev_code):
self.dev_check.popleft()
else:
pygame.event.post(pygame.event.Event(pygame.KEYDOWN, {'key': pressed_button}))
pygame.event.post(pygame.event.Event(pygame.KEYUP, {'key': pressed_button}))
@ -70,3 +79,5 @@ class InputHandler:
def update(self):
"""Run either the GPIO handler or the keyboard handler to check for input and create events."""
self.handle_keyboard()
if self.dev_code == self.dev_check:
self.dev_found = True