Interfaz » History » Version 11
cristobal hernandez, 12/12/2024 10:07 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 | 11 | cristobal hernandez | def on_select(self, event): |
| 225 | 11 | cristobal hernandez | seleccion = self.combobox_controles.get() |
| 226 | 11 | cristobal hernandez | self.eliminar_teclas_y_botones() |
| 227 | 11 | cristobal hernandez | if seleccion == "Botones": |
| 228 | 11 | cristobal hernandez | self.crear_botones() |
| 229 | 11 | cristobal hernandez | elif seleccion == "Teclado": |
| 230 | 11 | cristobal hernandez | self.crear_teclas_teclado() |
| 231 | 11 | cristobal hernandez | self.activar_teclas_teclado() |
| 232 | 11 | cristobal hernandez | else: |
| 233 | 11 | cristobal hernandez | self.crear_teclas_joystick() |
| 234 | 11 | cristobal hernandez | self.inicializar_pygame() |
| 235 | 11 | cristobal hernandez | self.root.focus() |
| 236 | 11 | cristobal hernandez | self.tecla_pressed = None |
| 237 | 11 | cristobal hernandez | self.key_pressed = False |
| 238 | 11 | cristobal hernandez | |
| 239 | 11 | cristobal hernandez | # Servidor |
| 240 | 11 | cristobal hernandez | def validar_ip(self, ip): |
| 241 | 11 | cristobal hernandez | try: |
| 242 | 11 | cristobal hernandez | socket.inet_aton(ip) |
| 243 | 11 | cristobal hernandez | return True |
| 244 | 11 | cristobal hernandez | except socket.error: |
| 245 | 11 | cristobal hernandez | return False |
| 246 | 11 | cristobal hernandez | |
| 247 | 11 | cristobal hernandez | def validar_puerto(self, port): |
| 248 | 11 | cristobal hernandez | try: |
| 249 | 11 | cristobal hernandez | port = int(port) |
| 250 | 11 | cristobal hernandez | return 1 <= port <= 65535 |
| 251 | 11 | cristobal hernandez | except ValueError: |
| 252 | 11 | cristobal hernandez | return False |
| 253 | 11 | cristobal hernandez | |
| 254 | 11 | cristobal hernandez | def conectar_servidor(self, ip_entry, port_entry, scene): |
| 255 | 11 | cristobal hernandez | ip = ip_entry.get() |
| 256 | 11 | cristobal hernandez | port = port_entry.get() |
| 257 | 11 | cristobal hernandez | |
| 258 | 11 | cristobal hernandez | if not self.validar_ip(ip): |
| 259 | 11 | cristobal hernandez | messagebox.showerror("Error", "Dirección IP no válida.") |
| 260 | 11 | cristobal hernandez | return |
| 261 | 11 | cristobal hernandez | if not self.validar_puerto(port): |
| 262 | 11 | cristobal hernandez | messagebox.showerror("Error", "Puerto no válido. Debe estar entre 1 y 65535.") |
| 263 | 11 | cristobal hernandez | return |
| 264 | 11 | cristobal hernandez | |
| 265 | 11 | cristobal hernandez | port = int(port) |
| 266 | 11 | cristobal hernandez | |
| 267 | 11 | cristobal hernandez | try: |
| 268 | 11 | cristobal hernandez | self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 269 | 11 | cristobal hernandez | self.client_socket.connect((ip, port)) |
| 270 | 11 | cristobal hernandez | |
| 271 | 11 | cristobal hernandez | self.combobox_controles.config(state="reondly") |
| 272 | 11 | cristobal hernandez | self.combobox_controles.set("Botones") |
| 273 | 11 | cristobal hernandez | self.crear_botones() |
| 274 | 11 | cristobal hernandez | |
| 275 | 11 | cristobal hernandez | scene.destroy() |
| 276 | 11 | cristobal hernandez | |
| 277 | 11 | cristobal hernandez | self.current_funcion = self.ventana_desconectar_servidor |
| 278 | 11 | cristobal hernandez | self.current_image = self.imagen_conexion |
| 279 | 11 | cristobal hernandez | self.canvas_image = self.canvas.create_image((self.x0 + self.x1) / 2, (self.y0 + self.y1) / 2, image=self.current_image) |
| 280 | 11 | cristobal hernandez | self.canvas.image = self.current_image |
| 281 | 11 | cristobal hernandez | |
| 282 | 11 | cristobal hernandez | messagebox.showinfo("Éxito", "Conexión exitosa al servidor.") |
| 283 | 11 | cristobal hernandez | except socket.error as e: |
| 284 | 11 | cristobal hernandez | messagebox.showerror("Error", f"Error al conectar al servidor") |
| 285 | 11 | cristobal hernandez | except Exception as e: |
| 286 | 11 | cristobal hernandez | messagebox.showerror("Error", f"Ha ocurrido un error desconocido") |
| 287 | 11 | cristobal hernandez | |
| 288 | 11 | cristobal hernandez | |
| 289 | 11 | cristobal hernandez | |
| 290 | 11 | cristobal hernandez | def ventana_conexion_servidor(self): |
| 291 | 11 | cristobal hernandez | self.secondary_window = tk.Toplevel(self.root) |
| 292 | 11 | cristobal hernandez | self.secondary_window.title("Ingresar IP y Puerto") |
| 293 | 11 | cristobal hernandez | self.secondary_window.geometry("400x225") |
| 294 | 11 | cristobal hernandez | self.secondary_window.config(bg="#0F2B6A") |
| 295 | 11 | cristobal hernandez | self.secondary_window.resizable(0, 0) |
| 296 | 11 | cristobal hernandez | self.secondary_window.iconbitmap("resources2/logoBlitz.png") |
| 297 | 11 | cristobal hernandez | |
| 298 | 11 | cristobal hernandez | tk.Label(self.secondary_window, text="IP del Servidor:", font=("Helvetica ", 9, "bold")).pack(pady=(20,5)) |
| 299 | 11 | cristobal hernandez | self.ip_entry = tk.Entry(self.secondary_window) |
| 300 | 11 | cristobal hernandez | self.ip_entry.pack(pady=5) |
| 301 | 11 | cristobal hernandez | |
| 302 | 11 | cristobal hernandez | tk.Label(self.secondary_window, text="Puerto del Servidor:", font=("Helvetica ", 9, "bold")).pack(pady=5) |
| 303 | 11 | cristobal hernandez | self.port_entry = tk.Entry(self.secondary_window) |
| 304 | 11 | cristobal hernandez | self.port_entry.pack(pady=5) |
| 305 | 11 | cristobal hernandez | |
| 306 | 11 | cristobal hernandez | confirm_button = tk.Button(self.secondary_window, text="Conectar", bg="#22c8c9", command=lambda: self.conectar_servidor(self.ip_entry, self.port_entry, self.secondary_window)) |
| 307 | 11 | cristobal hernandez | confirm_button.pack(pady=5) |
| 308 | 11 | cristobal hernandez | |
| 309 | 11 | cristobal hernandez | ip_save_button = tk.Button(self.secondary_window, text="Ingresar IP guardada", bg="#22c8c9", command=lambda: self.ingresar_ip_guardada(self.ip_entry, self.port_entry)) |
| 310 | 11 | cristobal hernandez | ip_save_button.pack() |
| 311 | 11 | cristobal hernandez | |
| 312 | 11 | cristobal hernandez | def ingresar_ip_guardada(self, ip_entry, port_entry): |
| 313 | 11 | cristobal hernandez | ip_entry.delete(0, tk.END) |
| 314 | 11 | cristobal hernandez | ip_entry.insert(0, "192.168.144.84") |
| 315 | 11 | cristobal hernandez | |
| 316 | 11 | cristobal hernandez | port_entry.delete(0, tk.END) |
| 317 | 11 | cristobal hernandez | port_entry.insert(0, "8080") |
| 318 | 11 | cristobal hernandez | |
| 319 | 11 | cristobal hernandez | def ventana_desconectar_servidor(self): |
| 320 | 11 | cristobal hernandez | if messagebox.askyesno("Confirmación", "¿Está seguro que quiere desconectar del servidor?"): |
| 321 | 11 | cristobal hernandez | self.client_socket.close() |
| 322 | 11 | cristobal hernandez | |
| 323 | 11 | cristobal hernandez | self.current_funcion = self.ventana_conexion_servidor |
| 324 | 11 | cristobal hernandez | self.current_image = self.imagen_desconexion |
| 325 | 11 | cristobal hernandez | self.canvas_image = self.canvas.create_image((self.x0 + self.x1) / 2, (self.y0 + self.y1) / 2, image=self.current_image) |
| 326 | 11 | cristobal hernandez | self.canvas.image = self.current_image |
| 327 | 11 | cristobal hernandez | self.eliminar_teclas_y_botones() |
| 328 | 11 | cristobal hernandez | |
| 329 | 11 | cristobal hernandez | |
| 330 | 11 | cristobal hernandez | def moveUp(self): |
| 331 | 11 | cristobal hernandez | self.client_socket.send(bytes([ord('w')])) |
| 332 | 11 | cristobal hernandez | |
| 333 | 11 | cristobal hernandez | def moveDown(self): |
| 334 | 11 | cristobal hernandez | self.client_socket.send(bytes([ord('s')])) |
| 335 | 11 | cristobal hernandez | |
| 336 | 11 | cristobal hernandez | def moveRight(self): |
| 337 | 11 | cristobal hernandez | self.client_socket.send(bytes([ord('d')])) |
| 338 | 11 | cristobal hernandez | |
| 339 | 11 | cristobal hernandez | def moveLeft(self): |
| 340 | 11 | cristobal hernandez | self.client_socket.send(bytes([ord('a')])) |
| 341 | 11 | cristobal hernandez | |
| 342 | 11 | cristobal hernandez | def upCraw(self): |
| 343 | 11 | cristobal hernandez | self.client_socket.send(bytes([ord('l')])) |
| 344 | 11 | cristobal hernandez | |
| 345 | 11 | cristobal hernandez | def downCraw(self): |
| 346 | 11 | cristobal hernandez | self.client_socket.send(bytes([ord('k')])) |
| 347 | 11 | cristobal hernandez | |
| 348 | 11 | cristobal hernandez | def grab(self): |
| 349 | 11 | cristobal hernandez | self.client_socket.send(bytes([ord('x')])) |
| 350 | 11 | cristobal hernandez | |
| 351 | 11 | cristobal hernandez | def drop(self): |
| 352 | 11 | cristobal hernandez | self.client_socket.send(bytes([ord('c')])) |
| 353 | 11 | cristobal hernandez | |
| 354 | 11 | cristobal hernandez | def stop(self): |
| 355 | 11 | cristobal hernandez | self.client_socket.send(bytes([ord(' ')])) |
| 356 | 11 | cristobal hernandez | |
| 357 | 11 | cristobal hernandez | if __name__ == "__main__": |
| 358 | 11 | cristobal hernandez | root = tk.Tk() |
| 359 | 11 | cristobal hernandez | app = Aplicacion(root) |
| 360 | 11 | cristobal hernandez | root.mainloop() |
| 361 | 8 | cristobal hernandez | </code></pre> |