Skip to content
Snippets Groups Projects
Select Git revision
  • main default protected
1 result

main.py

Blame
  • Forked from SOFT Core / SOFT 161 and 162 / Quizzer
    8 commits ahead of the upstream repository.
    main.py 5.68 KiB
    from random import shuffle
    
    from kivy.app import App
    from kivy.clock import Clock
    from kivy.properties import BooleanProperty, ListProperty, NumericProperty
    from kivy.modules import inspector
    from kivy.core.window import Window
    
    
    GUI_QUESTIONS = [
        ('What are the fundamental building blocks of a GUI?',
         ('Buttons', 'Containers', 'Layout Managers', 'Widgets'), 'Widgets'),
        ('What kind of widget is used to organize other widgets?',
         ('Buttons', 'Containers', 'Layout Managers', 'Organizers'), 'Containers'),
        ('What math term describes a widget hierarchy?',
         ('Rooted Tree', 'Separation of Concerns', 'Shrubbery', 'Vertex'), 'Rooted Tree'),
        ('Which of the following is not a true widget property?',
         ('Color', 'Enabledness', 'ID', 'Visibility'), 'ID'),
        ('What process connects widgets to event handlers?',
         ('Event Handling', 'Invocation', 'Registration', 'Selection'), 'Registration'),
        ('Which of the following is mostly useless unless in a group of two or more?',
         ('Buttons', 'Checkboxes', 'Radio Buttons', 'Textboxes'), 'Radio Buttons'),
        ('Which of the following GUI frameworks uses an unflipped y axis?',
         ('HTML+CSS', 'JavaFX', 'Kivy', 'WPF'), 'Kivy'),
    ]
    
    COLOR_QUESTIONS = [
        ('What of the following RGBA quadruples represents red?',
         ('(1, 0, 0, 1)', '(0, 1, 0, 1)', '(0, 1, 1, 0)', '(0, 0, 1, 1)'), '(1, 0, 0, 1)'),
        ('What of the following RGBA quadruples represents yellow?',
         ('(1, 0, 1, 1)', '(1, 1, 0, 1)', '(0, 1, 1, 1)', '(1, 1, 1, 1)'), '(1, 1, 0, 1)'),
        ('What of the following RGBA quadruples represents green?',
         ('(1, 0, 0, 1)', '(0, 1, 0, 1)', '(0, 1, 1, 0)', '(0, 0, 1, 1)'), '(0, 1, 0, 1)'),
        ('What of the following RGBA quadruples represents cyan?',
         ('(1, 0, 1, 1)', '(1, 1, 0, 1)', '(0, 1, 1, 1)', '(1, 1, 1, 1)'), '(0, 1, 1, 1)'),
        ('What of the following RGBA quadruples represents blue?',
         ('(1, 0, 0, 1)', '(0, 1, 0, 1)', '(0, 1, 1, 0)', '(0, 0, 1, 1)'), '(0, 0, 1, 1)'),
        ('What of the following RGBA quadruples represents magenta?',
         ('(1, 0, 1, 1)', '(1, 1, 0, 1)', '(0, 1, 1, 1)', '(1, 1, 1, 1)'), '(1, 0, 1, 1)'),
        ('What of the following RGBA quadruples represents white?',
         ('(1, 0, 1, 1)', '(1, 1, 0, 1)', '(0, 1, 1, 1)', '(1, 1, 1, 1)'), '(1, 1, 1, 1)'),
        ('What of the following RGBA quadruples represents black?',
         ('(0, 0, 0, 0)', '(0, 0, 0, 1)', '(0, 0, 1, 0)', '(0, 0, 1, 1)'), '(0, 0, 0, 1)'),
    ]
    
    TREE_QUESTIONS = [
        ('If a widget X is directly inside a container Y, then Y is X\'s what?',
         ('Child', 'Descendant', 'Parent', 'Root'), 'Parent'),
        ('If a widget X is directly inside a container Y, then X is Y\'s what?',
         ('Ancestor', 'Child', 'Parent', 'Leaf'), 'Child'),
        ('If a container Y is not inside anything else, then Y is called what?',
         ('Child', 'Descendant', 'Parent', 'Root'), 'Root'),
        ('If a widget X is inside Y inside a container Z, then Z is X\'s what?',
         ('Ancestor', 'Child', 'Descendant', 'Parent'), 'Ancestor'),
        ('If a widget X is inside Y inside a container Z, then X is Z\'s what?',
         ('Ancestor', 'Child', 'Descendant', 'Parent'), 'Descendant'),
        ('If a widget X contains no other widgets, then X is called what?',
         ('Ancestor', 'Leaf', 'Parent', 'Root'), 'Leaf'),
        ('If the widgets W and X are directly inside a container Y, then W is X\'s what?',
         ('Child', 'Parent', 'Sibling', 'Tree'), 'Sibling'),
    ]
    
    
    PAUSE_TIME = 0.75  # seconds
    
    
    class QuizzerApp(App):
        DEFAULT_QUESTION = ('What is your favorite color?', ('Blue', 'Blue', 'Blue', 'Blue'), 'Blue')
        NUMBER_OF_QUESTION = 5
    
        include_gui_topic = BooleanProperty(True)
        include_color_topic = BooleanProperty(False)
        include_tree_topic = BooleanProperty(False)
    
        questions = ListProperty([])
    
        paused = BooleanProperty(False)
        correct = BooleanProperty(False)
        score = NumericProperty(0)
        progress = NumericProperty(0)
    
        def build(self):
            inspector.create_inspector(Window, self)
    
        def reshuffle(self):
            questions = []  # Use a plain list, not a list property, for the computations.
            if self.include_gui_topic:
                questions += GUI_QUESTIONS
            elif self.include_color_topic:
                questions += COLOR_QUESTIONS
            elif self.include_tree_topic:
                questions += TREE_QUESTIONS
            shuffle(questions)
            self.questions = questions
    
        def on_start(self):
            self.reshuffle()
    
        def toggle_gui_topic(self, include_gui_topic):
            self.include_gui_topic = include_gui_topic
            self.reshuffle()
    
        def toggle_color_topic(self, include_color_topic):
            self.include_color_topic = include_color_topic
            self.reshuffle()
    
        def toggle_tree_topic(self, include_tree_topic):
            self.include_tree_topic = include_tree_topic
            self.reshuffle()
    
        def answer(self, answer):
    
            question = self.questions[-1] if len(self.questions) > 0 else QuizzerApp.DEFAULT_QUESTION
            self.paused = True
            self.correct = answer == question[2]
            self.progress += 1
            if self.correct:
                self.score += 1
                
                print(f'You got it correct, number of right are: {self.score} out of {QuizzerApp.NUMBER_OF_QUESTION}')
            else:
                print(f'total number of questions: {self.score} out of {QuizzerApp.NUMBER_OF_QUESTION}')
            Clock.schedule_once(lambda delta: self.unpause(), PAUSE_TIME)
    
        def unpause(self):
            self.paused = False
            if len(self.questions) > 0:
                self.questions.pop()
            if len(self.questions) == 0:
                self.reshuffle()
            if self.progress == QuizzerApp.NUMBER_OF_QUESTION:
                self.progress = 0
                self.score = 0
    
    
    if __name__ == '__main__':
        app = QuizzerApp()
        app.run()