0

我对 Python 很陌生。我正在使用 Vizard 4.0 创建一个简单的 3D 世界,我想在屏幕上添加一个秒表。我不想要一个真正的 3D 秒表,只是屏幕一角的一个简单文本框,它会随着秒表计数而更新。这是我到目前为止所拥有的,但它不起作用,任何建议或帮助将不胜感激!

def mytimer(num):
    time = 'Elapsed time:',viz.elapsed(),'seconds'
    print time
    timer_update = viz.addText('Time:', viz.SCREEN)
    timer_update.color(viz.GREEN)
    timer_update.fontSize(42)

    viz.callback(viz.TIMER_EVENT,mytimer)
    viz.starttimer(0,1, viz.FOREVER)
4

1 回答 1

0

破解它:)

#Add text fields to a dictionary.
text_dict = {}
for kind in ['score','instructions','time' ]:
    text = viz.addText('', viz.SCREEN )
    text.setScale( .5,.5)
    text.alignment( viz.TEXT_CENTER_BASE )
    text.alpha( 1 )
    text_dict[ kind ] = text
text_dict['score'].setPosition( .1,.9 )
text_dict['instructions'].setPosition( .5,.5 )
text_dict['time'].setPosition( .1,.85 )


def game_timer_task():
    #Grab the text field for
    #time.
    text = text_dict[ 'time' ]
    time = 0
    text.message( 'Time: 0' )
    #Loop through as long as time
    #is under a certain number of
    #seconds.
    while time < 10:
        yield viztask.waitTime( 1 )
        time += 1
        text.message( 'Time: ' + str( time ) )


def game():
    time_passing = viztask.waitTask( game_timer_task() )
    data = viz.Data()

    yield viztask.waitAny( [time_passing], data )


    if data.condition == time_passing:
        game_over = text.message( 'Game Over!' )
        game_over.setPosition( .1,.85 )

def main_sequence():
    yield game()


viztask.schedule( main_sequence() )   
于 2013-04-15T16:12:39.727 回答