Python怎么用turtle生成一个随机点,当点击它的时候继续生成下一个。(Python对函数理解不深想用onclick()发现不好使)
import random
import turtle
turtle.getscreen().bgcolor("pink")
# point size
R = 20
# set speed (optional)
turtle.speed("fastest")
# screen size for generate point
size = turtle.getscreen().screensize()
W = size[0]
H = size[1]
# save position
X = 0
Y = 0
def draw_rand_circle():
global W, H, X, Y
# generate point in screen
X = random.randrange(-W+R, W-R)
Y = random.randrange(-H, H-2*R)
# draw circle
turtle.pencolor("pink")
turtle.goto(X, Y)
turtle.color("yellow", "green")
turtle.begin_fill()
turtle.circle(R)
turtle.end_fill()
def redraw(a, b):
global X, Y
# is click in the circle?
if X-R < a < X+R and Y < b < Y+2*R:
draw_rand_circle()
draw_rand_circle()
turtle.getscreen().onclick(redraw)
turtle.mainloop()