Interfaz » History » Version 10
cristobal hernandez, 12/12/2024 10:06 PM
1 | 1 | ana gutierrez | h1. Interfaz |
---|---|---|---|
2 | 4 | cristobal hernandez | |
3 | 5 | cristobal hernandez | <pre><code class="python"> |
4 | 10 | cristobal hernandez | import pygame |
5 | 10 | cristobal hernandez | import tkinter as tk |
6 | 10 | cristobal hernandez | from tkinter import ttk |
7 | 10 | cristobal hernandez | from tkinter import messagebox |
8 | 10 | cristobal hernandez | import socket |
9 | 10 | cristobal hernandez | |
10 | 10 | cristobal hernandez | class Aplicacion: |
11 | 10 | cristobal hernandez | def __init__(self, root): |
12 | 10 | cristobal hernandez | self.key_pressed = False |
13 | 10 | cristobal hernandez | self.tecla_pressed = None |
14 | 10 | cristobal hernandez | self.botones_presionados = set() |
15 | 10 | cristobal hernandez | |
16 | 10 | cristobal hernandez | self.lista_botones = [] |
17 | 10 | cristobal hernandez | self.contenedor = {} |
18 | 10 | cristobal hernandez | self.labels = {} |
19 | 10 | cristobal hernandez | |
20 | 10 | cristobal hernandez | self.imagenes_botones = { |
21 | 10 | cristobal hernandez | "up": tk.PhotoImage(file="resources2/up.png"), |
22 | 10 | cristobal hernandez | "left": tk.PhotoImage(file="resources2/left.png"), |
23 | 10 | cristobal hernandez | "down": tk.PhotoImage(file="resources2/down.png"), |
24 | 10 | cristobal hernandez | "right": tk.PhotoImage(file="resources2/right.png"), |
25 | 10 | cristobal hernandez | "up_claw": tk.PhotoImage(file="resources2/up_claw.png"), |
26 | 10 | cristobal hernandez | "down_claw": tk.PhotoImage(file="resources2/down_claw.png"), |
27 | 10 | cristobal hernandez | "center1": tk.PhotoImage(file="resources2/center1.png"), |
28 | 10 | cristobal hernandez | "center2": tk.PhotoImage(file="resources2/center2.png"), |
29 | 10 | cristobal hernandez | "off": tk.PhotoImage(file="resources2/off1.png") |
30 | 10 | cristobal hernandez | } |
31 | 10 | cristobal hernandez | |
32 | 10 | cristobal hernandez | self.botones_config = [ |
33 | 10 | cristobal hernandez | (self.imagenes_botones["up"], 275, 250, self.moveUp), |
34 | 10 | cristobal hernandez | (self.imagenes_botones["left"], 225, 300, self.moveLeft), |
35 | 10 | cristobal hernandez | (self.imagenes_botones["down"], 275, 300, self.moveDown), |
36 | 10 | cristobal hernandez | (self.imagenes_botones["right"], 325, 300, self.moveRight), |
37 | 10 | cristobal hernandez | (self.imagenes_botones["up_claw"], 475, 250, self.upCraw), |
38 | 10 | cristobal hernandez | (self.imagenes_botones["down_claw"], 475, 300, self.downCraw), |
39 | 10 | cristobal hernandez | (self.imagenes_botones["center1"], 555, 275, self.grab), |
40 | 10 | cristobal hernandez | (self.imagenes_botones["center2"], 625, 275, self.drop) |
41 | 10 | cristobal hernandez | ] |
42 | 10 | cristobal hernandez | |
43 | 10 | cristobal hernandez | self.teclas_teclado = { |
44 | 10 | cristobal hernandez | "w": {"coords": (250, 225, 300, 275), "label_pos": (275, 250), "etiqueta": "W", "funcion": self.moveUp}, # Arriba |
45 | 10 | cristobal hernandez | "a": {"coords": (200, 275, 250, 325), "label_pos": (225, 300), "etiqueta": "A", "funcion": self.moveLeft}, # Izquierda |
46 | 10 | cristobal hernandez | "s": {"coords": (250, 275, 300, 325), "label_pos": (275, 300), "etiqueta": "S", "funcion": self.moveDown}, # Abajo |
47 | 10 | cristobal hernandez | "d": {"coords": (300, 275, 350, 325), "label_pos": (325, 300), "etiqueta": "D", "funcion": self.moveRight}, # Derecha |
48 | 10 | cristobal hernandez | "l": {"coords": (450, 225, 500, 275), "label_pos": (475, 250), "etiqueta": "L", "funcion": self.upCraw}, # Arriba Garra |
49 | 10 | cristobal hernandez | "k": {"coords": (450, 275, 500, 325), "label_pos": (475, 300), "etiqueta": "K", "funcion": self.downCraw}, # Abajo Garra |
50 | 10 | cristobal hernandez | "x": {"coords": (530, 250, 580, 300), "label_pos": (555, 275), "etiqueta": "X", "funcion": self.grab}, # Agarrar |
51 | 10 | cristobal hernandez | "c": {"coords": (600, 250, 650, 300), "label_pos": (625, 275), "etiqueta": "C", "funcion": self.drop} # Soltar |
52 | 10 | cristobal hernandez | } |
53 | 10 | cristobal hernandez | |
54 | 10 | cristobal hernandez | self.teclas_joystick = { |
55 | 10 | cristobal hernandez | (0, 1): {"coords": (175, 200, 225, 250), "label_pos": (200, 225), "etiqueta": "w"}, # Arriba |
56 | 10 | cristobal hernandez | (0, -1): {"coords": (175, 300, 225, 350), "label_pos": (200, 325), "etiqueta": "s"}, # Abajo |
57 | 10 | cristobal hernandez | (-1, 0): {"coords": (125, 250, 175, 300), "label_pos": (150, 275), "etiqueta": "a"}, # Izquierda |
58 | 10 | cristobal hernandez | (1, 0): {"coords": (225, 250, 275, 300), "label_pos": (250, 275), "etiqueta": "d"}, # Derecha |
59 | 10 | cristobal hernandez | 8: {"coords": (450, 200, 500, 250), "label_pos": (475, 225), "etiqueta": "L2"}, # Arriba Garra |
60 | 10 | cristobal hernandez | 9: {"coords": (450, 250, 500, 300), "label_pos": (475, 275), "etiqueta": "R2"}, # Abajo Garra |
61 | 10 | cristobal hernandez | 0: {"coords": (530, 225, 580, 275), "label_pos": (555, 250), "etiqueta": "A"}, # Agarrar |
62 | 10 | cristobal hernandez | 1: {"coords": (600, 225, 650, 275), "label_pos": (625, 250), "etiqueta": "B"}, # Soltar |
63 | 10 | cristobal hernandez | "C": {"coords": (300, 225, 400, 325), "label_pos": (625, 250), "etiqueta": ""}, # Círculo Grande |
64 | 10 | cristobal hernandez | "c": {"coords": (340, 265, 360, 285), "label_pos": (625, 250), "etiqueta": ""}, # Círculo Chico |
65 | 10 | cristobal hernandez | } |
66 | 10 | cristobal hernandez | |
67 | 10 | cristobal hernandez | self.root = root |
68 | 10 | cristobal hernandez | self.root.title("Blitz") |
69 | 10 | cristobal hernandez | self.root.geometry("800x450") |
70 | 10 | cristobal hernandez | self.root.resizable(0, 0) |
71 | 10 | cristobal hernandez | self.root.iconbitmap("resources2/logoBlitz.png") |
72 | 10 | cristobal hernandez | |
73 | 10 | cristobal hernandez | self._configurar_estilos() |
74 | 10 | cristobal hernandez | |
75 | 10 | cristobal hernandez | self.imagen_fondo = tk.PhotoImage(file="resources2/fondoBlitz.png") |
76 | 10 | cristobal hernandez | self.canvas = tk.Canvas(self.root, width=800, height=400) |
77 | 10 | cristobal hernandez | self.canvas.pack(fill="both", expand=True) |
78 | 10 | cristobal hernandez | self.canvas.create_image(0, 0, image=self.imagen_fondo, anchor="nw") |
79 | 10 | cristobal hernandez | |
80 | 10 | cristobal hernandez | self.crear_labels() |
81 | 10 | cristobal hernandez | self.combobox_controles = ttk.Combobox(self.root, values=["Botones", "Teclado", "Joystick"], |
82 | 10 | cristobal hernandez | state="disabled", font=("Comic Sans MS", 10), width=7, |
83 | 10 | cristobal hernandez | style="TCombobox") |
84 | 10 | cristobal hernandez | self.combobox_controles.bind("<<ComboboxSelected>>", self.on_select) |
85 | 10 | cristobal hernandez | self.canvas.create_window(180, 135, window=self.combobox_controles) |
86 | 10 | cristobal hernandez | self.combobox_controles.current(0) |
87 | 10 | cristobal hernandez | |
88 | 10 | cristobal hernandez | # Crear botón "off" |
89 | 10 | cristobal hernandez | self.x0, self.y0, self.x1, self.y1 = 375, 327, 425, 377 |
90 | 10 | cristobal hernandez | self.circle = self.canvas.create_oval(self.x0, self.y0, self.x1, self.y1, fill="blue", outline="black") |
91 | 10 | cristobal hernandez | |
92 | 10 | cristobal hernandez | self.imagen_conexion = tk.PhotoImage(file="resources2/off1.png") |
93 | 10 | cristobal hernandez | self.imagen_desconexion = tk.PhotoImage(file="resources2/off2.png") |
94 | 10 | cristobal hernandez | self.current_image = self.imagen_desconexion |
95 | 10 | cristobal hernandez | |
96 | 10 | cristobal hernandez | self.canvas_image = self.canvas.create_image((self.x0 + self.x1) / 2, (self.y0 + self.y1) / 2, image=self.current_image) |
97 | 10 | cristobal hernandez | self.canvas.image = self.current_image |
98 | 10 | cristobal hernandez | |
99 | 10 | cristobal hernandez | self.current_funcion = self.ventana_conexion_servidor |
100 | 10 | cristobal hernandez | |
101 | 10 | cristobal hernandez | self.canvas.bind("<Button-1>", self.check_click) |
102 | 10 | cristobal hernandez | |
103 | 10 | cristobal hernandez | |
104 | 10 | cristobal hernandez | def check_click(self, event): |
105 | 10 | cristobal hernandez | x, y = event.x, event.y |
106 | 10 | cristobal hernandez | if self.x0 <= x <= self.x1 and self.y0 <= y <= self.y1: |
107 | 10 | cristobal hernandez | self.current_funcion() |
108 | 10 | cristobal hernandez | |
109 | 10 | cristobal hernandez | def crear_labels(self): |
110 | 10 | cristobal hernandez | self.label1 = tk.Label(self.root, text="Robot", font=("Comic Sans MS", 16, "bold")) |
111 | 10 | cristobal hernandez | self.canvas.create_window(275, 170, window=self.label1) |
112 | 10 | cristobal hernandez | self.label2 = tk.Label(self.root, text="Garra", font=("Comic Sans MS", 16, "bold")) |
113 | 10 | cristobal hernandez | self.canvas.create_window(555, 170, window=self.label2) |
114 | 10 | cristobal hernandez | self.label_agarrar = tk.Label(self.root, text="Agarrar", font=("Comic Sans MS", 9, "bold")) |
115 | 10 | cristobal hernandez | self.canvas.create_window(555, 325, window=self.label_agarrar) |
116 | 10 | cristobal hernandez | self.label_soltar = tk.Label(self.root, text="Soltar", font=("Comic Sans MS", 9, "bold")) |
117 | 10 | cristobal hernandez | self.canvas.create_window(625, 325, window=self.label_soltar) |
118 | 10 | cristobal hernandez | |
119 | 10 | cristobal hernandez | def borrar_labels(self): |
120 | 10 | cristobal hernandez | self.label1.destroy() |
121 | 10 | cristobal hernandez | self.label2.destroy() |
122 | 10 | cristobal hernandez | self.label_agarrar.destroy() |
123 | 10 | cristobal hernandez | self.label_soltar.destroy() |
124 | 10 | cristobal hernandez | |
125 | 10 | cristobal hernandez | def _configurar_estilos(self): |
126 | 10 | cristobal hernandez | estilos = { |
127 | 10 | cristobal hernandez | '*TCombobox*Listbox.font': ("Comic Sans MS", 10), |
128 | 10 | cristobal hernandez | '*TCombobox*Listbox.background': "#0F2B6A", |
129 | 10 | cristobal hernandez | '*TCombobox*Listbox.foreground': "#ffffff", |
130 | 10 | cristobal hernandez | '*TCombobox*Listbox.selectBackground': '#08FBF9' |
131 | 10 | cristobal hernandez | } |
132 | 10 | cristobal hernandez | for k, v in estilos.items(): |
133 | 10 | cristobal hernandez | self.root.option_add(k, v) |
134 | 10 | cristobal hernandez | |
135 | 10 | cristobal hernandez | # Botones |
136 | 10 | cristobal hernandez | def crear_botones(self): |
137 | 10 | cristobal hernandez | for imagen, x, y, funcion in self.botones_config[:4]: |
138 | 10 | cristobal hernandez | boton = tk.Button(self.root, image=imagen, borderwidth=0, highlightthickness=0, cursor="hand2", bg="#0F2B6A") |
139 | 10 | cristobal hernandez | boton.bind("<ButtonPress>", lambda event, f=funcion: f()) |
140 | 10 | cristobal hernandez | boton.bind("<ButtonRelease>", lambda event: self.stop()) |
141 | 10 | cristobal hernandez | self.canvas.create_window(x, y, window=boton) |
142 | 10 | cristobal hernandez | self.lista_botones.append(boton) |
143 | 10 | cristobal hernandez | for imagen, x, y, funcion in self.botones_config[4:]: |
144 | 10 | cristobal hernandez | boton = tk.Button(self.root, image=imagen, borderwidth=0, highlightthickness=0, cursor="hand2", bg="#0F2B6A") |
145 | 10 | cristobal hernandez | boton.bind("<ButtonPress>", lambda event, f=funcion: f()) |
146 | 10 | cristobal hernandez | self.canvas.create_window(x, y, window=boton) |
147 | 10 | cristobal hernandez | self.lista_botones.append(boton) |
148 | 10 | cristobal hernandez | |
149 | 10 | cristobal hernandez | def eliminar_botones(self): |
150 | 10 | cristobal hernandez | for boton in self.lista_botones: |
151 | 10 | cristobal hernandez | boton.destroy() |
152 | 10 | cristobal hernandez | self.lista_botones.clear() |
153 | 10 | cristobal hernandez | |
154 | 10 | cristobal hernandez | # Crear y eliminar Teclas |
155 | 10 | cristobal hernandez | def crear_teclas_teclado(self): |
156 | 10 | cristobal hernandez | for tecla, datos in self.teclas_teclado.items(): |
157 | 10 | cristobal hernandez | coords, label_pos, etiqueta = datos["coords"], datos["label_pos"], datos["etiqueta"] |
158 | 10 | cristobal hernandez | |
159 | 10 | cristobal hernandez | self.contenedor[tecla] = self.canvas.create_rectangle(*coords, fill="#0F2B6A", outline="black") |
160 | 10 | cristobal hernandez | label = self.canvas.create_text(*label_pos, text=etiqueta, font=("Comic Sans MS", 20), fill="white") |
161 | 10 | cristobal hernandez | self.labels[tecla] = label |
162 | 10 | cristobal hernandez | |
163 | 10 | cristobal hernandez | def crear_teclas_joystick(self): |
164 | 10 | cristobal hernandez | for tecla, datos in self.teclas_joystick.items(): |
165 | 10 | cristobal hernandez | coords, label_pos, etiqueta = datos["coords"], datos["label_pos"], datos["etiqueta"] |
166 | 10 | cristobal hernandez | |
167 | 10 | cristobal hernandez | self.contenedor[tecla] = self.canvas.create_oval(*coords, fill="#0F2B6A", outline="black") |
168 | 10 | cristobal hernandez | label = self.canvas.create_text(*label_pos, text=etiqueta, font=("Comic Sans MS", 20), fill="white") |
169 | 10 | cristobal hernandez | self.labels[tecla] = label |
170 | 10 | cristobal hernandez | |
171 | 10 | cristobal hernandez | self.canvas.itemconfig(self.contenedor["C"], fill="") |
172 | 10 | cristobal hernandez | self.canvas.itemconfig(self.contenedor["c"], fill="white") |
173 | 10 | cristobal hernandez | |
174 | 10 | cristobal hernandez | |
175 | 10 | cristobal hernandez | def eliminar_teclas_y_botones(self): |
176 | 10 | cristobal hernandez | if self.contenedor: |
177 | 10 | cristobal hernandez | for tecla in self.contenedor: |
178 | 10 | cristobal hernandez | self.canvas.delete(self.contenedor[tecla]) |
179 | 10 | cristobal hernandez | self.contenedor.clear() |
180 | 10 | cristobal hernandez | for tecla in self.labels: |
181 | 10 | cristobal hernandez | self.canvas.delete(self.labels[tecla]) |
182 | 10 | cristobal hernandez | for boton in self.lista_botones: |
183 | 10 | cristobal hernandez | boton.destroy() |
184 | 10 | cristobal hernandez | self.labels.clear() |
185 | 10 | cristobal hernandez | self.lista_botones.clear() |
186 | 10 | cristobal hernandez | |
187 | 10 | cristobal hernandez | #Teclado |
188 | 10 | cristobal hernandez | def activar_teclas_teclado(self): |
189 | 10 | cristobal hernandez | for tecla in self.teclas_teclado: |
190 | 10 | cristobal hernandez | self.root.bind(f"<KeyPress-{tecla}>", self.crear_callback(tecla, self.pressed)) |
191 | 10 | cristobal hernandez | self.root.bind(f"<KeyRelease-{tecla}>", self.crear_callback(tecla, self.released)) |
192 | 10 | cristobal hernandez | self.root.bind(f"<KeyPress-{tecla.upper()}>", self.crear_callback(tecla, self.pressed)) |
193 | 10 | cristobal hernandez | self.root.bind(f"<KeyRelease-{tecla.upper()}>", self.crear_callback(tecla, self.released)) |
194 | 10 | cristobal hernandez | |
195 | 10 | cristobal hernandez | def desactivar_teclas_teclado(self): |
196 | 10 | cristobal hernandez | for tecla in self.teclas_teclado: |
197 | 10 | cristobal hernandez | self.root.unbind(f"<KeyPress-{tecla}>") |
198 | 10 | cristobal hernandez | self.root.unbind(f"<KeyRelease-{tecla}>") |
199 | 10 | cristobal hernandez | self.root.unbind(f"<KeyPress-{tecla.upper()}>") |
200 | 10 | cristobal hernandez | self.root.unbind(f"<KeyRelease-{tecla.upper()}>") |
201 | 10 | cristobal hernandez | |
202 | 10 | cristobal hernandez | def crear_callback(self, tecla, funcion): |
203 | 10 | cristobal hernandez | return lambda event: funcion(tecla) |
204 | 10 | cristobal hernandez | |
205 | 10 | cristobal hernandez | def pressed(self, tecla): |
206 | 10 | cristobal hernandez | if not self.key_pressed and self.tecla_pressed is None: |
207 | 10 | cristobal hernandez | self.canvas.itemconfig(self.contenedor[tecla], fill="#08FBF9") |
208 | 10 | cristobal hernandez | self.tecla_pressed = tecla |
209 | 10 | cristobal hernandez | self.key_pressed = True |
210 | 10 | cristobal hernandez | self.teclas_teclado[tecla]["funcion"]() |
211 | 10 | cristobal hernandez | |
212 | 10 | cristobal hernandez | |
213 | 10 | cristobal hernandez | def released(self, tecla): |
214 | 10 | cristobal hernandez | teclas_sin_stop = ["L", "K", "X", "C"] |
215 | 10 | cristobal hernandez | |
216 | 10 | cristobal hernandez | if tecla.upper() not in teclas_sin_stop: |
217 | 10 | cristobal hernandez | self.stop() |
218 | 10 | cristobal hernandez | |
219 | 10 | cristobal hernandez | if tecla == self.tecla_pressed: |
220 | 10 | cristobal hernandez | self.canvas.itemconfig(self.contenedor[tecla], fill="#0F2B6A") |
221 | 10 | cristobal hernandez | self.tecla_pressed = None |
222 | 10 | cristobal hernandez | self.key_pressed = False |
223 | 10 | cristobal hernandez | |
224 | 8 | cristobal hernandez | </code></pre> |