-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimereaction.py
More file actions
83 lines (64 loc) · 2.03 KB
/
timereaction.py
File metadata and controls
83 lines (64 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 16 19:42:08 2014
@author: Wanderson
"""
# Timers
# Reaction Time
# This program tests your reaction in milliseconds using a
# timer. Notice the uses of start() and stop() methods.
import simpleguitk as simplegui
import random
# Global Variables
canvas_width = 200
canvas_height = 200
reaction_time = 0
# Used to see if the circle should be drawn
started = False
count = 0
# Event Handlers
def check_start():
global started, count
count += 1
# This makes sure that the circle won't be drawn for at
# least one second after starting the frame or clicking
# the restart button.
if count > 10:
# This increases the likelihood of starting and ensures
# that the game will start within a time limit.
if count / 500.0 > random.random():
started = True
reaction_timer.start()
circle_timer.stop()
def increment():
global reaction_time
reaction_time += 1
def stop_button():
if started:
reaction_timer.stop()
print("Your reaction time was", reaction_time, "milliseconds")
def draw(canvas):
if started:
canvas.draw_circle([canvas_width / 2, canvas_height / 2], 60, 2, "Red", "Red")
def restart():
global started, count, reaction_time
started = False
count = 0
reaction_time = 0
circle_timer.start()
# Frame
frame = simplegui.create_frame("Reaction Time", canvas_width, canvas_height)
# Register Event Handlers
frame.set_draw_handler(draw)
# By the way, these are labels. You can check the docs for
# more info.
label = frame.add_label("Get ready...")
frame.add_label("Press 'stop' when the red circle appears.")
frame.add_button("Stop", stop_button, 75)
frame.add_button("Restart", restart, 75)
# Note that the timers do not have the same name or purpose
circle_timer = simplegui.create_timer(100, check_start)
reaction_timer = simplegui.create_timer(1, increment)
# Start Frame and circle_timer
frame.start()
circle_timer.start()