53 lines
1.8 KiB
Python
53 lines
1.8 KiB
Python
import threading
|
|
import tkinter as tk
|
|
import random
|
|
import time
|
|
|
|
class HiloJuego:
|
|
def __init__(self, frame):
|
|
self.frame = frame
|
|
|
|
self.score = 0
|
|
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()
|
|
|
|
def mover_boton(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.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}")
|