如何使用计数器重新启动while循环



我想做一个计数器,当它像游戏一样到达0时倒计时并重新启动循环,并使mr_krabs2在他开始时的位置生成。

# Graphing frame 3.
win3 = GraphWin('Frame.3', 640, 360)
mr_krabs2 = Image(Point(600, 315), 
r"UsersOneDriveDesktopMr.Krabs2.png")
win3.setBackground("lightblue")      
# Drawing the inserted images.
mr_krabs2.draw(win3)   
# X and Y Variables.
x = 0.0
y = 0.0   
while True:
key = win3.getKey()    
if key == "w":
mr_krabs2.move(0,-10)
y = y + 10
print("nx =",x,"y =",y,"n")   
if key == "s":
mr_krabs2.move(0,10)
y = y - 10
print("nx =",x,"y =",y,"n")    
if key == "a":
mr_krabs2.move(-10,0)
x = x + 10
print("nx =",x,"y =",y,"n") 
if key == "d":
mr_krabs2.move(10,0)
x = x - 10
print("nx =",x,"y =",y,"n")
# Hitbox for the destination.
if (x < (59) and
x > (52) and
y < (4) and
y > (-6)):
time.sleep(5)
win3.close()
break

感谢gerda die gandalfziega

from time import perf_counter
#Graphing frame 3
win3 = GraphWin('Frame.3', 640, 360)
mr_krabs2 = Image(Point(600, 315), 
r"UsersOneDriveDesktopMr.Krabs2.png")
win3.setBackground("lightblue")      
**Drawing the inserted images.**
mr_krabs2.draw(win3)   
# X and Y Variables.
x = 0.0
y = 0.0
start = perf_counter() # Get the current time this line is executed
while True:
key = win3.getKey()  

if perf_counter()-start >= 60:
mr_krabs2.undraw()
mr_krabs2 = Image(Point(600, 315), r"UsersleilaOneDriveDesktopMr.Krabs2.png")
mr_krabs2.draw(win3)
start = start + 60 **Adds 60 seconds to reset the krab.**

if key == "w":
mr_krabs2.move(0,-10)
y = y + 10
print("nx =",x,"y =",y,"n")
if key == "s":
mr_krabs2.move(0,10)
y = y - 10
print("nx =",x,"y =",y,"n")
if key == "a":
mr_krabs2.move(-10,0)
x = x + 10
print("nx =",x,"y =",y,"n")
if key == "d":
mr_krabs2.move(10,0)
x = x - 10
print("nx =",x,"y =",y,"n")

i用将运行100次的for循环替换为true,您可以将其更改为

# Graphing frame 3.
win3 = GraphWin('Frame.3', 640, 360)
mr_krabs2 = Image(Point(600, 315), 
r"UsersOneDriveDesktopMr.Krabs2.png")
win3.setBackground("lightblue")      
# Drawing the inserted images.
mr_krabs2.draw(win3)   
# X and Y Variables.
x = 0.0
y = 0.0   
for i in range(100):
key = win3.getKey()    
if key == "w":
mr_krabs2.move(0,-10)
y = y + 10
print("nx =",x,"y =",y,"n")   
if key == "s":
mr_krabs2.move(0,10)
y = y - 10
print("nx =",x,"y =",y,"n")    
if key == "a":
mr_krabs2.move(-10,0)
x = x + 10
print("nx =",x,"y =",y,"n") 
if key == "d":
mr_krabs2.move(10,0)
x = x - 10
print("nx =",x,"y =",y,"n")
# Hitbox for the destination.
if (x < (59) and
x > (52) and
y < (4) and
y > (-6)):
time.sleep(5)
win3.close()
break

以下是我的操作方法,这样循环在 60秒后停止

from time import perf_counter
# Graphing frame 3.
win3 = GraphWin('Frame.3', 640, 360)
mr_krabs2 = Image(Point(600, 315), 
r"UsersOneDriveDesktopMr.Krabs2.png")
win3.setBackground("lightblue")      
# Drawing the inserted images.
mr_krabs2.draw(win3)   
# X and Y Variables.
x = 0.0
y = 0.0   
start = perf_counter() # get the current time this line is executed
while perf_counter()-start <60: # This line check if the time difference between the current time and the starting time is smaller then 60 seconds
key = win3.getKey()    
if key == "w":
mr_krabs2.move(0,-10)
y = y + 10
print("nx =",x,"y =",y,"n")   
if key == "s":
mr_krabs2.move(0,10)
y = y - 10
print("nx =",x,"y =",y,"n")    
if key == "a":
mr_krabs2.move(-10,0)
x = x + 10
print("nx =",x,"y =",y,"n") 
if key == "d":
mr_krabs2.move(10,0)
x = x - 10
print("nx =",x,"y =",y,"n")
# Hitbox for the destination.
if (x < (59) and
x > (52) and
y < (4) and
y > (-6)):
time.sleep(5)
win3.close()
break

第2版:

from time import perf_counter
# Graphing frame 3.
win3 = GraphWin('Frame.3', 640, 360)
mr_krabs2 = Image(Point(600, 315), 
r"UsersOneDriveDesktopMr.Krabs2.png")
win3.setBackground("lightblue")      
# Drawing the inserted images.
mr_krabs2.draw(win3)   
# X and Y Variables.
x = 0.0
y = 0.0   
start = perf_counter() # get the current time this line is executed
while  # This line check if the time difference between the current time and the starting time is smaller then 60 seconds

if perf_counter()-start >=60:
mr_krabs2 = Image(Point(600, 315), 
win3.setBackground("lightblue")      
# Drawing the inserted images.
mr_krabs2.draw(win3)  

key = win3.getKey()    
if key == "w":
mr_krabs2.move(0,-10)
y = y + 10
print("nx =",x,"y =",y,"n")   
if key == "s":
mr_krabs2.move(0,10)
y = y - 10
print("nx =",x,"y =",y,"n")    
if key == "a":
mr_krabs2.move(-10,0)
x = x + 10
print("nx =",x,"y =",y,"n") 
if key == "d":
mr_krabs2.move(10,0)
x = x - 10
print("nx =",x,"y =",y,"n")
# Hitbox for the destination.
if (x < (59) and
x > (52) and
y < (4) and
y > (-6)):
time.sleep(5)
win3.close()
break

最新更新