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