Project

General

Profile

Pygame » History » Version 2

cristobal hernandez, 12/12/2024 10:03 PM

1 1 cristobal hernandez
h1. Pygame
2 1 cristobal hernandez
3 1 cristobal hernandez
<pre><code class="python">
4 1 cristobal hernandez
import pygame
5 1 cristobal hernandez
6 1 cristobal hernandez
# Joystick
7 2 cristobal hernandez
def inicializar_pygame(self):
8 2 cristobal hernandez
    pygame.init()
9 2 cristobal hernandez
    pygame.joystick.init()
10 1 cristobal hernandez
11 2 cristobal hernandez
    if pygame.joystick.get_count() > 0:
12 2 cristobal hernandez
        self.joystick = pygame.joystick.Joystick(0)
13 2 cristobal hernandez
        self.joystick.init()
14 2 cristobal hernandez
        print(f"Joystick detectado: {self.joystick.get_name()}")
15 2 cristobal hernandez
        self.running = True
16 2 cristobal hernandez
        self.verificar_eventos_pygame()      
17 2 cristobal hernandez
    else:
18 2 cristobal hernandez
        print("No se detectó ningún joystick.")
19 2 cristobal hernandez
        self.joystick = None
20 2 cristobal hernandez
        pygame.quit()
21 2 cristobal hernandez
22 2 cristobal hernandez
def verificar_eventos_pygame(self):
23 2 cristobal hernandez
    if self.combobox_controles.get() != "Joystick":
24 2 cristobal hernandez
        self.running = False
25 2 cristobal hernandez
        pygame.quit()
26 2 cristobal hernandez
        return
27 2 cristobal hernandez
28 2 cristobal hernandez
    for event in pygame.event.get():
29 2 cristobal hernandez
        if pygame.joystick.get_count() == 0:
30 1 cristobal hernandez
            self.running = False
31 2 cristobal hernandez
            messagebox.showinfo("Información", "Se ha perdido la conexión con el mando.")
32 1 cristobal hernandez
            return
33 1 cristobal hernandez
34 2 cristobal hernandez
        if event.type == pygame.JOYHATMOTION:
35 2 cristobal hernandez
            self.manejar_hat(event)
36 1 cristobal hernandez
37 2 cristobal hernandez
        elif event.type in [pygame.JOYBUTTONDOWN, pygame.JOYBUTTONUP]:
38 2 cristobal hernandez
            self.manejar_botones(event)
39 1 cristobal hernandez
40 2 cristobal hernandez
        elif self.joystick:
41 2 cristobal hernandez
            self.manejar_joystick()
42 1 cristobal hernandez
43 2 cristobal hernandez
    self.root.after(60, self.verificar_eventos_pygame)
44 1 cristobal hernandez
45 2 cristobal hernandez
def manejar_hat(self, event):
46 2 cristobal hernandez
    hat_value = self.joystick.get_hat(0)
47 2 cristobal hernandez
    direcciones = {(0, 1): self.moveUp, (0, -1): self.moveDown, (-1, 0): self.moveLeft, (1, 0): self.moveRight}
48 1 cristobal hernandez
49 2 cristobal hernandez
    if hat_value != (0, 0) and not self.key_pressed and hat_value in direcciones:
50 2 cristobal hernandez
        direcciones[hat_value]()
51 2 cristobal hernandez
        self.canvas.itemconfig(self.contenedor[hat_value], fill="#08FBF9")
52 2 cristobal hernandez
        self.tecla_pressed = hat_value
53 2 cristobal hernandez
        self.key_pressed = True
54 2 cristobal hernandez
    elif hat_value == (0, 0) and self.tecla_pressed in direcciones:
55 2 cristobal hernandez
        self.stop()
56 2 cristobal hernandez
        self.canvas.itemconfig(self.contenedor[self.tecla_pressed], fill="#0F2B6A")
57 2 cristobal hernandez
        self.key_pressed = False
58 2 cristobal hernandez
        self.tecla_pressed = None
59 1 cristobal hernandez
60 2 cristobal hernandez
def manejar_botones(self, event):
61 2 cristobal hernandez
    button_mapping = {
62 2 cristobal hernandez
        0: self.grab,    # button_x
63 2 cristobal hernandez
        1: self.drop,    # button_circle
64 2 cristobal hernandez
        8: self.upCraw,  # button_l2
65 2 cristobal hernandez
        9: self.downCraw # button_r2
66 2 cristobal hernandez
    }
67 2 cristobal hernandez
68 2 cristobal hernandez
    if event.type == pygame.JOYBUTTONDOWN and event.button in button_mapping:
69 2 cristobal hernandez
        self.botones_presionados.add(event.button)
70 2 cristobal hernandez
        if not self.key_pressed:
71 2 cristobal hernandez
            button_mapping[event.button]()
72 2 cristobal hernandez
            self.tecla_pressed = event.button
73 1 cristobal hernandez
            self.key_pressed = True
74 2 cristobal hernandez
        self.canvas.itemconfig(self.contenedor[self.tecla_pressed], fill="#08FBF9")
75 2 cristobal hernandez
76 2 cristobal hernandez
    elif event.type == pygame.JOYBUTTONUP and event.button in self.botones_presionados:
77 2 cristobal hernandez
        self.botones_presionados.discard(event.button)
78 2 cristobal hernandez
        if event.button == self.tecla_pressed:
79 1 cristobal hernandez
            self.canvas.itemconfig(self.contenedor[self.tecla_pressed], fill="#0F2B6A")
80 1 cristobal hernandez
            self.key_pressed = False
81 1 cristobal hernandez
            self.tecla_pressed = None
82 1 cristobal hernandez
83 2 cristobal hernandez
def manejar_joystick(self):
84 2 cristobal hernandez
    eje_x = self.joystick.get_axis(0)
85 2 cristobal hernandez
    eje_y = self.joystick.get_axis(1)
86 1 cristobal hernandez
87 2 cristobal hernandez
    eje_x_redondeado = round(eje_x, 1)
88 2 cristobal hernandez
    eje_y_redondeado = round(eje_y, 1)
89 1 cristobal hernandez
90 2 cristobal hernandez
    if abs(eje_x_redondeado) < 0.9 and abs(eje_y_redondeado) < 0.9:
91 2 cristobal hernandez
        if self.tecla_pressed == "c":
92 2 cristobal hernandez
            self.stop()
93 2 cristobal hernandez
            self.canvas.itemconfig(self.contenedor["c"], fill="white")
94 2 cristobal hernandez
            self.key_pressed = False
95 2 cristobal hernandez
            self.tecla_pressed = None
96 1 cristobal hernandez
97 2 cristobal hernandez
    else:
98 2 cristobal hernandez
        direcciones_x = {1: self.moveRight, -1: self.moveLeft}
99 2 cristobal hernandez
        direcciones_y = {1: self.moveDown, -1: self.moveUp}
100 1 cristobal hernandez
101 2 cristobal hernandez
        if eje_x_redondeado > 0.89 and not self.key_pressed:  # Derecha
102 2 cristobal hernandez
            direcciones_x[1]()
103 2 cristobal hernandez
            self.canvas.itemconfig(self.contenedor["c"], fill="#08FBF9")
104 2 cristobal hernandez
            self.tecla_pressed = "c"
105 2 cristobal hernandez
            self.key_pressed = True
106 1 cristobal hernandez
107 2 cristobal hernandez
        elif eje_x_redondeado < -0.89 and not self.key_pressed:  # Izquierda
108 2 cristobal hernandez
            direcciones_x[-1]()
109 2 cristobal hernandez
            self.canvas.itemconfig(self.contenedor["c"], fill="#08FBF9")
110 2 cristobal hernandez
            self.tecla_pressed = "c"
111 2 cristobal hernandez
            self.key_pressed = True
112 1 cristobal hernandez
113 2 cristobal hernandez
        elif eje_y_redondeado > 0.89 and not self.key_pressed:  # Abajo
114 2 cristobal hernandez
            direcciones_y[1]()
115 2 cristobal hernandez
            self.canvas.itemconfig(self.contenedor["c"], fill="#08FBF9")
116 2 cristobal hernandez
            self.tecla_pressed = "c"
117 2 cristobal hernandez
            self.key_pressed = True
118 1 cristobal hernandez
119 2 cristobal hernandez
        elif eje_y_redondeado < -0.89 and not self.key_pressed:  # Arriba
120 2 cristobal hernandez
            direcciones_y[-1]()
121 2 cristobal hernandez
            self.canvas.itemconfig(self.contenedor["c"], fill="#08FBF9")
122 2 cristobal hernandez
            self.tecla_pressed = "c"
123 2 cristobal hernandez
            self.key_pressed = True
124 1 cristobal hernandez
125 1 cristobal hernandez
126 2 cristobal hernandez
    nuevo_x = 350 + eje_x * 50
127 2 cristobal hernandez
    nuevo_y = 275 + eje_y * 50
128 2 cristobal hernandez
    self.pos_x = nuevo_x
129 2 cristobal hernandez
    self.pos_y = nuevo_y
130 2 cristobal hernandez
    self.canvas.coords(self.contenedor["c"], self.pos_x - 10, self.pos_y - 10, self.pos_x + 10, self.pos_y + 10)
131 1 cristobal hernandez
</code></pre>