1

我是一名新手程序员,我正在使用 Pythonista 创建一个使用按钮的糟糕的 UI 格斗游戏。我的代码引用了一个带有与下面代码中列出的攻击或防御功能相关的按钮的 ui 脚本。我的问题是,我希望游戏等到调用 ui 中的一个函数才能移动到标记为 eturn 的敌人的回合。目前它会拉起 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()
4

1 回答 1

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
于 2017-03-23T15:04:52.723 回答