获取一个函数调用以等待收到通过ui的输入

2024-10-02 00:39:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一个新手程序员,我用Pythonista创建了一个糟糕的UI-filling游戏,它利用了按钮。我的代码引用了一个ui脚本,其中包含与下面代码中列出的攻击或防御功能相关的按钮。我的问题是,我想让游戏等到用户界面中的一个函数被调用后,才能转到敌人的回合,这个回合被标记为eturn。目前,它拉起用户界面采取敌人的回合立即,然后继续分层用户界面和采取敌人轮流在一个不确定的循环。如果有人知道如何让脚本等到用户界面输入完成,同时保持在新手的知识水平,这将是非常感谢。 代码如下:

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

Tags: andyoufalseifdeflefthpprint
1条回答
网友
1楼 · 发布于 2024-10-02 00:39:18

试试这个。在

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

相关问题 更多 >

    热门问题