Guest User

Untitled

a guest
Jan 23rd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. import random
  2. from tkinter import *
  3. import tkinter as tk
  4. import time
  5. number_rounds = 2
  6. #sec = 30
  7. operations = ['add', 'subtract', 'multiply', 'divide', 'end']
  8. cound=40
  9.  
  10. def countdown(count):
  11. label['text'] = count
  12. global cound
  13. global ytr
  14. #global knop
  15. cound=count
  16. if count > 0:
  17. if ytr is True:
  18. root.after(1000, countdown, count-1)
  19. if cound == 0:
  20. lose=Label(root, text= "You lose!n But You can try again ", bg="blue", font="arial 25").pack()
  21. #knop=Button(root,text="Play again!",command=start_name(_)).pack()
  22. lose.grid_destroy()
  23. start_game
  24. return count
  25.  
  26.  
  27.  
  28.  
  29. def get_name(_):
  30. print("HELLO", contents.get())
  31. text = contents.get()
  32. if not text: return
  33. lbl['text'] = "Hello, " + contents.get()
  34.  
  35. entry.destroy()
  36.  
  37. start_game()
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44. def create_task():
  45. x = random.randint(1, 20)
  46. y = random.randint(1, 20)
  47. return x, y
  48.  
  49.  
  50.  
  51. def check_results(actual, expected, frame):
  52. global number_rounds
  53. global operations
  54.  
  55. print(actual, expected)
  56. if actual == expected:
  57.  
  58. frame.destroy()
  59. number_rounds -= 1
  60. if number_rounds > 0:
  61. display_task(operations[0])
  62. else:
  63. operation, *operations = operations
  64. if cound!=0:
  65. print(cound)
  66. if operation != 'end':
  67. number_rounds = 2
  68. display_task(operation)
  69. else:
  70. Label(root,text="You win").pack()
  71. Label(root,text="Remained"+str(cound-1)+"seconds!").pack()
  72. #knop=Button(root,text="Play again!",command=play_again).pack()
  73. global ytr
  74. ytr=False
  75. l.destroy()
  76. #lbl.grid_remove()###
  77. # elif cound==0:
  78. # Label(root,text="You lose!").pack()
  79. # Label.grid_remove()
  80.  
  81.  
  82.  
  83. root = Tk()
  84.  
  85. root.geometry("600x500")
  86. root.configure(background='lightblue')
  87. label = tk.Label(root,font=("Helvetica",25))
  88. label.place(x=35, y=15)
  89.  
  90.  
  91. root.title("MATH GAME")
  92. lbl = Label(root, text="PLAYER NAME", bg="lightblue")
  93. timeX = Label(root, fg="green")
  94. entry = Entry(width=15)
  95. contents = StringVar()
  96. entry['textvariable'] = contents
  97.  
  98. lbl.place(x=247, y=40)
  99. entry.place(x=242, y=60)
  100. timeX.place(x=235, y=180)
  101.  
  102. btn = Button(root, width=20, height=6, text="START GAME", bg="yellow")
  103. btn.place(x=220, y=150)
  104. btn.bind("<Button-1>", get_name)
  105. ytr=True
  106. Exitbtn = Button(root, width=10, height=2, text="EXIT", bg="red")
  107.  
  108. Exitbtn.bind("<Button-1>", quit)
  109. Exitbtn.place(x=250, y=450)
  110.  
  111. b=0
  112. def cont():
  113. b = countdown(15)
  114.  
  115.  
  116.  
  117.  
  118. def start_game():
  119. btn.grid_remove()
  120. display_task(operations[0])
  121. b=countdown(15)
  122.  
  123.  
  124.  
  125. def display_task(operation):
  126. x, y = create_task()
  127. frame = Frame(root, width=200, height=200)
  128. frame.place(x=150, y=150)
  129.  
  130.  
  131. if operation == 'add':
  132. expected = x + y
  133. op = '+'
  134. elif operation == 'subtract':
  135. expected = x - y
  136. op = '-'
  137. elif operation == 'multiply':
  138. expected = x * y
  139. op = '*'
  140. else:
  141. expected = round(x/y,1)
  142. op = '/'
  143.  
  144.  
  145.  
  146. label_task = Label(frame, text=f"{x} {op} {y} = ?")
  147. label_task.grid(column=1, row=1, columnspan=2)
  148.  
  149. results = [expected] + [random.randint(0, 20) for _ in range(3)]
  150. random.shuffle(results)
  151.  
  152.  
  153. button = Button(frame, text=f"{results[0]}",width=20,height=6, bg="pink")
  154. button.bind("<Button-1>", lambda a: check_results(results[0], expected, frame))
  155. button.grid(column=1, row=2)
  156.  
  157. button = Button(frame, text=f"{results[1]}",width=20,height=6, bg="pink")
  158. button.bind("<Button-1>", lambda a: check_results(results[1], expected, frame))
  159. button.grid(column=2, row=2)
  160.  
  161. button = Button(frame, text=f"{results[2]}",width=20,height=6, bg="pink")
  162. button.bind("<Button-1>", lambda a: check_results(results[2], expected, frame))
  163. button.grid(column=1, row=3)
  164.  
  165. button = Button(frame, text=f"{results[3]}",width=20,height=6, bg="pink")
  166. button.bind("<Button-1>", lambda a: check_results(results[3], expected, frame))
  167. button.grid(column=2, row=3)
  168.  
  169.  
  170. root.mainloop()
Add Comment
Please, Sign In to add comment