Search code examples
pythonvizard

Adding a click function to a button in Python


I am very new to Python. I'm creating a simple 3D world using Vizard 4.0 and I want to add a stopwatch to the screen. I dont want an actual 3D stopwatch, just a simple text box in the corner of the screen which updates as the stopwatch counts. Heres what I ave so far and it doesnt work, any suggestions or help would be very appreciated!

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)

Solution

  • Cracked it :)

    #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() )