在python海龟中定义一个区域
Define a region in python turtle
所以我的目标是当我在 turtle 屏幕上的特定区域内单击时发生一个函数。
因此,当我的乌龟绘制一个正方形并且我在正方形内单击时,我需要发生一些事情。
示例:
turtle.onscreenclick(turtle.goto)
for i in range(4):
turtle.forward(30)
turtle.left(90)
if turtle.position() == (within square region):
Activate function()
如果您的意思是仅允许 goto
进入正方形:
import turtle
def goto_inside(x, y):
if 0 <= x <= 30 and 0 <= y <= 30:
turtle.goto(x, y)
turtle.onscreenclick(goto_inside)
# draw a square 30x30
for i in range(4):
turtle.forward(30)
turtle.left(90)
# turtle.mainloop()
您需要计算出正方形所定义区域的范围,以便稍后将鼠标单击位置与其进行比较,看看它们是否在正方形内。下面是一个完整的程序,首先让鼠标点击定义一个正方形的左下角,绘制它,然后每次鼠标在矩形区域内点击时调用指定的函数。
import turtle
def draw_square(x, y):
global target_region
turtle.penup()
turtle.goto(x, y)
turtle.setheading(0)
turtle.pendown()
square = []
for i in range(4):
square.append(turtle.pos()) # Save turtle coords
turtle.forward(30)
turtle.left(90)
# Find min and max coordinates of region
min_x = min(square, key=lambda p: p[0])[0]
min_y = min(square, key=lambda p: p[1])[1]
max_x = max(square, key=lambda p: p[0])[0]
max_y = max(square, key=lambda p: p[1])[1]
target_region = [min_x, min_y, max_x, max_y]
turtle.hideturtle()
turtle.onscreenclick(check_click) # Switch to next event handler
def check_click(x, y):
if (target_region[0] <= x <= target_region[2] and
target_region[1] <= y <= target_region[3]): # Within square region?
within_square_region_function() # Call activate function
def within_square_region_function():
print('clicked in square')
turtle.onscreenclick(draw_square) # Set initial event handler
turtle.mainloop()
所以我的目标是当我在 turtle 屏幕上的特定区域内单击时发生一个函数。 因此,当我的乌龟绘制一个正方形并且我在正方形内单击时,我需要发生一些事情。
示例:
turtle.onscreenclick(turtle.goto)
for i in range(4):
turtle.forward(30)
turtle.left(90)
if turtle.position() == (within square region):
Activate function()
如果您的意思是仅允许 goto
进入正方形:
import turtle
def goto_inside(x, y):
if 0 <= x <= 30 and 0 <= y <= 30:
turtle.goto(x, y)
turtle.onscreenclick(goto_inside)
# draw a square 30x30
for i in range(4):
turtle.forward(30)
turtle.left(90)
# turtle.mainloop()
您需要计算出正方形所定义区域的范围,以便稍后将鼠标单击位置与其进行比较,看看它们是否在正方形内。下面是一个完整的程序,首先让鼠标点击定义一个正方形的左下角,绘制它,然后每次鼠标在矩形区域内点击时调用指定的函数。
import turtle
def draw_square(x, y):
global target_region
turtle.penup()
turtle.goto(x, y)
turtle.setheading(0)
turtle.pendown()
square = []
for i in range(4):
square.append(turtle.pos()) # Save turtle coords
turtle.forward(30)
turtle.left(90)
# Find min and max coordinates of region
min_x = min(square, key=lambda p: p[0])[0]
min_y = min(square, key=lambda p: p[1])[1]
max_x = max(square, key=lambda p: p[0])[0]
max_y = max(square, key=lambda p: p[1])[1]
target_region = [min_x, min_y, max_x, max_y]
turtle.hideturtle()
turtle.onscreenclick(check_click) # Switch to next event handler
def check_click(x, y):
if (target_region[0] <= x <= target_region[2] and
target_region[1] <= y <= target_region[3]): # Within square region?
within_square_region_function() # Call activate function
def within_square_region_function():
print('clicked in square')
turtle.onscreenclick(draw_square) # Set initial event handler
turtle.mainloop()