我是Python的新手,并使用Zelle的图形来创建游戏。我需要下面的两个while循环同时运行,但是我遇到了困难。我尝试嵌套while循环,但是马和平民只有在点击鼠标时才会移动,这是我不想要的。我想要的是马匹和平民总是在移动,公主只有在点击鼠标时才会移动,并且当她拯救了10名平民时,通过“游戏结束”来停止游戏。
# animation loop.
while True==True:
for horse in horseList:
if horse.leg.getX() > -187:
horse.move( -1, 20 )
else:
horse.move( 5, 28 )
for civilian in civiliansList:
if civilian.getX() < 800:
civilian.move( 20, 0 )
else:
civilian.move( -100, 0 )
while civiliansSaved != 10:
mouse = win.getMouse()
princess.move( mouse, civilianCounter)
civilianCounter = princess.move( mouse, civilianCounter)
# move is a method that will return an updated civilianCounter ( it is initially 0 and defined outside of the while loop ), depending on whether princess runs into civilians
else:
print( "Game over" )
win.getMouse()
win.close()
答案 0 :(得分:2)
在动画循环中使用checkMouse()
代替getMouse()
。
我认为很简单。
while civiliansSaved < 11:
for horse in horseList:
if horse.leg.getX() > -187
horse.move( -1, 20 )
else:
horse.move( 5, 28 )
for civilian in civiliansList:
if civilian.getX() < 800:
civilian.move( 20, 0 )
else:
civilian.move( -100, 0 )
mouse = win.checkMouse()
if mouse:
princess.move( mouse, civilianCounter)
civilianCounter = princess.move( mouse, civilianCounter)
print( "Game over" )
win.getMouse()
win.close()
DOCO:
checkMouse()与getMouse类似,但不会暂停用户点击。 返回单击鼠标的最新点,如果是,则返回None 自上次调用checkMouse或之后未单击的窗口 getMouse。这对于控制简单的动画循环特别有用。
答案 1 :(得分:0)
这是一个应该做你想做的事情而不需要并行处理的例子(这在python中很棘手):
while True: # you don't need to write True==True
for horse in horseList:
if horse.leg.getX() > -187:
horse.move( -1, 20 )
else:
horse.move( 5, 28 )
for civilian in civiliansList:
if civilian.getX() < 800:
civilian.move( 20, 0 )
else:
civilian.move( -100, 0 )
mouse = win.getMouse()
princess.move( mouse, civilianCounter)
civilianCounter = princess.move( mouse, civilianCounter)
if civiliansSaved >= 10: # check to see if 10 or more have been saved
break
print( "Game over" )
win.getMouse()
win.close()
你想要做的是保持游戏运行,直到civilSaved计数器至少为10.但你不能在主游戏循环的单独循环中这样做,所以更没有意义停止游戏直到计数至少为10.这个if语句可以包含在你的主游戏循环中。
答案 2 :(得分:0)
Zelle's graphics is not an event driven graphics tool, which I think is what you are looking for. Maybe consider switching to tkinter
which is part of the Python standard library. You can use a callback function to handle mouse events so that your app is free to do other things while it is waiting to process a mouse event.
tkinter
has an after()
feature which allows you to call a function after a specified period of time. You can create separate functions for civilian movement
and horse movement
and call these functions after a specified period of time (say 50 ms). This effectively simulates running the functions in parallel and allows horses and civilians to move at the same time, at least from the user's perspective. You would need another call to after()
within the movement functions so that they are continuously called.
self.root.after(50, self.process_horse_movement)
self.root.after(50, self.process_civilian_movement)
Then at the end of each movement function:
def process_horse_movement():
...
self.root.after(50, self.process_horse_movement)
def process_civilian_movement():
...
self.root.after(50, self.process_civilian_movement)
Also, a minor point. Use while True:
instead of while True == True:
.