2016-12-22 15 views
0

私はPygameで反応時間テスターを作ろうとしていますが、試行ごとのタイマーは0を表示しています。画面が赤色のとき、下キーを押すと時間が記録されます。私は、イベントループが実際にどのように動作するかを理解するのに苦労しているので、タイマーを配置する場所を正確にはわかりません。Pygameのタイマーが同じ値を表示

import pygame, sys 
import random 
from random import randint 
import time 
from time import sleep 
from pygame.locals import * 
FPS = 30 
fpsClock = pygame.time.Clock() 
DISPLAYSURF = pygame.display.set_mode((400, 300), 0, 32) 
pygame.display.set_caption('Test') 

WHITE = (255, 255, 255) 
RED = (255, 0, 0) 
y = False 
x = 0 

while True: 


    if y is False: 
     y = True 
     DISPLAYSURF.fill(WHITE) 

     start = time.time() 
     sleep(randint(1,3)) 

    else: 
     y = False 
     DISPLAYSURF.fill(RED) 
     time.sleep(2) 



    for event in pygame.event.get(): 

     if x < 5: 
      if event.type == QUIT: 
       pygame.quit() 
       sys.exit() 
      if event.type == pygame.KEYDOWN: 
       if y is True: 
        end = time.time() 
        print(end - start) 
        x += 1 



     else: 
      pygame.quit() 
      sys.exit() 



    pygame.display.update() 
    fpsClock.tick(FPS) 
+0

time.sleep()せずにそれが動作します私はコードを実行し、画面が赤色である間にキーを押すと、画面が赤色だった時間のプリントアウトを取得します。おそらく 'sleep(randint(1,3)) 'のために、1秒から3秒の間に非常に近い秒数です。したがって、ユーザーよりも赤/白のサイクルに時間がかかるように見えます。 – roganjosh

+0

はい、まさにその意図です。私はちょうど画面の赤とキーを押す間の時間ではなく、色の間の移行をタイミングだと分かったので、私は別の場所にタイマーを置く周りに遊んだが、私は欲しい結果を得ていない。 – Perplexityy

+0

私は 'pygame'に慣れていませんが、これはスレッディングが必要だと思います。 'time.sleep()'はブロックされているので、シングルスレッドの実行では、いつも赤い画面をどれくらい長く持続させるかに時間がかかると思います。 [This](http://inventwithpython.com/blog/2013/04/22/multithreaded-python-tutorial-with-threadworms/)はかなり深みがあり、この問題のために過度のものかもしれませんが、いくつかの参考になるかもしれません。 – roganjosh

答えて

1

私はあなたが期待するが、それはミリ秒単位で赤色に時間反応をチェックして、それが正確に動作するかどうかはわからない、と私は私が何このことを理解していない

import pygame 
import random 

# --- constants --- 

FPS = 30 

WHITE = (255, 255, 255) 
RED = (255, 0, 0) 

# --- main --- 

pygame.init() 
screen = pygame.display.set_mode((400, 300)) 
pygame.display.set_caption('Test') 

repeats = 5 

testing = False 
color = WHITE 

current_time = pygame.time.get_ticks() 

# random time to start test 
start_test = current_time + random.randint(1, 3)*1000 # 1000ms = 1s 

# - mainloop - 

fps_clock = pygame.time.Clock() 

while True: 

    # - events - 

    for event in pygame.event.get(): 

     if event.type == pygame.QUIT: 
      pygame.quit() 
      exit() 
     elif event.type == pygame.KEYDOWN: 
      if testing: 
       end = pygame.time.get_ticks() 
       print(end - start, 'ms') 
       repeats -= 1 

    # - other 

    if repeats == 0: 
     pygame.quit() 
     exit() 

    current_time = pygame.time.get_ticks() 

    # is it time to start displaying red color 
    if not testing and current_time >= start_test: 
     testing = True 
     color = RED 
     start = pygame.time.get_ticks() 
     # display white color 
     stop_test = current_time + 2*1000 # 1000ms = 1s 

    # is it time to stop displaying red color 
    if testing and current_time >= stop_test: 
     testing = False 
     color = WHITE 
     # random time to start next test 
     start_test = current_time + random.randint(1, 3)*1000 # 1000ms = 1s 

    # - draws - 

    screen.fill(color) 
    pygame.display.flip() 

    fps_clock.tick(FPS) 
関連する問題