Por favor, optimiza el codigo respetando sus funcionalidades y agrame que cuando pulse…
- User
Por favor, optimiza el codigo respetando sus funcionalidades y agrame que cuando pulse algun boton con la respuesta equivocada, me aparezca en pantalla que me equivoque, cual es la respuesta correcta y el puntaje: import kivy import json from kivy.app import App from kivy.uix.screenmanager import ScreenManager, Screen from kivy.uix.boxlayout import BoxLayout from kivy.uix.label import Label from kivy.uix.button import Button import random import os
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" if os.path.exists(archivo_puntuaciones): with open(archivo_puntuaciones, "a") as f: f.write(str(puntuacion) + "\n") else: with open(archivo_puntuaciones, "w") as f: f.write(str(puntuacion) + "\n")
class MainMenu(Screen): def init(self, **kwargs): super(MainMenu, self).init(**kwargs) self.layout = BoxLayout(orientation='vertical') self.title_label = Label(text='¡Bienvenido a la trivia!') self.layout.add_widget(self.title_label) self.play_button = Button(text='Jugar') self.play_button.bind(on_press=self.play_game) self.layout.add_widget(self.play_button) self.scores_button = Button(text='Mejores Puntuaciones') self.scores_button.bind(on_press=self.show_scores) self.layout.add_widget(self.scores_button) self.add_widget(self.layout)
codedef play_game(self, instance): self.manager.current = 'game' def show_scores(self, instance): self.manager.current = 'high_scores'class GameScreen(Screen): def init(self, **kwargs): super(GameScreen, self).init(**kwargs) self.layout = BoxLayout(orientation='vertical') self.question_label = Label(text='') self.layout.add_widget(self.question_label) self.options_layout = BoxLayout(orientation='vertical') self.layout.add_widget(self.options_layout) self.score_label = Label(text="Puntuación: 0") self.layout.add_widget(self.score_label) self.back_button = Button(text='Volver al menú principal') self.back_button.bind(on_press=self.go_back) self.layout.add_widget(self.back_button) self.add_widget(self.layout)
code - Assistant