from jupyter_widget_engine import jupyter_widget_engine

def start(engine):
    
    # set text under screen
    engine.screen['text'].description = 'Press any button to begin...'
    
    # set some parameters
    engine.started = False
    engine.pos = (8,8)
    engine.f = 0

def next_frame (engine):
    
    if not engine.started:
        # record that the game has started
        engine.started = True
        # change text under screen
        engine.screen['text'].description = 'The game is afoot!'
        # set initial player position
        (x,y) = engine.pos
        engine.screen[x,y].button_style = 'info'
    else:
    
        # display frame number in top left corner
        engine.f += 1
        engine.screen[0,0].description = str(engine.f)

        # get current position
        (x,y) = engine.pos
        
        # removed player from current position
        engine.screen[x,y].button_style = ''

        # update based on controller input
        if engine.controller['up'].value:
            y -= 1
        if engine.controller['down'].value:
            y += 1
        if engine.controller['left'].value:
            x -= 1
        if engine.controller['right'].value:
            x += 1

        # put player at new position
        engine.screen[x,y].button_style = 'info'
        
        # store new position
        engine.pos = (x,y)
        
    
engine = jupyter_widget_engine(start,next_frame,L=16)