Project

General

Profile

Interfaz » History » Version 9

Version 8 (cristobal hernandez, 12/12/2024 08:47 PM) → Version 9/11 (cristobal hernandez, 12/12/2024 09:12 PM)

h1. Interfaz

<pre><code class="python">
import tkinter as tk

class Aplicacion:
def __init__(self, root):
self.root = root
self.root.title(&quot;Blitz&quot;)
self.root.geometry(&quot;800x450&quot;)
self.root.resizable(0, 0)

# Cargar el logo
self.root.iconbitmap(&quot;resources/logo.ico&quot;)

# Cargar la imagen de fondo
self.imagen_fondo = tk.PhotoImage(file=&quot;resources/fondo1.png&quot;)

# Crear un Canvas para colocar la imagen de fondo
self.canvas = tk.Canvas(self.root, width=800, height=400)
self.canvas.pack(fill=&quot;both&quot;, expand=True)

# Colocar la imagen en el Canvas
self.canvas.create_image(0, 0, image=self.imagen_fondo, anchor=&quot;nw&quot;)

# Imágenes para los botones
self.imagen_central = tk.PhotoImage(file=&quot;resources/center.png&quot;)
self.imagen_arriba = tk.PhotoImage(file=&quot;resources/up.png&quot;)
self.imagen_abajo = tk.PhotoImage(file=&quot;resources/down.png&quot;)
self.imagen_izquierda = tk.PhotoImage(file=&quot;resources/left.png&quot;)
self.imagen_derecha = tk.PhotoImage(file=&quot;resources/right.png&quot;)
self.imagen_apagado = tk.PhotoImage(file=&quot;resources/off.png&quot;)


# Cuadro &quot;transparente&quot;
self.canvas.create_rectangle(50, 210, 230, 390, fill=&quot;#052D45&quot;, stipple=&quot;gray50&quot;, outline=&quot;&quot;)

self.crear_botones()

def crear_botones(self):
self.boton_central = tk.Button(self.root, image=self.imagen_central, borderwidth=0, highlightthickness=0, cursor=&quot;hand2&quot;)
self.canvas.create_window(140, 300, window=self.boton_central)

self.boton_arriba = tk.Button(self.root, image=self.imagen_arriba, borderwidth=0, highlightthickness=0, cursor=&quot;hand2&quot;)
self.canvas.create_window(140, 245, window=self.boton_arriba)

self.boton_abajo = tk.Button(self.root, image=self.imagen_abajo, borderwidth=0, highlightthickness=0, cursor=&quot;hand2&quot;)
self.canvas.create_window(140, 355, window=self.boton_abajo)

self.boton_izquierda = tk.Button(self.root, image=self.imagen_izquierda, borderwidth=0, highlightthickness=0, cursor=&quot;hand2&quot;)
self.canvas.create_window(85, 300, window=self.boton_izquierda)

self.boton_derecha = tk.Button(self.root, image=self.imagen_derecha, borderwidth=0, highlightthickness=0, cursor=&quot;hand2&quot;)
self.canvas.create_window(195, 300, window=self.boton_derecha)

self.boton_apagado = tk.Button(self.root, image=self.imagen_apagado, borderwidth=0, highlightthickness=0, cursor=&quot;hand2&quot;)
self.canvas.create_window(755, 400, window=self.boton_apagado)

if __name__ == &quot;__main__&quot;:
root = tk.Tk()
app = Aplicacion(root)
root.mainloop()
&lt;/code&gt;&lt;/pre&gt;

&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;

&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;
# Joystick
def inicializar_pygame(self):
pygame.init()
pygame.joystick.init()

if pygame.joystick.get_count() &gt; 0:
self.joystick = pygame.joystick.Joystick(0)
self.joystick.init()
print(f&quot;Joystick detectado: {self.joystick.get_name()}&quot;)
self.running = True
self.verificar_eventos_pygame()
else:
print(&quot;No se detectó ningún joystick.&quot;)
self.joystick = None
pygame.quit()

def verificar_eventos_pygame(self):
if self.combobox_controles.get() != &quot;Joystick&quot;:
self.running = False
pygame.quit()
return

for event in pygame.event.get():
if pygame.joystick.get_count() == 0:
self.running = False
messagebox.showinfo(&quot;Información&quot;, &quot;Se ha perdido la conexión con el mando.&quot;)
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=&quot;#08FBF9&quot;)
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=&quot;#0F2B6A&quot;)
self.key_pressed = False
self.tecla_pressed = None

def manejar_botones(self, event):
button_mapping = {
0: self.grab, # button_x
1: self.drop, # button_circle
8: self.upCraw, # button_l2
9: self.downCraw # button_r2
}

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=&quot;#08FBF9&quot;)

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=&quot;#0F2B6A&quot;)
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) &lt; 0.9 and abs(eje_y_redondeado) &lt; 0.9:
if self.tecla_pressed == &quot;c&quot;:
self.stop()
self.canvas.itemconfig(self.contenedor[&quot;c&quot;], fill=&quot;white&quot;)
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 &gt; 0.89 and not self.key_pressed: # Derecha
direcciones_x[1]()
self.canvas.itemconfig(self.contenedor[&quot;c&quot;], fill=&quot;#08FBF9&quot;)
self.tecla_pressed = &quot;c&quot;
self.key_pressed = True

elif eje_x_redondeado &lt; -0.89 and not self.key_pressed: # Izquierda
direcciones_x[-1]()
self.canvas.itemconfig(self.contenedor[&quot;c&quot;], fill=&quot;#08FBF9&quot;)
self.tecla_pressed = &quot;c&quot;
self.key_pressed = True

elif eje_y_redondeado &gt; 0.89 and not self.key_pressed: # Abajo
direcciones_y[1]()
self.canvas.itemconfig(self.contenedor[&quot;c&quot;], fill=&quot;#08FBF9&quot;)
self.tecla_pressed = &quot;c&quot;
self.key_pressed = True

elif eje_y_redondeado &lt; -0.89 and not self.key_pressed: # Arriba
direcciones_y[-1]()
self.canvas.itemconfig(self.contenedor[&quot;c&quot;], fill=&quot;#08FBF9&quot;)
self.tecla_pressed = &quot;c&quot;
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[&quot;c&quot;], self.pos_x - 10, self.pos_y - 10, self.pos_x + 10, self.pos_y + 10)
</code></pre>