【发布时间】:2020-12-22 08:47:03
【问题描述】:
如何让整个程序在 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
【问题讨论】:
标签: python onclick python-turtle