I’m an amateur programmer. I am trying to write a simple program that will measure the reaction time for a series of visual stimuli (flashes of squares) that will be used for a biology experiment. Here’s my code (beware, first time coding a graphical interface):
stimulus = pygame.Rect(100,250,100,100)
#draw on surface object
time.sleep(2) #wait for 2 seconds before it appears
screen.fill(BLACK)
pygame.draw.rect(screen,WHITE,stimulus)
pygame.display.update(stimulus)
#record time stimulus appeared
t0 = time.clock()
#clear screen ("flash" illusion)
time.sleep(0.5) #***PROBLEM***
screen.fill(BLACK)
pygame.display.update(stimulus)
while True:
for event in pygame.event.get():
if event.type == KEYDOWN:
t1 = time.clock()
print t1-t0
if event.type == QUIT:
pygame.quit()
sys.exit()
The program was working fine before I included the block with the line marked “problem”. The reaction time printed seemed reasonable. However, I want the square to disappear after a while, as though it just “flashed”. After including the time.sleep(0.5), the time printed is no longer correct. It is always 0.5xxxx or greater, no matter how fast I press. Is there any workaround?
P.S. I need it to disappear because I want to present a sequence of flashes with predetermined (not constant) pauses in between.
Thanks.
Edit
I need to achieve two things:
1. The shape must flash on the screen for 0.5 sec. 2. The program must create a timestamp (e.g. write to a list) every time the spacebar is pressed (even if it is pressed randomly twice between two flashes).
Your problem is that the computer will be doing nothing for 0.5 seconds due to the line you marked as a problem. What you need to do is make it so it is possible for the reaction to be registered while the square is still being shown. Instead of having
time.sleep(0.5), put this:This should fix your code.