diff --git a/app/__pycache__/game.cpython-313.pyc b/app/__pycache__/game.cpython-313.pyc index f832a28..6f64a2f 100644 Binary files a/app/__pycache__/game.cpython-313.pyc and b/app/__pycache__/game.cpython-313.pyc differ diff --git a/app/game.py b/app/game.py index 09750e0..0ab6962 100644 --- a/app/game.py +++ b/app/game.py @@ -1,52 +1,112 @@ +import pygame import threading -import tkinter as tk import random import time +import tkinter as tk +from tkinter import Frame, Canvas, messagebox, Button class HiloJuego: - def __init__(self, frame): - self.frame = frame - - self.score = 0 + def __init__(self, parent): + self.parent = parent self.running = False - - self.label_score = tk.Label(frame, text=f"Puntaje: {self.score}", font=("Arial", 16), bg="lightgray") - self.label_score.pack(pady=10) - - self.start_button = tk.Button(frame, text="Iniciar Juego", command=self.iniciar_juego) - self.start_button.pack(pady=5) - - self.target_button = tk.Button(frame, text="¡Click me!", state="disabled", command=self.incrementar_puntaje) - self.target_button.pack(pady=20) - - def iniciar_juego(self): - self.running = True self.score = 0 - self.label_score.config(text=f"Puntaje: {self.score}") - self.start_button.config(state="disabled") - self.target_button.config(state="normal") - - # Hilo para mover el botón - self.hilo_movimiento = threading.Thread(target=self.mover_boton) - self.hilo_movimiento.daemon = True - self.hilo_movimiento.start() + self.misses = 0 # Contador de fallos + self.max_misses = 3 # Número máximo de fallos permitidos + self.circles = [] - def mover_boton(self): + # Crear el canvas dentro del frame proporcionado + self.canvas = Canvas(parent, width=200, height=100, bg="black") + self.canvas.pack(fill="both", expand=True) + + # Crear botón para iniciar el juego + self.start_button = Button(parent, text="Iniciar Juego", command=self.start_game) + self.start_button.pack(pady=10) + + class Circle: + 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 is_expired(self): + return time.time() - self.appeared_at > self.duration + + def generate_circles(self): while self.running: - x = random.randint(50, self.frame.winfo_width() - 100) - y = random.randint(50, self.frame.winfo_height() - 100) + 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)) - self.target_button.place(x=x, y=y) - time.sleep(1) + def main_loop(self): + self.canvas.bind("", self.on_click) - 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.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}") + # 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.score = 0 + self.misses = 0 + self.circles = [] + 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() + + def end_game(self): + self.running = False + messagebox.showinfo("Game Over", f"Has perdido. Puntaje final: {self.score}") + self.start_button.pack(pady=10) # Mostrar el botón nuevamente al finalizar el juego + + def stop(self): + self.running = False diff --git a/main.py b/main.py index b5b8a03..f767625 100644 --- a/main.py +++ b/main.py @@ -126,8 +126,10 @@ for i in range(1, 6): notebook.add(tab, text="Gestor de tareas", padding=4) gestor_tareas.GestorTareas(tab) elif i == 4: - notebook.add(tab,text='Juego', padding=4) - hilo_juego = game.HiloJuego(tab) + notebook.add(tab, text='Juego', padding=4) + game_frame = tk.Frame(tab) + game_frame.pack(fill="both", expand=True) + hilo_juego = game.HiloJuego(game_frame) else: notebook.add(tab, text=f"Solapa {i}", padding=4) # Añadir un Label en cada solapa para diferenciarla @@ -197,4 +199,4 @@ panel_i = panel_izquierdo.PanelIzquierdo(frame_izquierdo, text_scrapping) root.protocol("WM_DELETE_WINDOW", detener_aplicacion) # Ejecución de la aplicación -root.mainloop() \ No newline at end of file +root.mainloop()