Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- import sys
- import os
- base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
- pygame.init()
- size = (700, 700)
- black = (0, 0, 0)
- white = (255, 255, 255)
- pink = (255, 73, 243)
- blue = (75, 193, 255)
- green = (0, 255, 0)
- window = pygame.display.set_mode(size)
- luny_chibi=pygame.image.load(os.path.join(base_path, "luny chibi.png"))
- luny_kluk=pygame.image.load(os.path.join(base_path, "luny chibi kluk.png"))
- luny_chibi=pygame.transform.scale(luny_chibi,(200,200))
- luny_kluk=pygame.transform.scale(luny_kluk,(200,200))
- luny_cry=pygame.image.load(os.path.join(base_path, "luny chibi cry.png"))
- luny_cry=pygame.transform.scale(luny_cry,(200,200))
- title1_color = (0, 0, 0)
- title1_rect = pygame.Rect(300, 100, 50, 50)
- title1_font = pygame.font.SysFont(None, 46)
- title1_text = title1_font.render("Luny-", True, pink)
- title1_text_rect = title1_text.get_rect(center=title1_rect.center)
- title2_color = (0, 0, 0)
- title2_rect = pygame.Rect(300, 150, 50, 50)
- title2_font = pygame.font.SysFont(None, 46)
- title2_text = title2_font.render("Shimas", True, blue)
- title2_text_rect = title2_text.get_rect(center=title2_rect.center)
- title3_color = (0, 0, 0)
- title3_rect = pygame.Rect(300, 200, 50, 50)
- title3_font = pygame.font.SysFont(None, 46)
- title3_text = title3_font.render("Gehirnjogging", True, white)
- title3_text_rect = title3_text.get_rect(center=title3_rect.center)
- play_color = blue
- play = pygame.Rect(275, 300, 100, 50)
- play_font = pygame.font.SysFont(None, 40)
- play_text = play_font.render("PLAY", True, white)
- play_text_rect = play_text.get_rect(center=play.center)
- quit_color = pink
- quit = pygame.Rect(275, 400, 100, 50)
- quit_font = pygame.font.SysFont(None, 40)
- quit_text = quit_font.render("QUIT", True, white)
- quit_text_rect = quit_text.get_rect(center=quit.center)
- patchnotes_color=blue
- patchnotes=pygame.Rect(275,500,100,50)
- patchnotes_font=pygame.font.SysFont(None,30)
- patchnotes_text=patchnotes_font.render("PATCHES",True,white)
- patchnotes_text_rect=patchnotes_text.get_rect(center=patchnotes.center)
- board_color = green
- board = pygame.Rect(150, 150, 400, 250)
- board_font = pygame.font.SysFont(None, 40)
- level1_color = pink
- level1 = pygame.Rect(275, 200, 110, 50)
- level1_font = pygame.font.SysFont(None, 36)
- level1_text = level1_font.render("LEVEL 1", True, white)
- level1_text_rect = level1_text.get_rect(center=level1.center)
- level2_color = blue
- level2 = pygame.Rect(275, 275, 110, 50)
- level2_font = pygame.font.SysFont(None, 36)
- level2_text = level2_font.render("LEVEL 2", True, white)
- level2_text_rect = level2_text.get_rect(center=level2.center)
- level3_color = pink
- level3 = pygame.Rect(275, 350, 110, 50)
- level3_font = pygame.font.SysFont(None, 36)
- level3_text = level3_font.render("LEVEL 3", True, white)
- level3_text_rect = level3_text.get_rect(center=level3.center)
- level4_color = blue
- level4 = pygame.Rect(275, 425, 110, 50)
- level4_font = pygame.font.SysFont(None, 36)
- level4_text = level4_font.render("LEVEL 4", True, white)
- level4_text_rect = level4_text.get_rect(center=level4.center)
- back_color=pink
- back=pygame.Rect(200,500,100,50)
- back_font=pygame.font.SysFont(None,30)
- back_text=back_font.render("MENU",True,white)
- back_text_rect=back_text.get_rect(center=back.center)
- patches_back_color=pink
- patches_back=pygame.Rect(300,500,100,50)
- patches_back_font=pygame.font.SysFont(None,30)
- patches_back_text=patches_back_font.render("MENU",True,white)
- patches_back_text_rect=patches_back_text.get_rect(center=patches_back.center)
- retry_color=blue
- retry=pygame.Rect(400,500,100,50)
- retry_font=pygame.font.SysFont(None,30)
- retry_text=retry_font.render("RETRY",True,white)
- retry_text_rect=retry_text.get_rect(center=retry.center)
- view_wrong_answers = pygame.Rect(200, 400, 300, 50)
- view_wrong_answers_font = pygame.font.SysFont(None, 36)
- view_wrong_answers_text = view_wrong_answers_font.render("View Wrong Answers", True, white)
- view_wrong_answers_text_rect = view_wrong_answers_text.get_rect(center=view_wrong_answers.center)
- # Timer and score variables
- custom_timer_duration = 61000 # 60 seconds in milliseconds
- custom_timer_duration_2 = 121000
- start_time = 0
- questions_asked = 0
- correct_answers = 0
- wrong_answers = 0
- wrong_answer_list=[]
- current_Level=None
- # Game states
- MENU = 0
- LEVELS = 1
- LEVEL_1 = 2
- LEVEL_2 = 3
- LEVEL_3 = 4
- LEVEL_4 = 5
- END = 6
- PATCHNOTES = 7
- WRONG_ANSWERS = 8
- game_state = MENU
- text = "" # This holds the equation text.
- equation = False # To track if the equation has been set.
- user_input = "" # This will hold the user input.
- font = pygame.font.SysFont(None, 40)
- Patches=[
- "Version 0.0.2 Update:",
- "",
- "-added patchnote-Button - displays patchnotes",
- "-added a menu and a retry button",
- "-increased timer on levels 3 and 4 to 120 seconds",
- "-added a wrong answers list which shows the correct",
- "answers to the equations if a wrong answer was given",
- "",
- "-Version 0.0.3 Update:",
- "",
- "-Added a chibi Luny onto the level screens",
- "-Changed level time back to 60 seconds on levels 1 and 2",
- "-Added a menu button inside the patchnotes",
- "",
- "-Version 0.0.4 Update",
- "",
- "-Added reaction images based on the answer given",
- ]
- def draw_menu(window: pygame.Surface):
- window.fill(black)
- pygame.draw.rect(window, title1_color, title1_rect)
- window.blit(title1_text, title1_text_rect)
- pygame.draw.rect(window, title2_color, title2_rect)
- window.blit(title2_text, title2_text_rect)
- pygame.draw.rect(window, title3_color, title3_rect)
- window.blit(title3_text, title3_text_rect)
- pygame.draw.rect(window, play_color, play)
- window.blit(play_text, play_text_rect)
- pygame.draw.rect(window, quit_color, quit)
- window.blit(quit_text, quit_text_rect)
- pygame.draw.rect(window,patchnotes_color,patchnotes)
- window.blit(patchnotes_text,patchnotes_text_rect)
- def draw_levels(window: pygame.Surface):
- window.fill(black)
- pygame.draw.rect(window, level1_color, level1)
- window.blit(level1_text, level1_text_rect)
- pygame.draw.rect(window, level2_color, level2)
- window.blit(level2_text, level2_text_rect)
- pygame.draw.rect(window, level3_color, level3)
- window.blit(level3_text, level3_text_rect)
- pygame.draw.rect(window, level4_color, level4)
- window.blit(level4_text, level4_text_rect)
- def draw_game(window: pygame.Surface):
- pygame.draw.rect(window, board_color, board)
- if feedback_image:
- window.blit(feedback_image,(450,300))
- else:
- window.blit(luny_chibi,(450,300))
- def set_equation(level):
- global text
- if level == 1:
- a = random.randint(1, 9)
- b = random.randint(1, 9)
- elif level == 2:
- a = random.randint(10, 99) # 2-digit number
- b = random.randint(10, 99)
- elif level == 3:
- a = random.randint(100, 999) # 3-digit number
- b = random.randint(100, 999)
- elif level == 4:
- a = random.randint(1000, 9999) # 4-digit number
- b = random.randint(1000, 9999)
- text = str(a) + " + " + str(b)
- def draw_equation():
- board_text = board_font.render(text, True, white)
- board_text_rect = board_text.get_rect(center=board.center)
- window.blit(board_text, board_text_rect)
- def draw_input_box():
- # Draw the input box for the user to enter their response
- input_box = pygame.Rect(200, 450, 300, 40)
- pygame.draw.rect(window, white, input_box, 2) # Draw the border of the input box
- # Render the user input and display it in the input box
- input_text = font.render(user_input, True, white)
- input_text_rect = input_text.get_rect(center=input_box.center)
- window.blit(input_text, input_text_rect)
- return input_box
- # Timer display
- def draw_timer(remaining_time):
- timer_font = pygame.font.SysFont(None, 40)
- timer_text = timer_font.render(f"Time left: {remaining_time // 1000}s", True, white)
- window.blit(timer_text, (10, 10))
- # End screen
- def draw_end_screen():
- window.fill(black)
- end_text = end_font.render("Time's Up!", True, white)
- window.blit(end_text, (250, 150))
- summary = [
- f"Questions Asked: {questions_asked}",
- f"Correct Answers: {correct_answers}",
- f"Wrong Answers: {wrong_answers}"
- ]
- for i, line in enumerate(summary):
- summary_text = end_font.render(line, True, white)
- window.blit(summary_text, (200, 250 + i * 50))
- pygame.draw.rect(window,black,view_wrong_answers)
- window.blit(view_wrong_answers_text,view_wrong_answers_text_rect)
- def draw_patches():
- window.fill(black)
- for i,line in enumerate(Patches):
- patchnotes_text=patchnotes_font.render(line,True,white)
- window.blit(patchnotes_text,(100,100+i*20))
- def draw_back():
- pygame.draw.rect(window,back_color,back)
- window.blit(back_text,back_text_rect)
- def draw_patches_back():
- pygame.draw.rect(window,patches_back_color,patches_back)
- window.blit(patches_back_text,patches_back_text_rect)
- def draw_retry():
- pygame.draw.rect(window,retry_color,retry)
- window.blit(retry_text,retry_text_rect)
- def draw_wrong_answers():
- window.fill(black)
- answer_font=pygame.font.SysFont(None,36)
- for i, (text,user_input, correct_answer) in enumerate(wrong_answer_list):
- text_2 = f"Equation: {text} | Your Answer: {user_input} | Correct: {correct_answer}"
- answer_text = answer_font.render(text_2, True, white)
- window.blit(answer_text, (50, 150 + i * 30)) # List answers vertically
- def level_1():
- global equation
- if not equation: # Only set the equation once when entering LEVEL_1
- set_equation(1)
- equation = True # Mark the equation as set
- draw_equation() # Draw the equation on the board
- def level_2():
- global equation
- if not equation: # Only set the equation once when entering LEVEL_2
- set_equation(2)
- equation = True
- draw_equation()
- def level_3():
- global equation
- if not equation: # Only set the equation once when entering LEVEL_3
- set_equation(3)
- equation = True
- draw_equation()
- def level_4():
- global equation
- if not equation: # Only set the equation once when entering LEVEL_4
- set_equation(4)
- equation = True
- draw_equation()
- # Fonts for the end screen
- end_font = pygame.font.SysFont(None, 50)
- feedback_image=None
- feedback_timer=0
- # Update check_answer to count scores
- def check_answer(correct_answer):
- global user_input, questions_asked, correct_answers, wrong_answers,feedback_image,feedback_timer
- questions_asked += 1
- if user_input == str(correct_answer):
- correct_answers += 1
- feedback_image=luny_kluk
- feedback_timer=pygame.time.get_ticks()
- else:
- wrong_answers += 1
- feedback_image=luny_cry
- wrong_answer_list.append((text,user_input,correct_answer))
- feedback_timer=pygame.time.get_ticks()
- user_input = ""
- return True
- # Main loop
- running = True
- while running:
- pos = pygame.mouse.get_pos()
- current_time = pygame.time.get_ticks()
- elapsed_time = current_time - start_time
- if game_state in [LEVEL_1, LEVEL_2, LEVEL_3, LEVEL_4]:
- if feedback_image: # Show feedback when it's set
- window.blit(feedback_image, (450, 300)) # Adjust position as necessary
- if current_time - feedback_timer > 1000: # 1 second
- feedback_image = None # Clear feedback image after timeout
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- if event.type == pygame.MOUSEBUTTONDOWN:
- if game_state == MENU:
- if play.collidepoint(pos):
- game_state = LEVELS
- elif quit.collidepoint(pos):
- running = False
- break
- elif patchnotes.collidepoint(pos):
- game_state= PATCHNOTES
- elif game_state==PATCHNOTES:
- if patches_back.collidepoint(pos):
- game_state=MENU
- elif game_state == LEVELS:
- if level1.collidepoint(pos):
- game_state = LEVEL_1
- equation = False
- start_time = pygame.time.get_ticks() # Start the timer
- current_Level=LEVEL_1
- elif level2.collidepoint(pos):
- game_state = LEVEL_2
- equation = False
- start_time = pygame.time.get_ticks()
- current_Level=LEVEL_2
- elif level3.collidepoint(pos):
- game_state = LEVEL_3
- equation = False
- start_time = pygame.time.get_ticks()
- current_Level=LEVEL_3
- elif level4.collidepoint(pos):
- game_state = LEVEL_4
- equation = False
- start_time = pygame.time.get_ticks()
- current_Level=LEVEL_4
- elif game_state==END:
- if back.collidepoint(pos):
- game_state=MENU
- elif view_wrong_answers.collidepoint(pos):
- game_state=WRONG_ANSWERS
- elif retry.collidepoint(pos):
- # Reset game variables
- equation = False
- user_input = ""
- questions_asked = 0
- correct_answers = 0
- wrong_answers = 0
- wrong_answer_list.clear()
- start_time = pygame.time.get_ticks() # Restart the timer
- game_state = current_Level # Reload the current level
- elif game_state==WRONG_ANSWERS:
- if back.collidepoint(pos):
- game_state=END
- # Capture keyboard input for the input box
- if event.type == pygame.KEYDOWN and game_state in [LEVEL_1, LEVEL_2, LEVEL_3, LEVEL_4]:
- if event.key == pygame.K_RETURN: # Check when user presses Enter
- correct_answer = eval(text.replace(" + ", "+")) # Calculate the correct answer
- check_answer(correct_answer)
- equation = False
- elif event.key == pygame.K_BACKSPACE: # Handle backspace
- user_input = user_input[:-1]
- else:
- user_input += event.unicode # Add typed character to user input
- # Check if timer has run out
- if game_state in [LEVEL_1, LEVEL_2, LEVEL_3, LEVEL_4] and elapsed_time >= custom_timer_duration:
- game_state = END
- window.fill(black)
- if game_state == MENU:
- draw_menu(window)
- elif game_state == LEVELS:
- draw_levels(window)
- elif game_state == LEVEL_1:
- draw_game(window)
- level_1()
- draw_input_box()
- draw_timer(custom_timer_duration - elapsed_time)
- elif game_state == LEVEL_2:
- draw_game(window)
- level_2()
- draw_input_box()
- draw_timer(custom_timer_duration - elapsed_time)
- elif game_state == LEVEL_3:
- draw_game(window)
- level_3()
- draw_input_box()
- draw_timer(custom_timer_duration_2 - elapsed_time)
- elif game_state == LEVEL_4:
- draw_game(window)
- level_4()
- draw_input_box()
- draw_timer(custom_timer_duration_2 - elapsed_time)
- elif game_state == END:
- draw_end_screen()
- draw_back()
- draw_retry()
- elif game_state == PATCHNOTES:
- draw_patches()
- draw_patches_back()
- elif game_state==WRONG_ANSWERS:
- draw_wrong_answers()
- draw_back()
- pygame.display.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement