0

我是一个业余程序员。我正在尝试编写一个简单的程序,该程序将测量将用于生物学实验的一系列视觉刺激(方块闪烁)的反应时间。这是我的代码(注意,第一次编写图形界面):

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()

在我将块包含在标记为“问题”的行之前,该程序运行良好。打印的反应时间似乎是合理的。但是,我希望广场在一段时间后消失,就好像它只是“闪过”一样。包含 time.sleep(0.5) 后,打印的时间不再正确。无论我按多快,它始终为 0.5xxxx 或更大。有什么解决方法吗?

PS我需要它消失,因为我想呈现一系列闪烁,其间有预定的(非恒定的)停顿。

谢谢。

编辑

我需要实现两件事: 1. 形状必须在屏幕上闪烁 0.5 秒。2. 程序必须在每次按下空格键时创建一个时间戳(例如写入一个列表)(即使在两次闪烁之间随机按下两次)。

4

2 回答 2

1

您的问题是由于您标记为问题的行,计算机将在 0.5 秒内无操作。你需要做的就是让它在方块仍然显示的同时记录反应。而不是有time.sleep(0.5),把这个:

while time.clock()-t0<0.5:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            t1 = time.clock()
            print t1-t0

这应该可以修复您的代码。

于 2012-11-17T15:27:06.787 回答
1

使用pygame.time.get_ticks()which 给你毫秒后过去pygame.init

编辑:

如果您在get_ticks首次显示形状时保存该值,那么每次按下键时,将当前的get_ticks.

for press in press_times:
    print "milliseconds: {}".format(press - time_start)

毫秒意味着 500 相当于 0.5 秒。

于 2012-11-17T20:35:08.653 回答