def __init__(self): super(GameScreen, self).__init__(0xBF907A, 0x806052, 0xFFC0A3, 0x403029) # Placeholders self.answer = "0" self.question = None # Initialize game self.game = Game( game_engine=GameEngine.BuildGameEngineFromStatesAndTransitions( GAME_STATES_FILENAME, GAME_TRANSITIONS_FILENAME), player_name=playerName, difficulty_level=difficultyLevel) # Create required labels. self.timer_label = Label("00", font_name="Times New Roman", font_size=32, anchor_x='center', anchor_y='center') self.question_label = Label("Question Text", font_name="Times New Roman", font_size=32, anchor_x='center', anchor_y='center') self.answer_label = Label("Answer Text", font_name="Times New Roman", font_size=32, anchor_x='center', anchor_y='center') self.score_board_label = Label("Score: ", font_name="Times New Roman", font_size=32, anchor_x='center', anchor_y='center') self.instruction_label = Label("Press Enter to submit answer!", font_name="Times New Roman", font_size=32, anchor_x='center', anchor_y='center') self.timer_label.position = ( director._window_virtual_width / 10) * 9, director._window_virtual_height / 10 self.question_label.position = director._window_virtual_width / 2, director._window_virtual_height / 2 self.answer_label.position = director._window_virtual_width / 2, director._window_virtual_height / 2 - 50 self.score_board_label.position = director._window_virtual_width / 10, director._window_virtual_height / 10 self.instruction_label.position = director._window_virtual_width / 2, ( director._window_virtual_height / 10) * 9 self.add(self.instruction_label) self.add(self.timer_label) self.add(self.question_label) self.add(self.score_board_label) self.add(self.answer_label) self.display_question()
def main(): # Take User's name input until valid. while True: user_name = input("Name: ").strip() if user_name.isalpha(): print() break print("Invalid username. Name can only consist of alphabets.") while True: while True: try: difficulty = int( input( "Choose difficulty:\n\t1. Easy\n\t2. Medium\n\t3. Hard\n" )) except ValueError: print("Input can only be integer. Please try again!") continue if 0 < difficulty < 4: break print("Invalid choice. Please choose a number from 1 to 3.") # Game object that contains all the information about a game session. game = Game( game_engine=GameEngine.BuildGameEngineFromStatesAndTransitions( GAME_STATES_FILENAME, GAME_TRANSITIONS_FILENAME), player_name=user_name, difficulty_level=difficulty) print("-" * 20 + "Starting the Game" + "-" * 20) while not game.is_game_over(): curr_question = game.get_question() while True: try: answer = int(input(curr_question)) except ValueError: print("Invalid Input. Answers can only be integers") continue if game.submit_answer(answer): # Correct. print("Good one!") else: print("You missed it! :(") print("Score: {}".format(game.get_current_state())) break print() # Game is over. if game.get_current_state() == 'win': print("Congratulations!! You won.") else: print("Better luck next time :(") while True: play_again = input("Play again(y/n)?") if play_again == 'y' or play_again == 'n': break print("Invalid Choice. Press 'y' for Yes and 'n' for No") if play_again == 'n': print("-" * 20 + "Ending the Game" + "-" * 20) break print() print("Thank you for playing :)")