Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- from tkinter import *
- import tkinter as tk
- import time
- number_rounds = 2
- #sec = 30
- operations = ['add', 'subtract', 'multiply', 'divide', 'end']
- cound=40
- def countdown(count):
- label['text'] = count
- global cound
- global ytr
- #global knop
- cound=count
- if count > 0:
- if ytr is True:
- root.after(1000, countdown, count-1)
- if cound == 0:
- lose=Label(root, text= "You lose!n But You can try again ", bg="blue", font="arial 25").pack()
- #knop=Button(root,text="Play again!",command=start_name(_)).pack()
- lose.grid_destroy()
- start_game
- return count
- def get_name(_):
- print("HELLO", contents.get())
- text = contents.get()
- if not text: return
- lbl['text'] = "Hello, " + contents.get()
- entry.destroy()
- start_game()
- def create_task():
- x = random.randint(1, 20)
- y = random.randint(1, 20)
- return x, y
- def check_results(actual, expected, frame):
- global number_rounds
- global operations
- print(actual, expected)
- if actual == expected:
- frame.destroy()
- number_rounds -= 1
- if number_rounds > 0:
- display_task(operations[0])
- else:
- operation, *operations = operations
- if cound!=0:
- print(cound)
- if operation != 'end':
- number_rounds = 2
- display_task(operation)
- else:
- Label(root,text="You win").pack()
- Label(root,text="Remained"+str(cound-1)+"seconds!").pack()
- #knop=Button(root,text="Play again!",command=play_again).pack()
- global ytr
- ytr=False
- l.destroy()
- #lbl.grid_remove()###
- # elif cound==0:
- # Label(root,text="You lose!").pack()
- # Label.grid_remove()
- root = Tk()
- root.geometry("600x500")
- root.configure(background='lightblue')
- label = tk.Label(root,font=("Helvetica",25))
- label.place(x=35, y=15)
- root.title("MATH GAME")
- lbl = Label(root, text="PLAYER NAME", bg="lightblue")
- timeX = Label(root, fg="green")
- entry = Entry(width=15)
- contents = StringVar()
- entry['textvariable'] = contents
- lbl.place(x=247, y=40)
- entry.place(x=242, y=60)
- timeX.place(x=235, y=180)
- btn = Button(root, width=20, height=6, text="START GAME", bg="yellow")
- btn.place(x=220, y=150)
- btn.bind("<Button-1>", get_name)
- ytr=True
- Exitbtn = Button(root, width=10, height=2, text="EXIT", bg="red")
- Exitbtn.bind("<Button-1>", quit)
- Exitbtn.place(x=250, y=450)
- b=0
- def cont():
- b = countdown(15)
- def start_game():
- btn.grid_remove()
- display_task(operations[0])
- b=countdown(15)
- def display_task(operation):
- x, y = create_task()
- frame = Frame(root, width=200, height=200)
- frame.place(x=150, y=150)
- if operation == 'add':
- expected = x + y
- op = '+'
- elif operation == 'subtract':
- expected = x - y
- op = '-'
- elif operation == 'multiply':
- expected = x * y
- op = '*'
- else:
- expected = round(x/y,1)
- op = '/'
- label_task = Label(frame, text=f"{x} {op} {y} = ?")
- label_task.grid(column=1, row=1, columnspan=2)
- results = [expected] + [random.randint(0, 20) for _ in range(3)]
- random.shuffle(results)
- button = Button(frame, text=f"{results[0]}",width=20,height=6, bg="pink")
- button.bind("<Button-1>", lambda a: check_results(results[0], expected, frame))
- button.grid(column=1, row=2)
- button = Button(frame, text=f"{results[1]}",width=20,height=6, bg="pink")
- button.bind("<Button-1>", lambda a: check_results(results[1], expected, frame))
- button.grid(column=2, row=2)
- button = Button(frame, text=f"{results[2]}",width=20,height=6, bg="pink")
- button.bind("<Button-1>", lambda a: check_results(results[2], expected, frame))
- button.grid(column=1, row=3)
- button = Button(frame, text=f"{results[3]}",width=20,height=6, bg="pink")
- button.bind("<Button-1>", lambda a: check_results(results[3], expected, frame))
- button.grid(column=2, row=3)
- root.mainloop()
Add Comment
Please, Sign In to add comment