Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pyglet动画:在特定时间后停止并加载新参数_Python_Animation_Pyglet - Fatal编程技术网

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

Python Pyglet动画:在特定时间后停止并加载新参数,python,animation,pyglet,Python,Animation,Pyglet,我正在编写一个python脚本,该脚本从文本文件加载一些值,并将它们用作动画的参数。我想显示一系列动画,它们之间有一个菜单,等待用户交互 我想知道如何在确定的时间内停止动画,然后为第二个动画加载新参数,但不关闭显示第一个动画的窗口 我使用的代码如下所示: ## Create pyglet window # Get screens screens = pyglet.window.get_platform().get_default_display().get_screens() config =

我正在编写一个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()

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

  • 例如,在经过时间循环内部时使用

  • 使
    main
    中的“加载配置”代码部分成为一个单独的函数(以生成更干净的代码),并在每次while循环退出时从
    update\u frames(dt)
    调用它


使用while循环时,它不起作用,因此我改为使用if语句。谢谢你的回答!