获取函数调用以等待直到通过ui输入

时间:2017-03-21 17:07:38

标签: python pythonista

我是一名新手程序员,我正在使用Pythonista创建一个利用按钮的糟糕的UI格斗游戏。我的代码引用了一个ui脚本,其中包含与下面代码中列出的攻击或防御功能相关的按钮。我的问题是,我希望游戏等到ui中的某个函数被调用才能移动到被标记为eturn的敌人的转弯处。目前它拉起ui使敌人立即转向然后继续分层ui并在无限循环中进行敌人转弯。如果有人知道如何让脚本等到ui输入完成后用户转动同时保持新手知识水平,那将非常感激。 代码如下:

import ui
import random
import time
hp = 100
pots = 3
atk = 7
guard = False

#enemy values
ehp = random.randint(50, 75)
eatk = random.randint(8, 11)
eguard = False

def menu():
    global guard
    v = ui.load_view('option menu')
    v.present('sheet')

def attack(sender):
    v.close()
    if eguard == False:
        ehp -= atk
        print('You slash at the enemy and deal %r damage' % atk)
        print('\nIt has %r hp left' % ehp)
        return ehp
    elif eguard == True:
        dmg = (atk - 3)
        ehp -= dmg
        print('You slash at the enemy and deal %r damage' % dmg)
        print('\nIt has %r hp left' % ehp)
        return ehp

def defend(sender):
    v.close()
    print('You put up your guard')
    guard = True
    return guard

def heal(sender):
    global hp, pots
    v.close()
    if pots > 0:
        pots -= 1
        hp += 25
        if hp > 100:
            hp = 100
        print('You use a health potion and gain 25hp, you have %r potions and %r hp left' % (pots, hp))
    elif pots == 0:
        print('You are out of potions!')

def eturn():
    global eguard, hp, ehp
    choice = random.randint(1, 3)
    eguard = False
    if choice == 1:
        if guard == True:
            dmg = eatk - 3
            hp -= dmg
            print('The creature swipes at you and deals %r damage, you have %r hp left' % (dmg, hp))
        elif guard == False:
            hp -= eatk
            print('The creature swipes at you and deals %r damage, you have %r hp left' % (eatk, hp))
    elif choice == 2:
        print('The creature defends itself')
        eguard = True
    elif choice == 3:
        ehp += 10
        if ehp > 75:
            ehp = 75
        print('The creature heals itself for 10 hp')

def turn():
    menu()

def game():
    print('instructions')
    turn()
    eturn()

game()

1 个答案:

答案 0 :(得分:0)

试试这个。

def menu():
    global guard
    v = ui.load_view('option menu')
    v.present('sheet')
    v.wait_modal()   # Waits until the dialog is off the screen
    v.close()        # Properly cleans up the dialog