예제 #1
0
def process_user_command(game_tree, target_tree, curr, pic):
    '''Read and process one command from the user, modifying BTNode game_tree
    and current BTNode curr as appropriate and redrawing the new game_tree and
    BTNode target_tree on Picture pic.  Return the new value of curr.'''

    d = {'Left': 'l', 'Right': 'r', 'Up': 'u', 'a': 'L', 's': 'R', 'q': 'q'}
    cmd = d.get(KD.moving_by_keys().key, 'm')

    # Only listen to valid commands.
    if len(cmd) != 1 or cmd[0] not in 'qulrLR':
        return curr

    # Erase the old tree and redraw target_tree halfway across the window.
    media.add_rect_filled(pic, 0, 0, WIDTH, HEIGHT, media.white)
    draw(pic, target_tree.root, 0, WIDTH / 2, curr)

    # Process user commands.
    if cmd == 'q':
        media.close(pic)
        sys.exit(0)
    elif cmd == 'u' and curr != None and curr.parent != None:
        curr = curr.parent
    elif cmd == 'l' and curr.left != None:
        curr = curr.left
    elif cmd == 'r' and curr.right != None:
        curr = curr.right
    elif cmd == 'L' and curr.right != None:
        curr = bst.rotate_left(game_tree, curr)
    elif cmd == 'R' and curr.left != None:
        curr = bst.rotate_right(game_tree, curr)

    # The parent attribute of the nodes of the new tree must be corrected.
    # If curr is at the top, a rotation may have moved it there. Set the
    # game_tree root to curr if that happened.

    if curr.parent == None:
        game_tree.root = curr

    # Draw the new game tree.
    draw(pic, game_tree.root, 0, 0, curr)
    media.update(pic)

    return curr
예제 #2
0
파일: tree_rot.py 프로젝트: idcxb/python
def process_user_command(game_tree, target_tree, curr, pic):
    '''Read and process one command from the user, modifying BTNode game_tree
    and current BTNode curr as appropriate and redrawing the new game_tree and
    BTNode target_tree on Picture pic.  Return the new value of curr.'''

    cmd = raw_input(prompt)
    
    # Only listen to valid commands.
    while cmd not in 'shqulrLR':
        cmd = raw_input(prompt)
    
    # Erase the old tree and redraw target_tree halfway across the window.
    media.add_rect_filled(pic, 0, 0, WIDTH, HEIGHT, media.white)
    draw(pic, target_tree.root, 0, WIDTH / 2, curr)

    # Process user commands.
    if cmd == 'q':
        media.close(pic)
        return None
    elif cmd == 'h':
        solver.help_solve(game_tree, target_tree)
    elif cmd== 's':
        while game_tree != target_tree:
            solver.help_solve(game_tree, target_tree)
    elif cmd == 'u' and curr != None and curr.parent != None:
        curr = curr.parent
    elif cmd == 'l' and curr.left != None:
        curr = curr.left
    elif cmd == 'r' and curr.right != None:
        curr = curr.right
    elif cmd == 'L' and curr.right != None:
        curr = bst.rotate_left(game_tree, curr)
    elif cmd == 'R' and curr.left != None:
        curr = bst.rotate_right(game_tree, curr)
        
    # Draw the new game tree.
    draw(pic, game_tree.root, 0, 0, curr)
    media.update(pic)
    
    return curr
예제 #3
0
    When the level has been solved, push y to contine and n to quit.'''
    scoring.print_highscore_list(highscore.score_list)
    raw_input("enter a key to begin: ")

    tree_size = 2
    cont = 'y'
    while cont == 'y':
        start = time.time()
        play_game(tree_size)
        end = time.time() - start
        highscore.enter_score(end)
        if tree_size < 8:
            print ("Congrats!  You finished tree size " + \
            str(tree_size) + ".  Continue (y/n)? ")
            cont = KD.moving_by_keys().key
            while cont != 'y' and cont != 'n':
                cont = KD.moving_by_keys().key
            #clear()
        else:
            #clear()
            print '''Congrats! You finished level 8 and have completed the game
            and scored a vlue of %i.''' % (int(highscore.score))
            highscore.insert_score([int(highscore.score), name])
            highscore.write_to_file()
            cont = 'n'
        tree_size += 1
    scoring.print_highscore_list(highscore.score_list)
    raw_input("Press enter to exit.")
    media.close(pic)

예제 #4
0
            break
    media.clear(color=(0, 0, 0))
    text_y = 40
    text_h = 20
    text_x = 10
    media.line(x=0, y=text_y, w=100, h=0, color=(128, 0, 0))
    media.line(x=0, y=text_y - text_h, w=100, h=0, color=(0, 0, 128))
    media.line(x=text_x, y=0, w=0, h=60, color=(0, 128, 0))
    media.line(x=700, y=500, w=100, h=100, color=(128, 128, 0))
    media.vector_text(
        '''0.123#456$789%!                        \n'''
        '''abc'def,ghijklmnopqrstuvwxyz           \n'''
        '''ABC"DEF_GHIJKLMNOPQRSTUVWXYZ           \n'''
        '''D&D;   a-A+(B/c)*  p|q                 \n'''
        '''a: b=c<d A>f?  [email protected]           \n'''
        '''\\n x[i]^2 `${x}` ~30 {a: 3}            \n'''
        '''                                       \n'''
        '''The quick brown fox jumps over the lazy\n'''
        '''dog.                                   \n'''
        '''                                       \n'''
        '''                                       \n'''
        '''                                       \n'''
        '''                                       \n'''
        '''                                      .\n''',
        x=text_x,
        y=text_y,
        h=text_h)
    media.display()
    time.sleep(0.05)
media.close()