Пример #1
0
def runtrial(trial, screen,score, sID,outfile = None,nt = 0, noocclude = False):
    if opts['PrintTrial'] == True: trial.pr()
    its = 0
    leads = trial.leadsteps
    prebounce = 0
    running = True
    catch = None
    failarea = pg.Rect((0,0),(trial.endpt[0]-15,trial.dim[1]))
    paddley = None
    toffset = (int((screen.get_size()[0]-trial.dim[0])/2),100 + int((screen.get_size()[1]-trial.dim[1]-100)/2))
    table = PhysicsTable(trial.dim, ball_rad = ballsize, badzone = failarea, active = False)
    
    table.addPaddle((trial.endpt[0]-ballsize,trial.dim[1]/2),paddlelen,'v',True)
    #targx = 50 + ballsize

    clock = pg.time.Clock()

    # Let the subject get set up
    running = True
    while running:
        for event in pg.event.get():
            if event.type == QUIT: safequit(outfile)
            elif event.type == KEYDOWN and quitevent(): safequit(outfile)
            elif event.type == MOUSEBUTTONDOWN: running = False
            
        if pg.mouse.get_focused():
            mp = pg.mouse.get_pos()
            mpoff = (mp[0] - toffset[0], mp[1] - toffset[1])
            r = table.step(1/frrate,True,mpoff,xtarg = trial.endpt[0])
        else:
            r = table.step(1/frrate,False,None,xtarg = trial.endpt[0])

        clock.tick(frrate)
        pg.display.set_caption('FPS: ' + str(clock.get_fps()))

        screen.fill(pg.Color('White'))
        prtexttop("Click to shoot the ball",screen,toffset[1],toffset[0],toffset[0]+trial.dim[0],score)
        tscreen = table.draw()
        screen.blit(tscreen,toffset)
        pg.display.flip()

    # Add the ball and start the trial
    table.addBall(trial.initpos,trial.vel)

    running = True
    table.sincecol = 0
    while running:
        for event in pg.event.get():
            if event.type == QUIT: safequit(outfile)
            elif event.type == KEYDOWN and quitevent(): safequit(outfile)

        if pg.mouse.get_focused():
            mp = pg.mouse.get_pos()
            mpoff = (mp[0] - toffset[0], mp[1] - toffset[1])
            r = table.step(1/frrate,True,mpoff,xtarg = trial.endpt[0])
        else:
            r = table.step(1/frrate,False,None,xtarg = trial.endpt[0])
        its += 1
        if its == leads:
            prebounce = table.ball.bounces
            table.activate()

        clock.tick(frrate)
        pg.display.set_caption('FPS: ' + str(clock.get_fps()))

        screen.fill(pg.Color('White'))
        prtexttop(None,screen,toffset[1],toffset[0],toffset[0]+trial.dim[0],score)
        tscreen = table.draw()
        if its > leads and noocclude == False: pg.draw.rect(tscreen,pg.Color('Grey'),Rect((trial.endpt[0]-ballsize+3,1),(trial.dim[0]-trial.endpt[0]+ballsize-4,trial.dim[1]-2)))
        screen.blit(tscreen,toffset)
        pg.display.flip()

        catch = table.catches()
        if catch == True or catch == False:
            running = False
            paddley = table.padend
            if paddley is None:
                print table.paddle.getcenter()[1]
                print table.ball.getpos()
                raise Exception('DID NOT CAPTURE PADDLE LOCATION')

    # Show results of trial
    if catch == True:
        fintxt = 'Nice catch! Click for the next trial'
        if score is not None: score += 1
    else: fintxt = 'You missed. Click for the next trial'
    running = True
    while running:
        for event in pg.event.get():
            if event.type == QUIT: safequit(outfile)
            elif event.type == KEYDOWN and quitevent(): safequit(outfile)
            elif event.type == MOUSEBUTTONDOWN: running = False

        clock.tick(frrate)
        pg.display.set_caption('FPS: ' + str(clock.get_fps()))

        screen.fill(pg.Color('White'))
        prtexttop(fintxt,screen,toffset[1],toffset[0],toffset[0]+trial.dim[0],score)
        tscreen = table.draw()
        screen.blit(tscreen,toffset)
        pg.display.flip()

    pixeldiff = (paddley - trial.endpt[1])
    pixelsoff = abs(pixeldiff)

    # Write to output file
    if outfile:
        outfile.write(sID+','+str(nt)+','+str(trial.n)+','+trial.dimtxt+','+str(trial.dist)+','+str(trial.bounces)+','+str(trial.v)+','+str(trial.runup)+',')
        outfile.write(str(catch)+','+str(trial.endpt[0])+','+str(trial.endpt[1])+','+str(paddley)+','+str(pixeldiff)+','+str(pixelsoff)+','+str(prebounce)+'\n')
    
    return catch, pixelsoff