abbarnes

Multiplication Game

Nov 8th, 2018
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.30 KB | None | 0 0
  1. # CodeSkulptor runs Python programs in your browser.
  2. # Click the upper left button to run this simple demo.
  3.  
  4. # CodeSkulptor is tested to run in recent versions of
  5. # Chrome, Firefox, and Safari.
  6.  
  7. #https://py3.codeskulptor.org/#user302_u8p2IrZei3OtDaK.py
  8.  
  9. import simplegui
  10. import random
  11. import time
  12.  
  13. started = False
  14.  
  15. message = "Good Luck!"
  16. x = 0
  17. y = 0
  18. ans = 0
  19. user_ans = -1
  20. attempts = 0
  21. max_attempts = 20
  22. correct = 0
  23. times = []
  24.  
  25. ans_time = -float('inf')
  26. ans_color = 'Green'
  27. ans_message = ''
  28.  
  29. font_size = 48
  30. color = 'Red'
  31.  
  32. start_point_1 = (120, 20)
  33. end_point_1 = (120,20)
  34. end_point_2 = (480, 20)
  35.  
  36.  
  37.  
  38. nums = [3,4,6,7,8,9,12]
  39. xnums = list(range(13))
  40. xnums.extend(nums)
  41. random.shuffle(xnums)
  42. xnums_gen = (x_num for x_num in xnums)
  43. miss_list = []
  44.  
  45.  
  46. def gen_prob():
  47.     global x,y,ans
  48.     x = next(xnums_gen)
  49.     y = random.choice(nums)
  50.     ans = x*y
  51.     display_problem(x,y)
  52.    
  53.  
  54. def display_problem(x,y):
  55.     global message
  56.     #inp.set_text("")
  57.     message = "{} x {} = ?".format(str(x), str(y))
  58.    
  59. def display_ans():
  60.     global color, message
  61.     color = temp_color
  62.  
  63.    
  64.    
  65.    
  66. def miss():
  67.     global miss_list, ans_time, ans_color, ans_message
  68.     miss_list.append((x,y,x*y))
  69.     ans_time = time.time()
  70.     ans_color = 'Red'
  71.     ans_message = "{} x {} = {}".format(x,y,ans)
  72.    
  73. def print_misses(miss_list):
  74.     print("Study These:")
  75.     for miss in miss_list:
  76.         x = str(miss[0])
  77.         y = str(miss[1])
  78.         ans = str(miss[2])
  79.         print("{} x {} = {}".format(x,y,ans))
  80.        
  81. def hit():
  82.     global ans_time, ans_message, ans_color
  83.     ans_time = time.time()
  84.     ans_color = 'Green'
  85.     ans_message = "{} x {} = {}".format(x,y,ans)
  86.    
  87. def grade():
  88.     global message, started, times, font_size
  89.     #inp.set_text("")
  90.     started = False
  91.     runtime = int(time.time() - t1)
  92.     grade = 100*float(correct)/float(attempts)
  93.     message = "Grade: {}  Time: {} ".format(grade, runtime)
  94.     if miss_list:
  95.         print_misses(miss_list)
  96.     if grade > 90:
  97.         times.append(runtime)
  98.     if len(times) ==3:
  99.         font_size = 30
  100.         message = "Your three times were: {}, {}, {}".format(*times)
  101.     print(message)
  102.    
  103.    
  104. def check_answer():
  105.     if not started:
  106.         return
  107.     global ans, user_ans, attempts, correct, end_point_1, color, ans_message, ans_color, ans_time
  108.     attempts += 1
  109.     end_point_1 = (start_point_1[0] + (end_point_2[0]-start_point_1[0]) * attempts/max_attempts, end_point_1[1])
  110.     if user_ans == ans:
  111.         correct += 1
  112.         hit()
  113.     else:
  114.         miss()
  115.     user_ans = None
  116.     if attempts < max_attempts:
  117.         gen_prob()
  118.     else:
  119.         grade()
  120.     return
  121.  
  122. # Handler for mouse click
  123. def click():
  124.     global t1, attempts, correct, started
  125.     started = True
  126.     attempts = 0
  127.     correct = 0
  128.     t1 = time.time()
  129.     gen_prob()
  130.    
  131. def key_handler(key):
  132.     global user_ans
  133.     if key == 13:
  134.           check_answer()
  135.     elif key < 58 and key > 47:
  136.           char = chr(key)
  137.           if user_ans <0:
  138.               user_ans = int(char)
  139.           else:
  140.               user_ans = int(str(user_ans) + char)
  141.     elif key == 8:
  142.           user_ans = -1
  143.     else:
  144.         pass
  145.          
  146.    
  147.  
  148. # Handler to draw on canvas
  149. def draw(canvas):
  150.     check_time = ans_time + 1
  151.     if ans_color == 'Red':
  152.         check_time += 2
  153.     if time.time() > check_time:
  154.         canvas.draw_text(message, [112,112], font_size, color)
  155.     else:
  156.         canvas.draw_text(ans_message, [112,112], font_size, ans_color)
  157.         if ans_color == 'Red':
  158.             canvas.draw_text('INCORRECT :(', [112,212], font_size-5, ans_color)
  159.         else:
  160.             canvas.draw_text('CORRECT :)', [112,212], font_size-5, ans_color)
  161.     if started:
  162.           if user_ans >= 0:
  163.               canvas.draw_text(str(user_ans), [112,212], 48, "Red")
  164.               canvas.draw_line(start_point_1, end_point_1, 10, "Grey")
  165.               canvas.draw_line(end_point_1, end_point_2, 10, "White")
  166.  
  167. # Create a frame and assign callbacks to event handlers
  168. frame = simplegui.create_frame("Home", 600, 300)
  169. frame.add_button("Start", click)
  170. frame.set_draw_handler(draw)
  171. #inp = frame.add_input("Answer", answer_box, 50)
  172. #inp.set_text("")
  173. frame.set_keyup_handler(key_handler)
  174.  
  175. # Start the frame animation
  176. frame.start()
Advertisement
Add Comment
Please, Sign In to add comment