如何在 5 次尝试(点击)后让整个程序完全停止
How do I make the whole program stop completely after 5 tries (clicks)
如何让整个程序在 5 次尝试(点击)后完全停止,这与它们是否在海龟上无关紧要,但是在编写完你尝试失败后的程序应该会中断,按下时不要放黑或者红点什么都不做。这是代码:
from turtle import *
from random import *
import time
reset()
penup()
hideturtle()
speed(0)
bandymai = 0 #tries
taskai = 0 #points
info = Turtle()
info.penup()
info.goto(-180, 160)
info.color("Blue")
def gaudom(x, y):
goto(x, y)
if distance(beglys.pos()) < 10:
color('red') #hit color
global taskai
global bandymai
taskai = taskai + 1
info.clear()
info.write(taskai, font = ("Arial", 20, "bold"))
bandymai = bandymai + 1
else:
color('black') #miss color
dot(20)
bandymai = bandymai + 1
screen = getscreen()
screen.onclick( gaudom )
beglys = Turtle()
beglys.color('green')
beglys.shape('square')
for n in range(100):
x = randint(-200, 200)
y = randint(-200, 200)
beglys.penup()
beglys.clear()
beglys.goto(x, y)
time.sleep(1)
if taskai == 3:
info.write('you win!!!', font = ("Arial", 80, "bold"))
break
elif bandymai == 5:
info.write('you are out of trys', font = ("Arial", 50, "bold"))
break
在代码末尾添加 2 行。
screen.onclick(None) #Do nothing when clicking the screen.
screen.mainloop() #Must be last statement in a turtle graphics program.
与其在代码上贴创可贴,不如将其拆开并以事件兼容的方式重建它(即海龟计时器事件而不是 time.sleep()
)。我们还将 implicit 默认 turtle explicit,删除几个空操作,并通常调整样式:
from turtle import Screen, Turtle
from random import randint
bandymai = 0 # tries
taskai = 0 # points
def gaudom(x, y):
global taskai, bandymai
screen.onclick(None) # disable handler inside handler
turtle.goto(x, y)
if turtle.distance(beglys.pos()) < 10:
taskai += 1
info.clear()
info.write(taskai, font=('Arial', 20, 'bold'))
else:
turtle.color('black') # miss color
turtle.dot(20)
bandymai += 1
screen.onclick(gaudom) # reenable handler
ticks = 100
def play():
global ticks
x = randint(-200, 200)
y = randint(-200, 200)
beglys.goto(x, y)
if taskai == 3:
screen.onclick(None) # Do nothing when clicking the screen.
info.write("You win!", font=('Arial', 80, 'bold'))
return
if bandymai == 5:
screen.onclick(None) # Do nothing when clicking the screen.
info.write("You are out of trys.", font=('Arial', 50, 'bold'))
return
ticks -= 1
if ticks:
screen.ontimer(play, 1000) # 1 second (1000 milliseconds)
screen = Screen()
turtle = Turtle()
turtle.hideturtle()
turtle.speed('fastest')
turtle.penup()
info = Turtle()
info.color('blue')
info.penup()
info.goto(-180, 160)
beglys = Turtle()
beglys.color('green')
beglys.shape('square')
beglys.penup()
screen.onclick(gaudom)
play()
screen.mainloop() # Must be last statement in a turtle graphics program.
如何让整个程序在 5 次尝试(点击)后完全停止,这与它们是否在海龟上无关紧要,但是在编写完你尝试失败后的程序应该会中断,按下时不要放黑或者红点什么都不做。这是代码:
from turtle import *
from random import *
import time
reset()
penup()
hideturtle()
speed(0)
bandymai = 0 #tries
taskai = 0 #points
info = Turtle()
info.penup()
info.goto(-180, 160)
info.color("Blue")
def gaudom(x, y):
goto(x, y)
if distance(beglys.pos()) < 10:
color('red') #hit color
global taskai
global bandymai
taskai = taskai + 1
info.clear()
info.write(taskai, font = ("Arial", 20, "bold"))
bandymai = bandymai + 1
else:
color('black') #miss color
dot(20)
bandymai = bandymai + 1
screen = getscreen()
screen.onclick( gaudom )
beglys = Turtle()
beglys.color('green')
beglys.shape('square')
for n in range(100):
x = randint(-200, 200)
y = randint(-200, 200)
beglys.penup()
beglys.clear()
beglys.goto(x, y)
time.sleep(1)
if taskai == 3:
info.write('you win!!!', font = ("Arial", 80, "bold"))
break
elif bandymai == 5:
info.write('you are out of trys', font = ("Arial", 50, "bold"))
break
在代码末尾添加 2 行。
screen.onclick(None) #Do nothing when clicking the screen.
screen.mainloop() #Must be last statement in a turtle graphics program.
与其在代码上贴创可贴,不如将其拆开并以事件兼容的方式重建它(即海龟计时器事件而不是 time.sleep()
)。我们还将 implicit 默认 turtle explicit,删除几个空操作,并通常调整样式:
from turtle import Screen, Turtle
from random import randint
bandymai = 0 # tries
taskai = 0 # points
def gaudom(x, y):
global taskai, bandymai
screen.onclick(None) # disable handler inside handler
turtle.goto(x, y)
if turtle.distance(beglys.pos()) < 10:
taskai += 1
info.clear()
info.write(taskai, font=('Arial', 20, 'bold'))
else:
turtle.color('black') # miss color
turtle.dot(20)
bandymai += 1
screen.onclick(gaudom) # reenable handler
ticks = 100
def play():
global ticks
x = randint(-200, 200)
y = randint(-200, 200)
beglys.goto(x, y)
if taskai == 3:
screen.onclick(None) # Do nothing when clicking the screen.
info.write("You win!", font=('Arial', 80, 'bold'))
return
if bandymai == 5:
screen.onclick(None) # Do nothing when clicking the screen.
info.write("You are out of trys.", font=('Arial', 50, 'bold'))
return
ticks -= 1
if ticks:
screen.ontimer(play, 1000) # 1 second (1000 milliseconds)
screen = Screen()
turtle = Turtle()
turtle.hideturtle()
turtle.speed('fastest')
turtle.penup()
info = Turtle()
info.color('blue')
info.penup()
info.goto(-180, 160)
beglys = Turtle()
beglys.color('green')
beglys.shape('square')
beglys.penup()
screen.onclick(gaudom)
play()
screen.mainloop() # Must be last statement in a turtle graphics program.