Python中的并行while循环
我刚接触Python,也刚开始学习编程,正在为我妹妹制作一个虚拟宠物风格的游戏。
在Python中,有办法让两个while循环同时运行吗?比如:
while 1:
input_event_1 = gui.buttonbox(
msg = 'Hello, what would you like to do with your Potato Head?',
title = 'Main Screen',
choices = ('Check Stats', 'Feed', 'Exercise', 'Teach', 'Play', 'Go to Doctor', 'Sleep', 'Change Favourite Thing', 'Get New Toy', 'Quit'))
if input_event_1 == 'Check Stats':
myPotatoHead.check_p_h_stats()
elif input_event_1 == 'Feed':
myPotatoHead.feed_potato_head()
elif input_event_1 == 'Exercise':
myPotatoHead.exercise_potato_head()
elif input_event_1 == 'Teach':
myPotatoHead.teach_potato_head(myPotatoHead)
elif input_event_1 == 'Play':
myPotatoHead.play_with_toy()
elif input_event_1 == 'Sleep':
myPotatoHead.put_p_h_asleep()
elif input_event_1 == 'Go to Doctor':
myPotatoHead.doctor_check_up()
elif input_event_1 == 'Change Favourite Thing':
myPotatoHead.change_favourite_thing()
elif input_event_1 == 'Quit':
input_quit = gui.ynbox(
msg = 'Are you sure you want to quit?',
title = 'Confirm quit',
choices = ('Quit', 'Cancel'))
if input_quit == 1:
sys.exit(0)
while 1:
time.sleep(20)
myPotatoHead.hunger = str(float(myPotatoHead.hunger) + 1.0)
myPotatoHead.happiness = str(float(myPotatoHead.happiness) - 1.0)
myPotatoHead.tiredness = str(float(myPotatoHead.tiredness) + 1.0)
如果不行,有没有办法把这两个循环合成一个呢?我希望第二个循环里的内容每20秒发生一次,而第一个循环里的内容则是一直在进行的。
谢谢大家的帮助!
7 个回答
3
把其中一个放到一个函数里,threading.Thread 类支持一个叫做 target 的属性:
import threading
threading.Thread(target=yourFunc).start()
这会让你的函数 yourFunc() 在后台运行。
3
你应该使用状态机来处理这个问题(可以参考Apress的pygame书籍 - 下载链接在这里: http://apress.com/book/downloadfile/3765),具体内容在第七章。
下面是一个简化版的状态机:
def do_play(pet, time_passed):
pet.happiness += time_pass*4.0
def do_feed(pet, time_passed):
pet.hunger -= time_passed*4.0
def do_sleep(pet, time_passed):
pet.tiredness += time_passed*4.0
if pet.tiredness <= 0:
return 'Waiting'
def do_waiting(pet, time_passed):
pass
def do_howl(pet, time_passed):
print 'Hoooowl'
def do_beg(pet, time_passed):
print "I'm bored!"
def do_dead(pet, time_passed):
print '...'
STATE_TO_FUNC = dict(Waiting=do_waiting,
Sleeping=do_sleep,
Feeding=do_feed,
Playing=do_play,
Howling=do_howl,
Begging=do_beg,
Dead=do_dead
)
class Pet:
def __init__(self):
self.state = 'Waiting'
self.hunger = 1.0
self.tiredness = 1.0
self.happiness = 1.0
def process(self, time_passed):
self.hunger +=1*time_passed
self.tiredness +=1*time_passed
self.happiness -= 1*time_passed
func = STATE_TO_FUNC[self.state]
new_state = func(self, time_passed)
if new_state is not None:
self.set_state(new_state)
if self.hunger >10:
self.set_state('Dead')
elif self.hunger > 5 and not (self.state == 'Feeding'):
self.set_state('Howling')
elif self.tiredness > 5:
self.set_state('Sleeping')
elif self.happiness < 0 and not (self.state == 'Playing'):
self.set_state('Begging')
def set_state(self,state):
if not self.state == 'Dead':
self.state = state
from msvcrt import getch
import time
pet = Pet()
while True:
time0 = time.time()
cmd = getch() # what command?
pet.process(time.time()-time0)
if cmd == 'a':
pet.set_state('Feeding')
if cmd == 's':
pet.set_state('Sleeping')
if cmd == 'd':
pet.set_state('Playing')
3
你可以看看这个链接:Threading.Timer。
这里有一个链接:代码示例,教你如何每5秒运行一次一个函数。
import thread
import threading
class Operation(threading._Timer):
def __init__(self, *args, **kwargs):
threading._Timer.__init__(self, *args, **kwargs)
self.setDaemon(True)
def run(self):
while True:
self.finished.clear()
self.finished.wait(self.interval)
if not self.finished.isSet():
self.function(*self.args, **self.kwargs)
else:
return
self.finished.set()
class Manager(object):
ops = []
def add_operation(self, operation, interval, args=[], kwargs={}):
op = Operation(interval, operation, args, kwargs)
self.ops.append(op)
thread.start_new_thread(op.run, ())
def stop(self):
for op in self.ops:
op.cancel()
self._event.set()
if __name__ == '__main__':
# Print "Hello World!" every 5 seconds
import time
def hello():
print "Hello World!"
timer = Manager()
timer.add_operation(hello, 5)
while True:
time.sleep(.1)