diff options
| -rw-r--r-- | reactiontime.py | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/reactiontime.py b/reactiontime.py new file mode 100644 index 0000000..482bd4e --- /dev/null +++ b/reactiontime.py | |||
| @@ -0,0 +1,48 @@ | |||
| 1 | import random | ||
| 2 | import time | ||
| 3 | from tkinter import * | ||
| 4 | from tkinter import messagebox | ||
| 5 | |||
| 6 | |||
| 7 | def start_game(): | ||
| 8 | start_time = time.time() | ||
| 9 | |||
| 10 | def show_green_screen(): | ||
| 11 | root.configure(bg="green") | ||
| 12 | root.bind("<Button-1>", lambda event, start_time=start_time: check_reaction_time(start_time)) | ||
| 13 | global randInt | ||
| 14 | randInt = random.randint(1000, 7000) | ||
| 15 | root.after(randInt, show_green_screen) # Random delay between 1 to 5 seconds | ||
| 16 | |||
| 17 | def check_reaction_time(start_time): | ||
| 18 | end_time = time.time() | ||
| 19 | reaction_time = ((end_time - start_time) * 1000) - randInt # Convert to milliseconds | ||
| 20 | messagebox.showinfo(title="Reaction Time", message=f"Your reaction time: {reaction_time:.3f} ms") | ||
| 21 | root.configure(bg="black") | ||
| 22 | root.unbind("<Button-1>") | ||
| 23 | |||
| 24 | |||
| 25 | def toggle_full_screen(): | ||
| 26 | if root.attributes("-fullscreen"): | ||
| 27 | root.attributes("-fullscreen", True) | ||
| 28 | else: | ||
| 29 | root.attributes("-fullscreen", False) | ||
| 30 | |||
| 31 | def exit_program(): | ||
| 32 | root.destroy() | ||
| 33 | |||
| 34 | root = Tk() | ||
| 35 | root.title("ReactionTime") | ||
| 36 | root.geometry('800x600') | ||
| 37 | root.configure(bg="black") | ||
| 38 | |||
| 39 | startBtn = Button(root, text="Start", bg="white", fg="black", command=start_game) | ||
| 40 | startBtn.grid(column=2, row=2) | ||
| 41 | |||
| 42 | fullScreenBtn = Button(root, text="Toggle Fullscreen", bg="white", fg="black", command=toggle_full_screen) | ||
| 43 | fullScreenBtn.grid(column=4, row=2) | ||
| 44 | |||
| 45 | exitBtn = Button(root, text="Exit", bg="white", fg="black", command=exit_program) | ||
| 46 | exitBtn.grid(column=8, row=2) | ||
| 47 | |||
| 48 | root.mainloop() | ||
