120 lines
4.0 KiB
Python
120 lines
4.0 KiB
Python
import pygame
|
|
from abc import ABC, abstractmethod
|
|
|
|
# Initialize pygame
|
|
pygame.init()
|
|
|
|
# Screen settings
|
|
WIDTH, HEIGHT = 600, 400
|
|
screen = pygame.display.set_mode((WIDTH, HEIGHT))
|
|
pygame.display.set_caption("Select Control Method")
|
|
|
|
# Colors
|
|
WHITE = (255, 255, 255)
|
|
BLACK = (0, 0, 0)
|
|
BLUE = (0, 0, 255)
|
|
RED = (255, 0, 0)
|
|
|
|
# Font
|
|
font = pygame.font.Font(None, 36)
|
|
|
|
# Load images (replace with actual image paths)
|
|
def load_image(path, max_size):
|
|
image = pygame.image.load(path).convert_alpha() # Use convert_alpha for transparency
|
|
image_rect = image.get_rect()
|
|
scale_factor = min(max_size[0] / image_rect.width, max_size[1] / image_rect.height)
|
|
new_size = (int(image_rect.width * scale_factor), int(image_rect.height * scale_factor))
|
|
return pygame.transform.smoothscale(image, new_size)
|
|
|
|
keyboard_image = load_image("keyboard.png", (100, 100))
|
|
#joystick_image = load_image("ps5-dualsense.png", (100, 100))
|
|
joystick_image = load_image("gamepad.png", (100, 100))
|
|
|
|
# Abstract Control Class
|
|
class Control(ABC):
|
|
@abstractmethod
|
|
def handle_input(self, event):
|
|
pass
|
|
|
|
class KeyboardControl(Control):
|
|
def handle_input(self, event):
|
|
if event.type == pygame.KEYDOWN:
|
|
print("Keyboard key pressed")
|
|
|
|
class JoystickControl(Control):
|
|
def __init__(self, joystick_id):
|
|
self.joystick = pygame.joystick.Joystick(joystick_id)
|
|
self.joystick.init()
|
|
|
|
def handle_input(self, event):
|
|
if event.type == pygame.JOYBUTTONDOWN and event.joy == self.joystick.get_id():
|
|
print(f"Joystick {self.joystick.get_id()} button pressed")
|
|
|
|
# Options
|
|
options = ["Keyboard"]
|
|
option_images = {"Keyboard": keyboard_image}
|
|
pygame.joystick.init()
|
|
joystick_count = pygame.joystick.get_count()
|
|
joystick_controls = {}
|
|
|
|
for i in range(joystick_count):
|
|
joystick = pygame.joystick.Joystick(i)
|
|
joystick.init()
|
|
joystick_name = joystick.get_name()
|
|
options.append(joystick_name)
|
|
joystick_controls[joystick_name] = JoystickControl(i)
|
|
option_images[joystick_name] = joystick_image
|
|
|
|
selected_index = 0
|
|
|
|
# Control mapping
|
|
control_classes = {"Keyboard": KeyboardControl}
|
|
control_classes.update(joystick_controls)
|
|
selected_control = KeyboardControl()
|
|
|
|
running = True
|
|
while running:
|
|
screen.fill(WHITE)
|
|
|
|
for event in pygame.event.get():
|
|
if event.type == pygame.QUIT:
|
|
running = False
|
|
elif event.type == pygame.KEYDOWN:
|
|
if event.key == pygame.K_DOWN and selected_index < len(options) - 1:
|
|
selected_index += 1
|
|
elif event.key == pygame.K_UP and selected_index > 0:
|
|
selected_index -= 1
|
|
elif event.key == pygame.K_RETURN:
|
|
selected_control = control_classes[options[selected_index]]() if options[selected_index] == "Keyboard" else joystick_controls[options[selected_index]]
|
|
elif event.type == pygame.MOUSEBUTTONDOWN:
|
|
x, y = event.pos
|
|
for i in range(len(options)):
|
|
if 100 <= x <= 300 and 100 + i * 40 <= y <= 140 + i * 40:
|
|
selected_index = i
|
|
selected_control = control_classes[options[selected_index]]() if options[selected_index] == "Keyboard" else joystick_controls[options[selected_index]]
|
|
break
|
|
|
|
# Pass input to selected control method
|
|
selected_control.handle_input(event)
|
|
|
|
# Render label
|
|
label = font.render("Controls:", True, BLACK)
|
|
screen.blit(label, (50, 50))
|
|
|
|
# Render list
|
|
for i, option in enumerate(options):
|
|
color = RED if i == selected_index else BLACK
|
|
text = font.render(option, True, color)
|
|
screen.blit(text, (100, 100 + i * 40))
|
|
|
|
# Render selected item image
|
|
selected_option = options[selected_index]
|
|
if selected_option in option_images:
|
|
image = option_images[selected_option]
|
|
image_rect = image.get_rect(center=(500, 200)) # Keep aspect ratio
|
|
screen.blit(image, image_rect.topleft)
|
|
|
|
pygame.display.flip()
|
|
|
|
pygame.quit()
|