I want these while loops on line 21 - 34 to alternate (one ends the next one begins) but one simply stops and the next one doesn't run.
def update(self):
mv_p = False
while not mv_p:
self.rect.x -= 5
if self.rect.left > width - 750:
mv_p = True
return mv_p
break
while mv_p:
self.rect.y += 5
if self.rect.right < width - 750:
mv_p = False
return mv_p
break
Calling return inside of a loop will break function/method execution and return value to the caller.
So, as soon as the first loop returns mv_p
, your method call is over.
If you want them to alternate (first loop, second loop, first loop, second loop, etc), you should nest them inside another loop.
def update(self):
mv_p = False
while True:
while not mv_p:
self.rect.x -= 5
if self.rect.left > width - 750:
mv_p = True
break
while mv_p:
self.rect.y += 5
if self.rect.right < width - 750:
mv_p = False
break
#need to draw on the screen here or nothing will be shown
#add condition to exit the function, adding for example a return
#inside af if, otherwise this will be an infinite loop.
If instead you just want first loop, second loop and exit no need to nest them, just remove the return
calls from your function.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.