调用的函数在我的主代码之前运行
Called function play out ahead of my main code
我有两个文件。一个带有邪恶赌徒的游戏,另一个带有加载功能,可以在文本行之间玩。我的目标是用我的加载函数替换 time.sleep() 函数。第一个文件如下所示:
import random
import time
import test
def game():
string_o = "Opponent "
string_u = "User "
input_n = ""
input_n = input('Care to try your luck?\n')
while input_n == 'yes' or input_n == 'y':
cpu = random.randint(1,6)
user = random.randint(1,6)
time.sleep(0.5)
print('\nGreat!')
time.sleep(0.2)
input_n=input("\nAre you ready?\n")
time.sleep(0.4)
print(string_o , cpu)
#If the gambler's die roll is above three he gets very happy
if cpu > 3:
print('Heh, this looks good')
time.sleep(0.2)
#...but if it's lower he gets very anxious
else:
('Oh, no!')
test.animate()
print(string_u , user)
if cpu < user:
print('Teach me, master')
else:
print('Heh, better luck next time, kid')
time.sleep()
input_n = input('\nDo you want to try again?\n')
print("Heh, didn't think so.\nPlease leave some room for thr big boys")
game()
另一个文件如下所示:
import itertools
import threading
import time
import sys
done = False
#here is the animation
def animate():
for c in itertools.cycle(['|', '/', '-', '\']):
if done:
break
sys.stdout.write('\rloading ' + c)
sys.stdout.flush()
time.sleep(0.1)
sys.stdout.write('\rDone! ')
t = threading.Thread(target=animate)
t.start()
#would like an x here instead that is defined in the other file
time.sleep(1)
done = True
问题是 animate() 函数甚至在游戏开始之前就运行完了。
我还想在我的主游戏文件中设置加载功能的时间。这可能吗?
通过将 t.start()
放在 test.py
中的任何函数之外,一旦您导入 test.py
,您就是 运行 animate
。您应该将 t.start()
放在函数中。此外,当导入 test.py
时,您的 done
标志也设置为 True
,并且总是会立即中断 animate
内的 for
循环。我认为你根本不需要这个标志。将您的 test.py
更改为:
import itertools
import threading
import time
import sys
#here is the animation
def animate():
for c in itertools.cycle(['|', '/', '-', '\']):
sys.stdout.write('\rloading ' + c)
sys.stdout.flush()
time.sleep(0.1)
sys.stdout.write('\rDone! ')
def start():
t = threading.Thread(target=animate)
t.start()
然后在您的第一个文件中,不是直接调用 test.animate()
,而是调用 test.start()
。
我有两个文件。一个带有邪恶赌徒的游戏,另一个带有加载功能,可以在文本行之间玩。我的目标是用我的加载函数替换 time.sleep() 函数。第一个文件如下所示:
import random
import time
import test
def game():
string_o = "Opponent "
string_u = "User "
input_n = ""
input_n = input('Care to try your luck?\n')
while input_n == 'yes' or input_n == 'y':
cpu = random.randint(1,6)
user = random.randint(1,6)
time.sleep(0.5)
print('\nGreat!')
time.sleep(0.2)
input_n=input("\nAre you ready?\n")
time.sleep(0.4)
print(string_o , cpu)
#If the gambler's die roll is above three he gets very happy
if cpu > 3:
print('Heh, this looks good')
time.sleep(0.2)
#...but if it's lower he gets very anxious
else:
('Oh, no!')
test.animate()
print(string_u , user)
if cpu < user:
print('Teach me, master')
else:
print('Heh, better luck next time, kid')
time.sleep()
input_n = input('\nDo you want to try again?\n')
print("Heh, didn't think so.\nPlease leave some room for thr big boys")
game()
另一个文件如下所示:
import itertools
import threading
import time
import sys
done = False
#here is the animation
def animate():
for c in itertools.cycle(['|', '/', '-', '\']):
if done:
break
sys.stdout.write('\rloading ' + c)
sys.stdout.flush()
time.sleep(0.1)
sys.stdout.write('\rDone! ')
t = threading.Thread(target=animate)
t.start()
#would like an x here instead that is defined in the other file
time.sleep(1)
done = True
问题是 animate() 函数甚至在游戏开始之前就运行完了。
我还想在我的主游戏文件中设置加载功能的时间。这可能吗?
通过将 t.start()
放在 test.py
中的任何函数之外,一旦您导入 test.py
,您就是 运行 animate
。您应该将 t.start()
放在函数中。此外,当导入 test.py
时,您的 done
标志也设置为 True
,并且总是会立即中断 animate
内的 for
循环。我认为你根本不需要这个标志。将您的 test.py
更改为:
import itertools
import threading
import time
import sys
#here is the animation
def animate():
for c in itertools.cycle(['|', '/', '-', '\']):
sys.stdout.write('\rloading ' + c)
sys.stdout.flush()
time.sleep(0.1)
sys.stdout.write('\rDone! ')
def start():
t = threading.Thread(target=animate)
t.start()
然后在您的第一个文件中,不是直接调用 test.animate()
,而是调用 test.start()
。