Pyglet动画:在一定时间后停止并加载新参数

时间:2014-05-27 13:28:32

标签: python animation pyglet

我正在研究一个python脚本,它从文本文件加载一些值并将它们用作动画的参数。我希望在它们之间显示一系列动画,等待用户交互。

我想知道如何在既定时间内停止动画,然后为第二个动画加载新参数,但不关闭显示第一个动画的窗口。

我使用的代码如下:

## Create pyglet window
# Get screens
screens = pyglet.window.get_platform().get_default_display().get_screens()

config = pyglet.gl.Config(stencil_size=8)

# Create window
myWindow = pyglet.window.Window(config=config,fullscreen=True,screen=screens[0])


## Functions definition

def ConfigFileToStruct(configfilename):
#Loads the configuration file to a pydic

@myWindow.event
def on_mouse_press(x, y, button, modifiers):

@myWindow.event
def on_mouse_release(x, y, button, modifiers):

@myWindow.event
def on_close():

def drawCircle(radius, circle_color, x, y):

def drawGrating(x, y, fill_color, orientation, mylambda, duty_cycle):

def update_frames(dt):
    global x1, x2, apertRad_pix, speed1, speed2, timeStart, mylambda1, mylambda2

    timeNow = time.clock()
    motion_cycle1 = mylambda1/(math.cos(math.radians(orientation1)))
    motion_cycle2 = mylambda2/(math.cos(math.radians(orientation2)))


    initialpos1 = myWindow.width/2 - apertRad_pix - mylambda1/2
    initialpos2 = myWindow.width/2 - apertRad_pix + mylambda2/2

    # update position of grating 1:
    x1 = initialpos1 + math.fmod(speed1*(timeNow-timeStart), motion_cycle1)

    # update position of grating 2:
    x2 = initialpos2 + math.fmod(speed2*(timeNow-timeStart), motion_cycle2)


@myWindow.event
def on_draw():

    #Define variables
    Red = (1.0, 0.88, 0.88, 0.5)
    Cyan = (0.88, 1.0, 1.0, 0.5)

    glEnable(GL_BLEND)
    pyglet.gl.glClearColor( 0.6, 0.6, 0.6, 0 )
    myWindow.clear()

    # Enable stencil
    glClearStencil(0x0)
    glEnable(GL_STENCIL_TEST) 

    #define the region where the stencil is 1
    glClear(GL_STENCIL_BUFFER_BIT)
    glStencilFunc(GL_ALWAYS, 0x1, 0x1)
    glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE)

    # draw Fixation point

    drawCircle(apertRad_pix/15, [0, 0, 0],       x0_pix, y0_pix)
    drawCircle(apertRad_pix/80, [0.9, 0.9, 0.9], x0_pix, y0_pix)

    stereo1 = -1

    drawGrating(x1 + stereo1, y1, Cyan, orientation1, mylambda1, duty_cycle1)
    drawGrating(x1 - stereo1, y1, Red, orientation1, mylambda1, duty_cycle1)

    glBlendFunc(GL_ONE, GL_ZERO)


## Main
if __name__ == "__main__":
    namefile = "cfgfile.txt"

    # Load configuration parameters
    config = ConfigFileToStruct(namefile)
    if not config:
        print("CONFIG FILE EMPTY")
    else:
        print("CONFIG FILE LOADED")

    # VARIABLES       
    apertRad_pix = 400

    ## GRATING PARAMETERS
    #grating1
    x1 = x0_pix - apertRad_pix
    y1 = y0_pix - apertRad_pix

    mylambda1 = config["gratings1"]["wavelength"]
    duty_cycle1 = config["gratings1"]["duty_cycle"]
    orientation1 = config["gratings1"]["orientation"]
    speed_pix = config["gratings1"]["speed"]
    speed1 = speed_pix / math.cos(math.radians(orientation1))       

    ## Run stimulus   
    timeStart = time.clock()    

    framerate = 1/60.0


    pyglet.clock.schedule_interval(update_frames,framerate)


    pyglet.app.run()

1 个答案:

答案 0 :(得分:0)

单步执行方案文本文件参数实际上应该是动画更新功能的一部分:

  • 例如使用while elapsed_time < current_sequence_time_limit循环 update_frames(dt)

  • 进行&#34;加载配置&#34;代码部分来自main一个单独的函数(以创建一个更干净的代码),并在每次while循环退出时从update_frames(dt)调用它