Skip to content

Instantly share code, notes, and snippets.

@agscala
Created August 28, 2024 16:02
Show Gist options
  • Save agscala/5611ca4aa77efbd195e5041f8f81a0ab to your computer and use it in GitHub Desktop.
Save agscala/5611ca4aa77efbd195e5041f8f81a0ab to your computer and use it in GitHub Desktop.
Reaction Test Game
# HOW TO RUN:
# Save this to reaction-test.py
# Running this: python3 reaction-test.py
import os
import pickle
import platform
import random
import signal
import statistics
import sys
import time
# File to store the reaction times
PICKLE_FILE = "reaction_times.pkl"
# Load existing reaction times if the file exists
if os.path.exists(PICKLE_FILE):
with open(PICKLE_FILE, "rb") as f:
all_time_reaction_times = pickle.load(f)
else:
all_time_reaction_times = []
# Current session reaction times
current_session_reaction_times = []
def save_and_exit():
# Merge current session reaction times with all-time reaction times
all_time_reaction_times.extend(current_session_reaction_times)
# Save the all-time reaction times to a pickle file before exiting
with open(PICKLE_FILE, "wb") as f:
pickle.dump(all_time_reaction_times, f)
print("\n> Reaction times saved.")
sys.exit(0)
def clear_screen():
if platform.system() == 'Windows':
os.system('cls')
else:
os.system('clear')
def calculate_statistics(reaction_times):
min_time = min(reaction_times)
max_time = max(reaction_times)
median_time = statistics.median(reaction_times)
mean_time = statistics.mean(reaction_times)
return min_time, max_time, median_time, mean_time
def read_single_char():
if platform.system() == 'Windows':
import msvcrt
return msvcrt.getch().decode()
else:
import tty
import termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
def display_statistics(reaction_time_ms, current_session_stats, all_time_stats):
print(f"reaction time: {reaction_time_ms:.2f}ms")
print("\nThis session ==> ALL TIME:")
print(f"""
Min: {current_session_stats[0]:.2f}ms ==> {all_time_stats[0]:.2f}ms
Max: {current_session_stats[1]:.2f}ms ==> {all_time_stats[1]:.2f}ms
Med: {current_session_stats[2]:.2f}ms ==> {all_time_stats[2]:.2f}ms
Avg: {current_session_stats[3]:.2f}ms ==> {all_time_stats[3]:.2f}ms
""")
def run_reaction_time_test():
print("(press the spacebar after seeing exclamation marks, press q to quit)\n\n")
# Wait for a random time between 2 and 10 seconds
wait_time = random.randint(2, 10)
time.sleep(wait_time)
# Print the signal
print('!!!!!!!!!!!!!!!\n\n')
# Record the start time
start_time = time.time()
# Wait for the user to press the spacebar
keypress = read_single_char()
# Record the end time
end_time = time.time()
if keypress == 'q':
save_and_exit()
# Calculate the reaction time in milliseconds
reaction_time_ms = (end_time - start_time) * 1000
# Store the reaction time
current_session_reaction_times.append(reaction_time_ms)
# Calculate statistics for the current session and all time
current_session_stats = calculate_statistics(current_session_reaction_times)
all_time_stats = calculate_statistics(all_time_reaction_times + current_session_reaction_times)
# Display the statistics
display_statistics(reaction_time_ms, current_session_stats, all_time_stats)
# Set up signal handler for Control-C
signal.signal(signal.SIGINT, save_and_exit)
clear_screen()
print("> reaction times loaded.\n")
print("Press <space> after seeing exclamation marks.\nExclamation marks will show randomly between 2 and 10 seconds\n\n(press any key to begin)")
read_single_char()
clear_screen()
while True:
run_reaction_time_test()
print("-> Press space to play again (any other key quits)")
cont = read_single_char()
if cont.lower() != ' ':
break
clear_screen()
save_and_exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment