Nuevo juego
This commit is contained in:
parent
c480f9a926
commit
9abdca650c
Binary file not shown.
142
app/game.py
142
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.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.score = 0
|
||||||
self.label_score.config(text=f"Puntaje: {self.score}")
|
self.misses = 0 # Contador de fallos
|
||||||
self.start_button.config(state="disabled")
|
self.max_misses = 3 # Número máximo de fallos permitidos
|
||||||
self.target_button.config(state="normal")
|
self.circles = []
|
||||||
|
|
||||||
# Hilo para mover el botón
|
|
||||||
self.hilo_movimiento = threading.Thread(target=self.mover_boton)
|
|
||||||
self.hilo_movimiento.daemon = True
|
|
||||||
self.hilo_movimiento.start()
|
|
||||||
|
|
||||||
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:
|
while self.running:
|
||||||
x = random.randint(50, self.frame.winfo_width() - 100)
|
x = random.randint(50, 750)
|
||||||
y = random.randint(50, self.frame.winfo_height() - 100)
|
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)
|
def main_loop(self):
|
||||||
time.sleep(1)
|
self.canvas.bind("<Button-1>", self.on_click)
|
||||||
|
|
||||||
def incrementar_puntaje(self):
|
# Inicia el hilo para generar círculos
|
||||||
self.score += 1
|
circle_thread = threading.Thread(target=self.generate_circles)
|
||||||
self.label_score.config(text=f"Puntaje: {self.score}")
|
circle_thread.daemon = True
|
||||||
|
circle_thread.start()
|
||||||
#Fin del juego
|
|
||||||
if self.score >= 10:
|
while self.running:
|
||||||
self.running = False
|
self.canvas.delete("all")
|
||||||
self.start_button.config(state="normal")
|
|
||||||
self.target_button.config(state="disabled")
|
# Dibujar círculos
|
||||||
self.target_button.place_forget()
|
for circle in self.circles[:]:
|
||||||
self.label_score.config(text=f"¡Juego Terminado! Puntaje final: {self.score}")
|
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
|
||||||
|
|
8
main.py
8
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
|
||||||
|
@ -197,4 +199,4 @@ panel_i = panel_izquierdo.PanelIzquierdo(frame_izquierdo, text_scrapping)
|
||||||
root.protocol("WM_DELETE_WINDOW", detener_aplicacion)
|
root.protocol("WM_DELETE_WINDOW", detener_aplicacion)
|
||||||
|
|
||||||
# Ejecución de la aplicación
|
# Ejecución de la aplicación
|
||||||
root.mainloop()
|
root.mainloop()
|
||||||
|
|
Loading…
Reference in New Issue