Advertisement
Melanie1204

Untitled

Dec 14th, 2024 (edited)
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 15.72 KB | None | 0 0
  1. import pygame
  2. import random
  3. import sys
  4. import os
  5.  
  6. base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
  7.  
  8.  
  9. pygame.init()
  10. size = (700, 700)
  11. black = (0, 0, 0)
  12. white = (255, 255, 255)
  13. pink = (255, 73, 243)
  14. blue = (75, 193, 255)
  15. green = (0, 255, 0)
  16. window = pygame.display.set_mode(size)
  17.  
  18. luny_chibi=pygame.image.load(os.path.join(base_path, "luny chibi.png"))
  19. luny_kluk=pygame.image.load(os.path.join(base_path, "luny chibi kluk.png"))
  20. luny_chibi=pygame.transform.scale(luny_chibi,(200,200))
  21. luny_kluk=pygame.transform.scale(luny_kluk,(200,200))
  22. luny_cry=pygame.image.load(os.path.join(base_path, "luny chibi cry.png"))
  23. luny_cry=pygame.transform.scale(luny_cry,(200,200))
  24.  
  25. title1_color = (0, 0, 0)
  26. title1_rect = pygame.Rect(300, 100, 50, 50)
  27. title1_font = pygame.font.SysFont(None, 46)
  28. title1_text = title1_font.render("Luny-", True, pink)
  29. title1_text_rect = title1_text.get_rect(center=title1_rect.center)
  30.  
  31. title2_color = (0, 0, 0)
  32. title2_rect = pygame.Rect(300, 150, 50, 50)
  33. title2_font = pygame.font.SysFont(None, 46)
  34. title2_text = title2_font.render("Shimas", True, blue)
  35. title2_text_rect = title2_text.get_rect(center=title2_rect.center)
  36.  
  37. title3_color = (0, 0, 0)
  38. title3_rect = pygame.Rect(300, 200, 50, 50)
  39. title3_font = pygame.font.SysFont(None, 46)
  40. title3_text = title3_font.render("Gehirnjogging", True, white)
  41. title3_text_rect = title3_text.get_rect(center=title3_rect.center)
  42.  
  43. play_color = blue
  44. play = pygame.Rect(275, 300, 100, 50)
  45. play_font = pygame.font.SysFont(None, 40)
  46. play_text = play_font.render("PLAY", True, white)
  47. play_text_rect = play_text.get_rect(center=play.center)
  48.  
  49. quit_color = pink
  50. quit = pygame.Rect(275, 400, 100, 50)
  51. quit_font = pygame.font.SysFont(None, 40)
  52. quit_text = quit_font.render("QUIT", True, white)
  53. quit_text_rect = quit_text.get_rect(center=quit.center)
  54.  
  55. patchnotes_color=blue
  56. patchnotes=pygame.Rect(275,500,100,50)
  57. patchnotes_font=pygame.font.SysFont(None,30)
  58. patchnotes_text=patchnotes_font.render("PATCHES",True,white)
  59. patchnotes_text_rect=patchnotes_text.get_rect(center=patchnotes.center)
  60.  
  61.  
  62. board_color = green
  63. board = pygame.Rect(150, 150, 400, 250)
  64. board_font = pygame.font.SysFont(None, 40)
  65.  
  66. level1_color = pink
  67. level1 = pygame.Rect(275, 200, 110, 50)
  68. level1_font = pygame.font.SysFont(None, 36)
  69. level1_text = level1_font.render("LEVEL 1", True, white)
  70. level1_text_rect = level1_text.get_rect(center=level1.center)
  71.  
  72. level2_color = blue
  73. level2 = pygame.Rect(275, 275, 110, 50)
  74. level2_font = pygame.font.SysFont(None, 36)
  75. level2_text = level2_font.render("LEVEL 2", True, white)
  76. level2_text_rect = level2_text.get_rect(center=level2.center)
  77.  
  78. level3_color = pink
  79. level3 = pygame.Rect(275, 350, 110, 50)
  80. level3_font = pygame.font.SysFont(None, 36)
  81. level3_text = level3_font.render("LEVEL 3", True, white)
  82. level3_text_rect = level3_text.get_rect(center=level3.center)
  83.  
  84. level4_color = blue
  85. level4 = pygame.Rect(275, 425, 110, 50)
  86. level4_font = pygame.font.SysFont(None, 36)
  87. level4_text = level4_font.render("LEVEL 4", True, white)
  88. level4_text_rect = level4_text.get_rect(center=level4.center)
  89.  
  90. back_color=pink
  91. back=pygame.Rect(200,500,100,50)
  92. back_font=pygame.font.SysFont(None,30)
  93. back_text=back_font.render("MENU",True,white)
  94. back_text_rect=back_text.get_rect(center=back.center)
  95.  
  96. patches_back_color=pink
  97. patches_back=pygame.Rect(300,500,100,50)
  98. patches_back_font=pygame.font.SysFont(None,30)
  99. patches_back_text=patches_back_font.render("MENU",True,white)
  100. patches_back_text_rect=patches_back_text.get_rect(center=patches_back.center)
  101.  
  102. retry_color=blue
  103. retry=pygame.Rect(400,500,100,50)
  104. retry_font=pygame.font.SysFont(None,30)
  105. retry_text=retry_font.render("RETRY",True,white)
  106. retry_text_rect=retry_text.get_rect(center=retry.center)
  107.  
  108. view_wrong_answers = pygame.Rect(200, 400, 300, 50)
  109. view_wrong_answers_font = pygame.font.SysFont(None, 36)
  110. view_wrong_answers_text = view_wrong_answers_font.render("View Wrong Answers", True, white)
  111. view_wrong_answers_text_rect = view_wrong_answers_text.get_rect(center=view_wrong_answers.center)
  112.  
  113. # Timer and score variables
  114. custom_timer_duration = 61000  # 60 seconds in milliseconds
  115. custom_timer_duration_2 = 121000
  116. start_time = 0
  117. questions_asked = 0
  118. correct_answers = 0
  119. wrong_answers = 0
  120. wrong_answer_list=[]
  121. current_Level=None
  122.  
  123. # Game states
  124. MENU = 0
  125. LEVELS = 1
  126. LEVEL_1 = 2
  127. LEVEL_2 = 3
  128. LEVEL_3 = 4
  129. LEVEL_4 = 5
  130. END = 6
  131. PATCHNOTES = 7
  132. WRONG_ANSWERS = 8
  133.  
  134. game_state = MENU
  135.  
  136. text = ""  # This holds the equation text.
  137. equation = False  # To track if the equation has been set.
  138. user_input = ""  # This will hold the user input.
  139. font = pygame.font.SysFont(None, 40)
  140.  
  141.  
  142. Patches=[
  143.     "Version 0.0.2 Update:",
  144.     "",
  145.     "-added patchnote-Button - displays patchnotes",
  146.     "-added a menu and a retry button",
  147.     "-increased timer on levels 3 and 4 to 120 seconds",
  148.     "-added a wrong answers list which shows the correct",
  149.     "answers to the equations if a wrong answer was given",
  150.     "",
  151.     "-Version 0.0.3 Update:",
  152.     "",
  153.     "-Added a chibi Luny onto the level screens",
  154.     "-Changed level time back to 60 seconds on levels 1 and 2",
  155.     "-Added a menu button inside the patchnotes",
  156.     "",
  157.     "-Version 0.0.4 Update",
  158.     "",
  159.     "-Added reaction images based on the answer given",
  160. ]
  161.  
  162. def draw_menu(window: pygame.Surface):
  163.     window.fill(black)
  164.     pygame.draw.rect(window, title1_color, title1_rect)
  165.     window.blit(title1_text, title1_text_rect)
  166.  
  167.     pygame.draw.rect(window, title2_color, title2_rect)
  168.     window.blit(title2_text, title2_text_rect)
  169.  
  170.     pygame.draw.rect(window, title3_color, title3_rect)
  171.     window.blit(title3_text, title3_text_rect)
  172.  
  173.     pygame.draw.rect(window, play_color, play)
  174.     window.blit(play_text, play_text_rect)
  175.  
  176.     pygame.draw.rect(window, quit_color, quit)
  177.     window.blit(quit_text, quit_text_rect)
  178.  
  179.     pygame.draw.rect(window,patchnotes_color,patchnotes)
  180.     window.blit(patchnotes_text,patchnotes_text_rect)
  181.  
  182.  
  183. def draw_levels(window: pygame.Surface):
  184.     window.fill(black)
  185.     pygame.draw.rect(window, level1_color, level1)
  186.     window.blit(level1_text, level1_text_rect)
  187.  
  188.     pygame.draw.rect(window, level2_color, level2)
  189.     window.blit(level2_text, level2_text_rect)
  190.  
  191.     pygame.draw.rect(window, level3_color, level3)
  192.     window.blit(level3_text, level3_text_rect)
  193.  
  194.     pygame.draw.rect(window, level4_color, level4)
  195.     window.blit(level4_text, level4_text_rect)
  196.  
  197.  
  198. def draw_game(window: pygame.Surface):
  199.     pygame.draw.rect(window, board_color, board)
  200.  
  201.     if feedback_image:
  202.         window.blit(feedback_image,(450,300))
  203.     else:
  204.         window.blit(luny_chibi,(450,300))
  205.  
  206.  
  207. def set_equation(level):
  208.     global text
  209.     if level == 1:
  210.         a = random.randint(1, 9)
  211.         b = random.randint(1, 9)
  212.     elif level == 2:
  213.         a = random.randint(10, 99)  # 2-digit number
  214.         b = random.randint(10, 99)
  215.     elif level == 3:
  216.         a = random.randint(100, 999)  # 3-digit number
  217.         b = random.randint(100, 999)
  218.     elif level == 4:
  219.         a = random.randint(1000, 9999)  # 4-digit number
  220.         b = random.randint(1000, 9999)
  221.  
  222.     text = str(a) + " + " + str(b)
  223.  
  224.  
  225. def draw_equation():
  226.     board_text = board_font.render(text, True, white)
  227.     board_text_rect = board_text.get_rect(center=board.center)
  228.     window.blit(board_text, board_text_rect)
  229.  
  230.  
  231. def draw_input_box():
  232.     # Draw the input box for the user to enter their response
  233.     input_box = pygame.Rect(200, 450, 300, 40)
  234.     pygame.draw.rect(window, white, input_box, 2)  # Draw the border of the input box
  235.  
  236.     # Render the user input and display it in the input box
  237.     input_text = font.render(user_input, True, white)
  238.     input_text_rect = input_text.get_rect(center=input_box.center)
  239.     window.blit(input_text, input_text_rect)
  240.  
  241.     return input_box
  242.  
  243. # Timer display
  244. def draw_timer(remaining_time):
  245.     timer_font = pygame.font.SysFont(None, 40)
  246.     timer_text = timer_font.render(f"Time left: {remaining_time // 1000}s", True, white)
  247.     window.blit(timer_text, (10, 10))
  248.  
  249. # End screen
  250. def draw_end_screen():
  251.     window.fill(black)
  252.     end_text = end_font.render("Time's Up!", True, white)
  253.     window.blit(end_text, (250, 150))
  254.  
  255.     summary = [
  256.         f"Questions Asked: {questions_asked}",
  257.         f"Correct Answers: {correct_answers}",
  258.         f"Wrong Answers: {wrong_answers}"
  259.     ]
  260.  
  261.     for i, line in enumerate(summary):
  262.         summary_text = end_font.render(line, True, white)
  263.         window.blit(summary_text, (200, 250 + i * 50))
  264.  
  265.     pygame.draw.rect(window,black,view_wrong_answers)
  266.     window.blit(view_wrong_answers_text,view_wrong_answers_text_rect)
  267.  
  268. def draw_patches():
  269.     window.fill(black)
  270.     for i,line in enumerate(Patches):
  271.         patchnotes_text=patchnotes_font.render(line,True,white)
  272.         window.blit(patchnotes_text,(100,100+i*20))
  273.  
  274.  
  275.  
  276. def draw_back():
  277.     pygame.draw.rect(window,back_color,back)
  278.     window.blit(back_text,back_text_rect)
  279.  
  280. def draw_patches_back():
  281.     pygame.draw.rect(window,patches_back_color,patches_back)
  282.     window.blit(patches_back_text,patches_back_text_rect)
  283.  
  284. def draw_retry():
  285.     pygame.draw.rect(window,retry_color,retry)
  286.     window.blit(retry_text,retry_text_rect)
  287.  
  288. def draw_wrong_answers():
  289.     window.fill(black)
  290.     answer_font=pygame.font.SysFont(None,36)
  291.     for i, (text,user_input, correct_answer) in enumerate(wrong_answer_list):
  292.             text_2 = f"Equation: {text} | Your Answer: {user_input} | Correct: {correct_answer}"
  293.             answer_text = answer_font.render(text_2, True, white)
  294.             window.blit(answer_text, (50, 150 + i * 30))  # List answers vertically
  295.  
  296. def level_1():
  297.     global equation
  298.     if not equation:  # Only set the equation once when entering LEVEL_1
  299.         set_equation(1)
  300.         equation = True  # Mark the equation as set
  301.     draw_equation()  # Draw the equation on the board
  302.    
  303.  
  304. def level_2():
  305.     global equation
  306.     if not equation:  # Only set the equation once when entering LEVEL_2
  307.         set_equation(2)
  308.         equation = True
  309.     draw_equation()
  310.  
  311.  
  312. def level_3():
  313.     global equation
  314.     if not equation:  # Only set the equation once when entering LEVEL_3
  315.         set_equation(3)
  316.         equation = True
  317.     draw_equation()
  318.  
  319.  
  320. def level_4():
  321.     global equation
  322.     if not equation:  # Only set the equation once when entering LEVEL_4
  323.         set_equation(4)
  324.         equation = True
  325.     draw_equation()
  326.  
  327. # Fonts for the end screen
  328. end_font = pygame.font.SysFont(None, 50)
  329. feedback_image=None
  330. feedback_timer=0
  331.  
  332. # Update check_answer to count scores
  333. def check_answer(correct_answer):
  334.     global user_input, questions_asked, correct_answers, wrong_answers,feedback_image,feedback_timer
  335.     questions_asked += 1
  336.     if user_input == str(correct_answer):
  337.         correct_answers += 1
  338.         feedback_image=luny_kluk
  339.         feedback_timer=pygame.time.get_ticks()
  340.     else:
  341.         wrong_answers += 1
  342.         feedback_image=luny_cry
  343.         wrong_answer_list.append((text,user_input,correct_answer))
  344.         feedback_timer=pygame.time.get_ticks()
  345.     user_input = ""
  346.     return True
  347.  
  348. # Main loop
  349. running = True
  350. while running:
  351.     pos = pygame.mouse.get_pos()
  352.     current_time = pygame.time.get_ticks()
  353.     elapsed_time = current_time - start_time
  354.  
  355.     if game_state in [LEVEL_1, LEVEL_2, LEVEL_3, LEVEL_4]:
  356.         if feedback_image:  # Show feedback when it's set
  357.             window.blit(feedback_image, (450, 300))  # Adjust position as necessary
  358.             if current_time - feedback_timer > 1000:  # 1 second
  359.                 feedback_image = None  # Clear feedback image after timeout
  360.  
  361.  
  362.     for event in pygame.event.get():
  363.         if event.type == pygame.QUIT:
  364.             running = False
  365.         if event.type == pygame.MOUSEBUTTONDOWN:
  366.             if game_state == MENU:
  367.                 if play.collidepoint(pos):
  368.                     game_state = LEVELS
  369.                 elif quit.collidepoint(pos):
  370.                     running = False
  371.                     break
  372.                 elif patchnotes.collidepoint(pos):
  373.                     game_state= PATCHNOTES
  374.             elif game_state==PATCHNOTES:
  375.                 if patches_back.collidepoint(pos):
  376.                     game_state=MENU
  377.             elif game_state == LEVELS:
  378.                 if level1.collidepoint(pos):
  379.                     game_state = LEVEL_1
  380.                     equation = False
  381.                     start_time = pygame.time.get_ticks()  # Start the timer
  382.                     current_Level=LEVEL_1
  383.                 elif level2.collidepoint(pos):
  384.                     game_state = LEVEL_2
  385.                     equation = False
  386.                     start_time = pygame.time.get_ticks()
  387.                     current_Level=LEVEL_2
  388.                 elif level3.collidepoint(pos):
  389.                     game_state = LEVEL_3
  390.                     equation = False
  391.                     start_time = pygame.time.get_ticks()
  392.                     current_Level=LEVEL_3
  393.                 elif level4.collidepoint(pos):
  394.                     game_state = LEVEL_4
  395.                     equation = False
  396.                     start_time = pygame.time.get_ticks()
  397.                     current_Level=LEVEL_4
  398.             elif game_state==END:
  399.                 if back.collidepoint(pos):
  400.                     game_state=MENU
  401.                 elif view_wrong_answers.collidepoint(pos):
  402.                     game_state=WRONG_ANSWERS
  403.                 elif retry.collidepoint(pos):
  404.         # Reset game variables
  405.                  equation = False
  406.                  user_input = ""
  407.                  questions_asked = 0
  408.                  correct_answers = 0
  409.                  wrong_answers = 0
  410.                  wrong_answer_list.clear()
  411.                  start_time = pygame.time.get_ticks()  # Restart the timer
  412.                  game_state = current_Level  # Reload the current level
  413.             elif game_state==WRONG_ANSWERS:
  414.                 if back.collidepoint(pos):
  415.                     game_state=END
  416.  
  417.         # Capture keyboard input for the input box
  418.         if event.type == pygame.KEYDOWN and game_state in [LEVEL_1, LEVEL_2, LEVEL_3, LEVEL_4]:
  419.             if event.key == pygame.K_RETURN:  # Check when user presses Enter
  420.                 correct_answer = eval(text.replace(" + ", "+"))  # Calculate the correct answer
  421.                 check_answer(correct_answer)
  422.                 equation = False
  423.             elif event.key == pygame.K_BACKSPACE:  # Handle backspace
  424.                 user_input = user_input[:-1]
  425.             else:
  426.                 user_input += event.unicode  # Add typed character to user input
  427.  
  428.     # Check if timer has run out
  429.     if game_state in [LEVEL_1, LEVEL_2, LEVEL_3, LEVEL_4] and elapsed_time >= custom_timer_duration:
  430.         game_state = END
  431.  
  432.     window.fill(black)
  433.  
  434.     if game_state == MENU:
  435.         draw_menu(window)
  436.     elif game_state == LEVELS:
  437.         draw_levels(window)
  438.     elif game_state == LEVEL_1:
  439.         draw_game(window)
  440.         level_1()
  441.         draw_input_box()
  442.         draw_timer(custom_timer_duration - elapsed_time)
  443.     elif game_state == LEVEL_2:
  444.         draw_game(window)
  445.         level_2()
  446.         draw_input_box()
  447.         draw_timer(custom_timer_duration - elapsed_time)
  448.     elif game_state == LEVEL_3:
  449.         draw_game(window)
  450.         level_3()
  451.         draw_input_box()
  452.         draw_timer(custom_timer_duration_2 - elapsed_time)
  453.     elif game_state == LEVEL_4:
  454.         draw_game(window)
  455.         level_4()
  456.         draw_input_box()
  457.         draw_timer(custom_timer_duration_2 - elapsed_time)
  458.     elif game_state == END:
  459.         draw_end_screen()
  460.         draw_back()
  461.         draw_retry()
  462.     elif game_state == PATCHNOTES:
  463.         draw_patches()
  464.         draw_patches_back()
  465.     elif game_state==WRONG_ANSWERS:
  466.         draw_wrong_answers()
  467.         draw_back()
  468.  
  469.     pygame.display.update()
  470.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement