Project

General

Profile

Interfaz » History » Version 7

cristobal hernandez, 12/12/2024 08:46 PM

1 1 ana gutierrez
h1. Interfaz
2 4 cristobal hernandez
3 5 cristobal hernandez
<pre><code class="python">
4 5 cristobal hernandez
import tkinter as tk
5 5 cristobal hernandez
6 5 cristobal hernandez
class Aplicacion:
7 5 cristobal hernandez
    def __init__(self, root):
8 5 cristobal hernandez
        self.root = root
9 5 cristobal hernandez
        self.root.title("Blitz")
10 5 cristobal hernandez
        self.root.geometry("800x450")
11 5 cristobal hernandez
        self.root.resizable(0, 0)
12 5 cristobal hernandez
13 5 cristobal hernandez
        # Cargar el logo
14 5 cristobal hernandez
        self.root.iconbitmap("resources/logo.ico")
15 5 cristobal hernandez
16 5 cristobal hernandez
        # Cargar la imagen de fondo
17 5 cristobal hernandez
        self.imagen_fondo = tk.PhotoImage(file="resources/fondo1.png")
18 5 cristobal hernandez
        
19 5 cristobal hernandez
        # Crear un Canvas para colocar la imagen de fondo
20 5 cristobal hernandez
        self.canvas = tk.Canvas(self.root, width=800, height=400)
21 5 cristobal hernandez
        self.canvas.pack(fill="both", expand=True)
22 5 cristobal hernandez
23 5 cristobal hernandez
        # Colocar la imagen en el Canvas
24 5 cristobal hernandez
        self.canvas.create_image(0, 0, image=self.imagen_fondo, anchor="nw")
25 5 cristobal hernandez
        
26 5 cristobal hernandez
        # Imágenes para los botones
27 5 cristobal hernandez
        self.imagen_central = tk.PhotoImage(file="resources/center.png")
28 5 cristobal hernandez
        self.imagen_arriba = tk.PhotoImage(file="resources/up.png")
29 5 cristobal hernandez
        self.imagen_abajo = tk.PhotoImage(file="resources/down.png")
30 5 cristobal hernandez
        self.imagen_izquierda = tk.PhotoImage(file="resources/left.png")
31 5 cristobal hernandez
        self.imagen_derecha = tk.PhotoImage(file="resources/right.png")
32 5 cristobal hernandez
        self.imagen_apagado = tk.PhotoImage(file="resources/off.png")
33 5 cristobal hernandez
34 5 cristobal hernandez
        
35 5 cristobal hernandez
        # Cuadro "transparente"
36 5 cristobal hernandez
        self.canvas.create_rectangle(50, 210, 230, 390, fill="#052D45", stipple="gray50", outline="")
37 5 cristobal hernandez
        
38 5 cristobal hernandez
        self.crear_botones()
39 5 cristobal hernandez
40 5 cristobal hernandez
    def crear_botones(self):
41 5 cristobal hernandez
        self.boton_central = tk.Button(self.root, image=self.imagen_central, borderwidth=0, highlightthickness=0, cursor="hand2")
42 5 cristobal hernandez
        self.canvas.create_window(140, 300, window=self.boton_central)
43 5 cristobal hernandez
        
44 5 cristobal hernandez
        self.boton_arriba = tk.Button(self.root, image=self.imagen_arriba, borderwidth=0, highlightthickness=0, cursor="hand2")
45 5 cristobal hernandez
        self.canvas.create_window(140, 245, window=self.boton_arriba)
46 5 cristobal hernandez
        
47 5 cristobal hernandez
        self.boton_abajo = tk.Button(self.root, image=self.imagen_abajo, borderwidth=0, highlightthickness=0, cursor="hand2")
48 5 cristobal hernandez
        self.canvas.create_window(140, 355, window=self.boton_abajo)
49 5 cristobal hernandez
        
50 5 cristobal hernandez
        self.boton_izquierda = tk.Button(self.root, image=self.imagen_izquierda, borderwidth=0, highlightthickness=0, cursor="hand2")
51 5 cristobal hernandez
        self.canvas.create_window(85, 300, window=self.boton_izquierda)
52 5 cristobal hernandez
        
53 5 cristobal hernandez
        self.boton_derecha = tk.Button(self.root, image=self.imagen_derecha, borderwidth=0, highlightthickness=0, cursor="hand2")
54 5 cristobal hernandez
        self.canvas.create_window(195, 300, window=self.boton_derecha)
55 5 cristobal hernandez
56 5 cristobal hernandez
        self.boton_apagado = tk.Button(self.root, image=self.imagen_apagado, borderwidth=0, highlightthickness=0, cursor="hand2")
57 5 cristobal hernandez
        self.canvas.create_window(755, 400, window=self.boton_apagado)
58 5 cristobal hernandez
59 5 cristobal hernandez
if __name__ == "__main__":
60 5 cristobal hernandez
    root = tk.Tk()
61 5 cristobal hernandez
    app = Aplicacion(root)
62 5 cristobal hernandez
    root.mainloop()
63 4 cristobal hernandez
</code></pre>
64 6 cristobal hernandez
65 6 cristobal hernandez
<pre><code class="python">
66 6 cristobal hernandez
67 6 cristobal hernandez
</code></pre>
68 7 cristobal hernandez
69 7 cristobal hernandez
# Joystick
70 7 cristobal hernandez
    def inicializar_pygame(self):
71 7 cristobal hernandez
        pygame.init()
72 7 cristobal hernandez
        pygame.joystick.init()
73 7 cristobal hernandez
74 7 cristobal hernandez
        if pygame.joystick.get_count() > 0:
75 7 cristobal hernandez
            self.joystick = pygame.joystick.Joystick(0)
76 7 cristobal hernandez
            self.joystick.init()
77 7 cristobal hernandez
            print(f"Joystick detectado: {self.joystick.get_name()}")
78 7 cristobal hernandez
            self.running = True
79 7 cristobal hernandez
            self.verificar_eventos_pygame()      
80 7 cristobal hernandez
        else:
81 7 cristobal hernandez
            print("No se detectó ningún joystick.")
82 7 cristobal hernandez
            self.joystick = None
83 7 cristobal hernandez
            pygame.quit()
84 7 cristobal hernandez
    
85 7 cristobal hernandez
    def verificar_eventos_pygame(self):
86 7 cristobal hernandez
        if self.combobox_controles.get() != "Joystick":
87 7 cristobal hernandez
            self.running = False
88 7 cristobal hernandez
            pygame.quit()
89 7 cristobal hernandez
            return
90 7 cristobal hernandez
91 7 cristobal hernandez
        for event in pygame.event.get():
92 7 cristobal hernandez
            if pygame.joystick.get_count() == 0:
93 7 cristobal hernandez
                self.running = False
94 7 cristobal hernandez
                messagebox.showinfo("Información", "Se ha perdido la conexión con el mando.")
95 7 cristobal hernandez
                return
96 7 cristobal hernandez
97 7 cristobal hernandez
            if event.type == pygame.JOYHATMOTION:
98 7 cristobal hernandez
                self.manejar_hat(event)
99 7 cristobal hernandez
100 7 cristobal hernandez
            elif event.type in [pygame.JOYBUTTONDOWN, pygame.JOYBUTTONUP]:
101 7 cristobal hernandez
                self.manejar_botones(event)
102 7 cristobal hernandez
103 7 cristobal hernandez
            elif self.joystick:
104 7 cristobal hernandez
                self.manejar_joystick()
105 7 cristobal hernandez
106 7 cristobal hernandez
        self.root.after(60, self.verificar_eventos_pygame)
107 7 cristobal hernandez
108 7 cristobal hernandez
    def manejar_hat(self, event):
109 7 cristobal hernandez
        hat_value = self.joystick.get_hat(0)
110 7 cristobal hernandez
        direcciones = {(0, 1): self.moveUp, (0, -1): self.moveDown, (-1, 0): self.moveLeft, (1, 0): self.moveRight}
111 7 cristobal hernandez
112 7 cristobal hernandez
        if hat_value != (0, 0) and not self.key_pressed and hat_value in direcciones:
113 7 cristobal hernandez
            direcciones[hat_value]()
114 7 cristobal hernandez
            self.canvas.itemconfig(self.contenedor[hat_value], fill="#08FBF9")
115 7 cristobal hernandez
            self.tecla_pressed = hat_value
116 7 cristobal hernandez
            self.key_pressed = True
117 7 cristobal hernandez
        elif hat_value == (0, 0) and self.tecla_pressed in direcciones:
118 7 cristobal hernandez
            self.stop()
119 7 cristobal hernandez
            self.canvas.itemconfig(self.contenedor[self.tecla_pressed], fill="#0F2B6A")
120 7 cristobal hernandez
            self.key_pressed = False
121 7 cristobal hernandez
            self.tecla_pressed = None
122 7 cristobal hernandez
123 7 cristobal hernandez
    def manejar_botones(self, event):
124 7 cristobal hernandez
        button_mapping = {
125 7 cristobal hernandez
            0: self.grab,    # button_x
126 7 cristobal hernandez
            1: self.drop,    # button_circle
127 7 cristobal hernandez
            8: self.upCraw,  # button_l2
128 7 cristobal hernandez
            9: self.downCraw # button_r2
129 7 cristobal hernandez
        }
130 7 cristobal hernandez
131 7 cristobal hernandez
        if event.type == pygame.JOYBUTTONDOWN and event.button in button_mapping:
132 7 cristobal hernandez
            self.botones_presionados.add(event.button)
133 7 cristobal hernandez
            if not self.key_pressed:
134 7 cristobal hernandez
                button_mapping[event.button]()
135 7 cristobal hernandez
                self.tecla_pressed = event.button
136 7 cristobal hernandez
                self.key_pressed = True
137 7 cristobal hernandez
            self.canvas.itemconfig(self.contenedor[self.tecla_pressed], fill="#08FBF9")
138 7 cristobal hernandez
139 7 cristobal hernandez
        elif event.type == pygame.JOYBUTTONUP and event.button in self.botones_presionados:
140 7 cristobal hernandez
            self.botones_presionados.discard(event.button)
141 7 cristobal hernandez
            if event.button == self.tecla_pressed:
142 7 cristobal hernandez
                self.canvas.itemconfig(self.contenedor[self.tecla_pressed], fill="#0F2B6A")
143 7 cristobal hernandez
                self.key_pressed = False
144 7 cristobal hernandez
                self.tecla_pressed = None
145 7 cristobal hernandez
146 7 cristobal hernandez
    def manejar_joystick(self):
147 7 cristobal hernandez
        eje_x = self.joystick.get_axis(0)
148 7 cristobal hernandez
        eje_y = self.joystick.get_axis(1)
149 7 cristobal hernandez
150 7 cristobal hernandez
        eje_x_redondeado = round(eje_x, 1)
151 7 cristobal hernandez
        eje_y_redondeado = round(eje_y, 1)
152 7 cristobal hernandez
153 7 cristobal hernandez
        if abs(eje_x_redondeado) < 0.9 and abs(eje_y_redondeado) < 0.9:
154 7 cristobal hernandez
            if self.tecla_pressed == "c":
155 7 cristobal hernandez
                self.stop()
156 7 cristobal hernandez
                self.canvas.itemconfig(self.contenedor["c"], fill="white")
157 7 cristobal hernandez
                self.key_pressed = False
158 7 cristobal hernandez
                self.tecla_pressed = None
159 7 cristobal hernandez
160 7 cristobal hernandez
        else:
161 7 cristobal hernandez
            direcciones_x = {1: self.moveRight, -1: self.moveLeft}
162 7 cristobal hernandez
            direcciones_y = {1: self.moveDown, -1: self.moveUp}
163 7 cristobal hernandez
164 7 cristobal hernandez
            if eje_x_redondeado > 0.89 and not self.key_pressed:  # Derecha
165 7 cristobal hernandez
                direcciones_x[1]()
166 7 cristobal hernandez
                self.canvas.itemconfig(self.contenedor["c"], fill="#08FBF9")
167 7 cristobal hernandez
                self.tecla_pressed = "c"
168 7 cristobal hernandez
                self.key_pressed = True
169 7 cristobal hernandez
170 7 cristobal hernandez
            elif eje_x_redondeado < -0.89 and not self.key_pressed:  # Izquierda
171 7 cristobal hernandez
                direcciones_x[-1]()
172 7 cristobal hernandez
                self.canvas.itemconfig(self.contenedor["c"], fill="#08FBF9")
173 7 cristobal hernandez
                self.tecla_pressed = "c"
174 7 cristobal hernandez
                self.key_pressed = True
175 7 cristobal hernandez
176 7 cristobal hernandez
            elif eje_y_redondeado > 0.89 and not self.key_pressed:  # Abajo
177 7 cristobal hernandez
                direcciones_y[1]()
178 7 cristobal hernandez
                self.canvas.itemconfig(self.contenedor["c"], fill="#08FBF9")
179 7 cristobal hernandez
                self.tecla_pressed = "c"
180 7 cristobal hernandez
                self.key_pressed = True
181 7 cristobal hernandez
182 7 cristobal hernandez
            elif eje_y_redondeado < -0.89 and not self.key_pressed:  # Arriba
183 7 cristobal hernandez
                direcciones_y[-1]()
184 7 cristobal hernandez
                self.canvas.itemconfig(self.contenedor["c"], fill="#08FBF9")
185 7 cristobal hernandez
                self.tecla_pressed = "c"
186 7 cristobal hernandez
                self.key_pressed = True
187 7 cristobal hernandez
188 7 cristobal hernandez
189 7 cristobal hernandez
        nuevo_x = 350 + eje_x * 50
190 7 cristobal hernandez
        nuevo_y = 275 + eje_y * 50
191 7 cristobal hernandez
        self.pos_x = nuevo_x
192 7 cristobal hernandez
        self.pos_y = nuevo_y
193 7 cristobal hernandez
        self.canvas.coords(self.contenedor["c"], self.pos_x - 10, self.pos_y - 10, self.pos_x + 10, self.pos_y + 10)