def json_processing(): """ Json file processing function Calls a function that determines if a json file is already created. If no - new json file is created and results are saved. If json file is already created - file is loaded and max score is checked """ if is_json_here("data_in_json.txt"): with open("data_in_json.txt", encoding="utf-8") as fileToRead: json_data = json.load(fileToRead) check_max_score(json_data) with open("data_in_json.txt", "w", encoding="utf-8") as file_name: json.dump(json_data, file_name, ensure_ascii=False, indent=2) else: data = {"".join(name): str(score[0])} with open("data_in_json.txt", "w", encoding="utf-8") as file: json.dump(data, file, ensure_ascii=False, indent=2) if score[0] > 500: evaluation_good = Text_to_print( font_example, [width / 2, height / 2 - 2 * font_example], 0, "Good job", colors[0]) end_texts.append(evaluation_good) else: evaluation_bad = Text_to_print( font_example, [width / 2, height / 2 - 2 * font_example], 0, "I think you can do better", colors[0]) end_texts.append(evaluation_bad)
def main_screen(): """ Generates texts for the main screen and adds them to the list """ headline = Text_to_print(2 * font_example, [width / 2, headline_y - 3 * font_example], 0, "M A T H G A M E", colors[3]) instruction = Text_to_print(font_example, [width / 2, 2 * font_example], 0, "Press SPACE BAR to Play", colors[0]) main_texts.extend([headline, instruction]) generate_questions() generate_possibilities()
def update_answer(): """Functions for drawing the right answer""" x = (buckets[3].coordx[0] - buckets[1].coordx[0]) / 2 + buckets[1].coordx[0] y = headline_y - 3 * font_example if right_examples: color = right_examples[-1].color else: color = colors[0] answer_text = Text_to_print(font_example, [x, y], 0, str(results[-1]), color) answer_text.draw_text("center")
def update_score(): """Function for drawing the current score state""" x = (buckets[3].coordx[0] - buckets[1].coordx[0]) / 2 + buckets[1].coordx[0] y = headline_y - 3 * font_example if right_examples: color = right_examples[-1].color else: color = colors[0] points = Text_to_print(font_example, [width - x, y], 0, str(score[0]), color) points.draw_text("center")
def on_draw(): """ Window push handler Function for drawing all objects. They are divided according to in which part of the game they should be drawn """ if main: window.clear() for text in main_texts: text.draw_text("center") for question in main_questions: question.draw_text("right") for possibility in possibilities: possibility.draw_text("left") if name: user_name() name_user[-1].draw_text("left") if difficulty: chosen = Text_to_print(font_example, [350, 2 * height / 3 - 8 * font_example], 0, str(difficulty[-1]), colors[0]) chosen.draw_text("left") if not main: window.clear() elif game_over: window.clear() for text in end_texts: text.draw_text("center") else: gl.glClear(gl.GL_COLOR_BUFFER_BIT) for i in range(len(objects)): objects[i].draw_text("left") del buckets[12:16] update_starter() for j in range(len(headlines)): headlines[j].draw_text("center") for k in range(len(buckets)): buckets[k].draw_bucket() for l in range(len(rectangles)): rectangles[l].draw_rectangle() update_score() if results: update_answer()
def generate_questions(): """ Generates questions for the main screen and adds them to the list """ instruction1 = Text_to_print(font_example, [300, 2 * height / 3], 0, "Your name:", colors[0]) instruction2 = Text_to_print(font_example, [300, 2 * height / 3 - 2 * font_example], 0, "Choose difficulty:", colors[0]) choice = Text_to_print(font_example, [300, 2 * height / 3 - 8 * font_example], 0, "Your choice:", colors[0]) main_questions.extend([instruction1, instruction2, choice])
def generate_headlines(): """ Generates headlines for game and adds them to the list """ x = (buckets[3].coordx[0] - buckets[1].coordx[0]) / 2 + buckets[1].coordx[0] headline1 = Text_to_print(font_example, [x, headline_y], 0, "ANSWER", colors[0]) headline2 = Text_to_print(font_example, [width - x, headline_y], 0, "SCORE", colors[0]) headline3 = Text_to_print(font_example, [width / 2, headline_y - 3 * font_example], 0, "M A T H G A M E", colors[3]) headlines.extend([headline1, headline2, headline3])
def generate_possibilities(): """ Generates possibilities for main screen and adds them to the list """ instruction3 = Text_to_print(font_example, [350, 2 * height / 3 - 2 * font_example], 0, "addition, substraction 1", colors[0]) instruction4 = Text_to_print(font_example, [350, 2 * height / 3 - 4 * font_example], 0, "multiply, division 2", colors[0]) instruction5 = Text_to_print(font_example, [350, 2 * height / 3 - 6 * font_example], 0, "mix 3", colors[0]) possibilities.extend([instruction3, instruction4, instruction5])
def screen_game_over(): """ Generates text objects for game over screen, adds them to the list and calls the json file processing function """ math_game = "M A T H G A M E" headline = Text_to_print(2 * font_example, [width / 2, 2 * height / 3], 0, math_game, colors[3]) text = "YOUR SCORE WAS " + str(score[0]) text_end = Text_to_print(font_example, [width / 2, height / 2], 0, text, colors[0]) text1 = "Do you want to play again (Y/N)?" decision = Text_to_print(font_example, [width / 2, 50], 0, text1, colors[0]) end_texts.extend([text_end, decision, headline]) json_processing()
def user_name(): """ Creates an object from the name entered by the user and adds it to the list """ name_input = Text_to_print(font_example, [350, 2 * height / 3], 0, "".join(name), colors[0]) name_user.append(name_input)
def better_than_before(data): """ Evaluates whether the player has a better result than before. If user is better than before new result will be saved. Creates object with result and adds it to the list """ for keys in data: data[keys] = int(data[keys]) if data["".join(name)] < score[0]: congrats = Text_to_print(font_example, [width / 2, height / 2 - 2 * font_example], 0, "Super! You did better than before!", colors[0]) end_texts.append(congrats) data["".join(name)] = str(score[0]) return True else: loser = Text_to_print(font_example, [width / 2, height / 2 - 2 * font_example], 0, "Oh no! You were worse than before!", colors[0]) end_texts.append(loser) return False
def check_max_score(data): """ Checks and evaluates score. Creates objects with results and adds them to the list """ if is_same_key(data): if better_than_before(data): for keys in data: data[keys] = int(data[keys]) max_key = max(data, key=data.get) if "".join(name) == max_key: congrats1 = Text_to_print( font_example, [width / 2, height / 2 - 4 * font_example], 0, "Wow! Now you have the best score of all!", colors[4]) end_texts.append(congrats1) else: almost = Text_to_print( font_example, [width / 2, height / 2 - 4 * font_example], 0, "Great job! But... " + max_key + "is still better with score " + str(data[max_key]), colors[0]) end_texts.append(almost) else: data["".join(name)] = str(score[0]) for keys in data: data[keys] = int(data[keys]) max_key = max(data, key=data.get) if "".join(name) == max_key: congrats1 = Text_to_print( font_example, [width / 2, height / 2 - 4 * font_example], 0, "Wow! Now you have the best score of all!", colors[4]) end_texts.append(congrats1) else: not_great = Text_to_print( font_example, [width / 2, height / 2 - 4 * font_example], 0, max_key + "is still better with score " + str(data[max_key]), colors[0]) end_texts.append(not_great)
def generate_object(): """Generates new object with example and adds it to the list""" x = take_closest(starter_position[0], possible_x_objects) y = buckets[-1].coordy[3] - font_example number_generator() choose_operation(difficulty[-1]) # dodat difficulty[-1] color = choice(colors[1:]) if numbers[0] == 0 and operation[-1] == " : ": # aby nedošlo k dělení nulou if difficulty[-1] == 2: operation.append(operations[2]) else: choose_operation(1) if operation[-1] == " : ": result = numbers[0] * numbers[1] example = str(result) + operation[-1] + str(numbers[0]) + " = " else: example = str(numbers[0]) + operation[-1] + str(numbers[1]) + " = " text = Text_to_print(font_example, [x, y], speed, example, color) objects.append(text)