Nuevo juego
This commit is contained in:
		
							parent
							
								
									c480f9a926
								
							
						
					
					
						commit
						9abdca650c
					
				
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										136
									
								
								app/game.py
								
								
								
								
							
							
						
						
									
										136
									
								
								app/game.py
								
								
								
								
							|  | @ -1,52 +1,112 @@ | ||||||
|  | import pygame | ||||||
| import threading | import threading | ||||||
| import tkinter as tk |  | ||||||
| import random | import random | ||||||
| import time | import time | ||||||
|  | import tkinter as tk | ||||||
|  | from tkinter import Frame, Canvas, messagebox, Button | ||||||
| 
 | 
 | ||||||
| class HiloJuego: | class HiloJuego: | ||||||
|     def __init__(self, frame): |     def __init__(self, parent): | ||||||
|         self.frame = frame |         self.parent = parent | ||||||
| 
 |  | ||||||
|         self.score = 0 |  | ||||||
|         self.running = False |         self.running = False | ||||||
|  |         self.score = 0 | ||||||
|  |         self.misses = 0  # Contador de fallos | ||||||
|  |         self.max_misses = 3  # Número máximo de fallos permitidos | ||||||
|  |         self.circles = [] | ||||||
| 
 | 
 | ||||||
|         self.label_score = tk.Label(frame, text=f"Puntaje: {self.score}", font=("Arial", 16), bg="lightgray") |         # Crear el canvas dentro del frame proporcionado | ||||||
|         self.label_score.pack(pady=10) |         self.canvas = Canvas(parent, width=200, height=100, bg="black") | ||||||
|  |         self.canvas.pack(fill="both", expand=True) | ||||||
| 
 | 
 | ||||||
|         self.start_button = tk.Button(frame, text="Iniciar Juego", command=self.iniciar_juego) |         # Crear botón para iniciar el juego | ||||||
|         self.start_button.pack(pady=5) |         self.start_button = Button(parent, text="Iniciar Juego", command=self.start_game) | ||||||
|  |         self.start_button.pack(pady=10) | ||||||
| 
 | 
 | ||||||
|         self.target_button = tk.Button(frame, text="¡Click me!", state="disabled", command=self.incrementar_puntaje) |     class Circle: | ||||||
|         self.target_button.pack(pady=20) |         def __init__(self, x, y, radius, duration): | ||||||
|  |             self.x = x | ||||||
|  |             self.y = y | ||||||
|  |             self.radius = radius | ||||||
|  |             self.duration = duration | ||||||
|  |             self.appeared_at = time.time() | ||||||
| 
 | 
 | ||||||
|     def iniciar_juego(self): |         def is_expired(self): | ||||||
|  |             return time.time() - self.appeared_at > self.duration | ||||||
|  | 
 | ||||||
|  |     def generate_circles(self): | ||||||
|  |         while self.running: | ||||||
|  |             x = random.randint(50, 750) | ||||||
|  |             y = random.randint(50, 550) | ||||||
|  |             radius = 40 | ||||||
|  |             duration = random.uniform(1.5, 3.0) | ||||||
|  |             circle = self.Circle(x, y, radius, duration) | ||||||
|  |             self.circles.append(circle) | ||||||
|  |             time.sleep(random.uniform(0.5, 1.5)) | ||||||
|  | 
 | ||||||
|  |     def main_loop(self): | ||||||
|  |         self.canvas.bind("<Button-1>", self.on_click) | ||||||
|  | 
 | ||||||
|  |         # Inicia el hilo para generar círculos | ||||||
|  |         circle_thread = threading.Thread(target=self.generate_circles) | ||||||
|  |         circle_thread.daemon = True | ||||||
|  |         circle_thread.start() | ||||||
|  | 
 | ||||||
|  |         while self.running: | ||||||
|  |             self.canvas.delete("all") | ||||||
|  | 
 | ||||||
|  |             # Dibujar círculos | ||||||
|  |             for circle in self.circles[:]: | ||||||
|  |                 if circle.is_expired(): | ||||||
|  |                     self.circles.remove(circle) | ||||||
|  |                     self.misses += 1 | ||||||
|  |                     if self.misses >= self.max_misses: | ||||||
|  |                         self.end_game() | ||||||
|  |                         return | ||||||
|  |                 else: | ||||||
|  |                     self.canvas.create_oval( | ||||||
|  |                         circle.x - circle.radius, circle.y - circle.radius, | ||||||
|  |                         circle.x + circle.radius, circle.y + circle.radius, | ||||||
|  |                         outline="red", width=3 | ||||||
|  |                     ) | ||||||
|  | 
 | ||||||
|  |             # Mostrar puntaje y fallos | ||||||
|  |             self.canvas.create_text(10, 10, anchor="nw", text=f"Score: {self.score}", fill="white", font=("Arial", 16)) | ||||||
|  |             self.canvas.create_text(10, 40, anchor="nw", text=f"Misses: {self.misses}/{self.max_misses}", fill="white", font=("Arial", 16)) | ||||||
|  | 
 | ||||||
|  |             self.canvas.update() | ||||||
|  |             time.sleep(0.016) | ||||||
|  | 
 | ||||||
|  |     def on_click(self, event): | ||||||
|  |         if not self.running: | ||||||
|  |             return | ||||||
|  |         x, y = event.x, event.y | ||||||
|  |         hit = False | ||||||
|  |         for circle in self.circles[:]: | ||||||
|  |             if (x - circle.x) ** 2 + (y - circle.y) ** 2 <= circle.radius ** 2: | ||||||
|  |                 self.score += 1 | ||||||
|  |                 self.circles.remove(circle) | ||||||
|  |                 hit = True | ||||||
|  |                 break | ||||||
|  |         if not hit: | ||||||
|  |             self.misses += 1 | ||||||
|  |             if self.misses >= self.max_misses: | ||||||
|  |                 self.end_game() | ||||||
|  | 
 | ||||||
|  |     def start_game(self): | ||||||
|  |         if not self.running: | ||||||
|             self.running = True |             self.running = True | ||||||
|             self.score = 0 |             self.score = 0 | ||||||
|         self.label_score.config(text=f"Puntaje: {self.score}") |             self.misses = 0 | ||||||
|         self.start_button.config(state="disabled") |             self.circles = [] | ||||||
|         self.target_button.config(state="normal") |             self.start_button.pack_forget()  # Ocultar el botón al iniciar el juego | ||||||
|  |             game_thread = threading.Thread(target=self.main_loop) | ||||||
|  |             game_thread.daemon = True | ||||||
|  |             game_thread.start() | ||||||
| 
 | 
 | ||||||
|         # Hilo para mover el botón |     def end_game(self): | ||||||
|         self.hilo_movimiento = threading.Thread(target=self.mover_boton) |         self.running = False | ||||||
|         self.hilo_movimiento.daemon = True |         messagebox.showinfo("Game Over", f"Has perdido. Puntaje final: {self.score}") | ||||||
|         self.hilo_movimiento.start() |         self.start_button.pack(pady=10)  # Mostrar el botón nuevamente al finalizar el juego | ||||||
| 
 | 
 | ||||||
|     def mover_boton(self): |     def stop(self): | ||||||
|         while self.running: |  | ||||||
|             x = random.randint(50, self.frame.winfo_width() - 100) |  | ||||||
|             y = random.randint(50, self.frame.winfo_height() - 100) |  | ||||||
| 
 |  | ||||||
|             self.target_button.place(x=x, y=y) |  | ||||||
|             time.sleep(1) |  | ||||||
| 
 |  | ||||||
|     def incrementar_puntaje(self): |  | ||||||
|         self.score += 1 |  | ||||||
|         self.label_score.config(text=f"Puntaje: {self.score}") |  | ||||||
|          |  | ||||||
|         #Fin del juego |  | ||||||
|         if self.score >= 10: |  | ||||||
|         self.running = False |         self.running = False | ||||||
|             self.start_button.config(state="normal") |  | ||||||
|             self.target_button.config(state="disabled") |  | ||||||
|             self.target_button.place_forget() |  | ||||||
|             self.label_score.config(text=f"¡Juego Terminado! Puntaje final: {self.score}") |  | ||||||
|  |  | ||||||
							
								
								
									
										6
									
								
								main.py
								
								
								
								
							
							
						
						
									
										6
									
								
								main.py
								
								
								
								
							|  | @ -126,8 +126,10 @@ for i in range(1, 6): | ||||||
|         notebook.add(tab, text="Gestor de tareas", padding=4) |         notebook.add(tab, text="Gestor de tareas", padding=4) | ||||||
|         gestor_tareas.GestorTareas(tab) |         gestor_tareas.GestorTareas(tab) | ||||||
|     elif i == 4: |     elif i == 4: | ||||||
|         notebook.add(tab,text='Juego', padding=4) |         notebook.add(tab, text='Juego', padding=4) | ||||||
|         hilo_juego = game.HiloJuego(tab) |         game_frame = tk.Frame(tab) | ||||||
|  |         game_frame.pack(fill="both", expand=True) | ||||||
|  |         hilo_juego = game.HiloJuego(game_frame) | ||||||
|     else: |     else: | ||||||
|         notebook.add(tab, text=f"Solapa {i}", padding=4) |         notebook.add(tab, text=f"Solapa {i}", padding=4) | ||||||
|         # Añadir un Label en cada solapa para diferenciarla |         # Añadir un Label en cada solapa para diferenciarla | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue