I'm a novice programmer and I'm using Pythonista to create a bad UI fighting game that utilizes buttons. My code references a ui script with buttons related to the attack or defend functions listed in the code below. My issue, is that I want the game to wait until a function in the ui is called to be able to move onto the enemy's turn which is labeled eturn. Currently it pulls up the ui takes the enemy turn instantly and then continues to layer the ui and take enemy turns in an indefinite loop. If someone knows how to make the script wait until a ui input completes the users turn while keeping it in novice knowledge level it would be greatly appreciated. Code below:
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()
Try this.
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