使用Python的反应时间测试测试器

时间:2014-07-08 02:37:32

标签: python time pygame

我正在制作一个可视反应时间测试仪和记录仪。我无法弄清楚这有什么问题,请帮忙。它抛出ReactionTime未定义错误。然后在解决问题后出现其他一些错误

#Imports        

import pygame
import pyHook
import pythoncom
import logging
import sys
import win32api
import win32con
from datetime import datetime
from datetime import timedelta
from random import randint
from math import *

#Creates PyGame Window
pygame.init()

screen = pygame.display.set_mode((640, 480))

screen.fill((255, 255, 255))

#Starts Counter to Start Reaction Time Start
a= 0
for x in range(0, 100):
    a = a +1
print(datetime.now())

#Starts Reaction Time After Dot Appears
if a == 100:
    pygame.draw.circle(screen, (0, 0, 0), (320, 240), 20)
    ReactionStart= datetime.now()
    print(ReactionStart)

#Tells If Someone Pushes A Key And Stops Timer
def OnKeyboardEvent(event):
    ReactionEnd == datetime.now()
    print (ReactionEnd)

 #Makes Sure That The Program Knows a Key was pressed 
 hooks_manager = pyHook.HookManager()
 hooks_manager.KeyDown = OnKeyboardEvent
 hooks_manager.HookKeyboard()
 pythoncom.PumpMessages()

#Calculates Reaction Time
ReactionTime == ReactionEnd - ReactionStart
print (ReactionTime)


#Unknown 
pygame.display.flip()

#Makes Sure You Can exit The Test
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()

2 个答案:

答案 0 :(得分:1)

您在两个地方使用==,您几乎肯定会尝试分配值。您应该使用=

答案 1 :(得分:0)

bsois找到解决方案 - 我只显示没有所有模块的代码。

import pygame
from datetime import datetime
import time

pygame.init()
screen = pygame.display.set_mode((640, 480))

#----

for x in range(5): # repeat 5 times

    screen.fill((255, 255, 255))

    # Send `screen` on monitor - show white screen
    pygame.display.flip()

    # wait 3 seconds
    time.sleep(3)

    pygame.draw.circle(screen, (0, 0, 0), (320, 240), 20)

    # Send `screen` on monitor - show circle
    pygame.display.flip()

    reaction_start = datetime.now()

    # wait for key press
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                 reaction_end = datetime.now()
                 running = False

    # result
    print( reaction_start )
    print( reaction_end )
    print( reaction_end - reaction_start )

#----

# clear `screen`
screen.fill((255, 255, 255))

# create font
font = pygame.font.SysFont(None, 30)

# create green text
text = font.render("PRESS ANY KEY TO EXIT", 0, (0,255,0))

# get text (rectangle) size and move to center of screen
text_rect = text.get_rect(center=screen.get_rect().center)

# put text on `screen`
screen.blit(text, text_rect)

# send `screen` on monitor
pygame.display.flip()

# wait for key to close program
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        if event.type == pygame.KEYDOWN:
            pygame.quit()