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

2024-09-27 00:13:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个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()

Tags: configdef动画mathwindowpygletdutyx1
1条回答
网友
1楼 · 发布于 2024-09-27 00:13:59

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

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

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

相关问题 更多 >

    热门问题