Pygame¶
import pygame
def inicializar_pygame(self):
pygame.init()
pygame.joystick.init()
if pygame.joystick.get_count() > 0:
self.joystick = pygame.joystick.Joystick(0)
self.joystick.init()
print(f"Joystick detectado: {self.joystick.get_name()}")
self.running = True
self.verificar_eventos_pygame()
else:
print("No se detectó ningún joystick.")
self.joystick = None
pygame.quit()
def verificar_eventos_pygame(self):
if self.combobox_controles.get() != "Joystick":
self.running = False
pygame.quit()
return
for event in pygame.event.get():
if pygame.joystick.get_count() == 0:
self.running = False
messagebox.showinfo("Información", "Se ha perdido la conexión con el mando.")
return
if event.type == pygame.JOYHATMOTION:
self.manejar_hat(event)
elif event.type in [pygame.JOYBUTTONDOWN, pygame.JOYBUTTONUP]:
self.manejar_botones(event)
elif self.joystick:
self.manejar_joystick()
self.root.after(60, self.verificar_eventos_pygame)
def manejar_hat(self, event):
hat_value = self.joystick.get_hat(0)
direcciones = {(0, 1): self.moveUp, (0, -1): self.moveDown, (-1, 0): self.moveLeft, (1, 0): self.moveRight}
if hat_value != (0, 0) and not self.key_pressed and hat_value in direcciones:
direcciones[hat_value]()
self.canvas.itemconfig(self.contenedor[hat_value], fill="#08FBF9")
self.tecla_pressed = hat_value
self.key_pressed = True
elif hat_value == (0, 0) and self.tecla_pressed in direcciones:
self.stop()
self.canvas.itemconfig(self.contenedor[self.tecla_pressed], fill="#0F2B6A")
self.key_pressed = False
self.tecla_pressed = None
def manejar_botones(self, event):
button_mapping = {
0: self.grab,
1: self.drop,
8: self.upCraw,
9: self.downCraw
}
if event.type == pygame.JOYBUTTONDOWN and event.button in button_mapping:
self.botones_presionados.add(event.button)
if not self.key_pressed:
button_mapping[event.button]()
self.tecla_pressed = event.button
self.key_pressed = True
self.canvas.itemconfig(self.contenedor[self.tecla_pressed], fill="#08FBF9")
elif event.type == pygame.JOYBUTTONUP and event.button in self.botones_presionados:
self.botones_presionados.discard(event.button)
if event.button == self.tecla_pressed:
self.canvas.itemconfig(self.contenedor[self.tecla_pressed], fill="#0F2B6A")
self.key_pressed = False
self.tecla_pressed = None
def manejar_joystick(self):
eje_x = self.joystick.get_axis(0)
eje_y = self.joystick.get_axis(1)
eje_x_redondeado = round(eje_x, 1)
eje_y_redondeado = round(eje_y, 1)
if abs(eje_x_redondeado) < 0.9 and abs(eje_y_redondeado) < 0.9:
if self.tecla_pressed == "c":
self.stop()
self.canvas.itemconfig(self.contenedor["c"], fill="white")
self.key_pressed = False
self.tecla_pressed = None
else:
direcciones_x = {1: self.moveRight, -1: self.moveLeft}
direcciones_y = {1: self.moveDown, -1: self.moveUp}
if eje_x_redondeado > 0.89 and not self.key_pressed:
direcciones_x[1]()
self.canvas.itemconfig(self.contenedor["c"], fill="#08FBF9")
self.tecla_pressed = "c"
self.key_pressed = True
elif eje_x_redondeado < -0.89 and not self.key_pressed:
direcciones_x[-1]()
self.canvas.itemconfig(self.contenedor["c"], fill="#08FBF9")
self.tecla_pressed = "c"
self.key_pressed = True
elif eje_y_redondeado > 0.89 and not self.key_pressed:
direcciones_y[1]()
self.canvas.itemconfig(self.contenedor["c"], fill="#08FBF9")
self.tecla_pressed = "c"
self.key_pressed = True
elif eje_y_redondeado < -0.89 and not self.key_pressed:
direcciones_y[-1]()
self.canvas.itemconfig(self.contenedor["c"], fill="#08FBF9")
self.tecla_pressed = "c"
self.key_pressed = True
nuevo_x = 350 + eje_x * 50
nuevo_y = 275 + eje_y * 50
self.pos_x = nuevo_x
self.pos_y = nuevo_y
self.canvas.coords(self.contenedor["c"], self.pos_x - 10, self.pos_y - 10, self.pos_x + 10, self.pos_y + 10)