Me gustaría que en este código, aparezca el temporizador en pantalla al mismo tiempo que…
- User
Me gustaría que en este código, aparezca el temporizador en pantalla al mismo tiempo que las preguntas, y que cuando se termine el tiempo, el espacio donde está el texto de la pregunta aparezca ahora un texto que diga: Tiempo terminado, tu puntaje es: import json import random import os import googletrans from googletrans import Translator from kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.uix.label import Label from kivy.uix.button import Button from kivy.uix.textinput import TextInput from kivy.uix.screenmanager import ScreenManager, Screen from kivy.clock import Clock from kivy.metrics import dp from kivy.properties import NumericProperty, StringProperty, ObjectProperty
... (mantén las funciones cargar_preguntas, mostrar_menu, mostrar_mensaje_porcentaje, jugar, jugar_de_nuevo, seleccionar_idioma, guardar_puntuacion y ver_mejores_puntuaciones) ...
def cargar_preguntas(): with open("preguntas_es.json", "r", encoding="utf-8") as f: preguntas = json.load(f) return preguntas
def guardar_puntuacion(puntuacion): archivo_puntuaciones = "mejores_puntuaciones.txt"
codeif not os.path.exists(archivo_puntuaciones): with open(archivo_puntuaciones, "w") as f: f.write(f"{puntuacion}\n") else: with open(archivo_puntuaciones, "r") as f: puntuaciones = [int(linea.strip()) for linea in f.readlines()] puntuaciones.append(puntuacion) puntuaciones.sort(reverse=True) puntajes = puntuaciones[:5] with open(archivo_puntuaciones, "w") as f: for puntuacion in puntajes: f.write(f"{puntuacion}\n")def cargar_puntuaciones(): archivo_puntuaciones = "mejores_puntuaciones.txt"
codeif os.path.exists(archivo_puntuaciones): with open(archivo_puntuaciones, "r") as f: puntuaciones = [int(linea.strip()) for linea in f.readlines()] else: puntuaciones = [] return puntuacionesdef traducir(texto, idioma): translator = Translator() traduccion = translator.translate(texto, dest=idioma) return traduccion.text
class LanguageScreen(Screen): def seleccionar_idioma(self, idioma): self.manager.idioma = idioma self.manager.current = "menu"
class MainMenu(Screen): def init(self, **kwargs): super(MainMenu, self).(**kwargs) self.layout = BoxLayout(orientation='vertical') self.add_widget(Label(text='Menú principal')) self.play_button = Button(text='Jugar') self.play_button.bind(on_press=self.start_game) self.layout.add_widget(self.play_button) self.high_scores_button = Button(text='Ver mejores puntuaciones') self.high_scores_button.bind(on_press=self.show_high_scores) self.layout.add_widget(self.high_scores_button) self.exit_button = Button(text='Salir') self.exit_button.bind(on_press=self.exit_app) self.layout.add_widget(self.exit_button) self.add_widget(self.layout)
- Assistant
Puedes agregar un temporizador en la pantalla de la siguiente manera:
- Inicializa el Label del temporizador en el método
__init__deGameScreen:
pythonself.timer_label = Label(text='') self.layout.add_widget(self.timer_label)- Actualiza el temporizador con el método
update_timery haz que se ejecute cada segundo usandoClock.schedule_interval:
python - Inicializa el Label del temporizador en el método