Python 使用resetscreen()的海龟程序中的概念性错误
我试图达到的标准: 当用户按下空格键时,屏幕将重置,这意味着绘制的线将消失,未命名的海龟将返回中心,但它不会返回默认的海龟颜色和形状强>Python 使用resetscreen()的海龟程序中的概念性错误,python,python-3.x,turtle-graphics,Python,Python 3.x,Turtle Graphics,我试图达到的标准: 当用户按下空格键时,屏幕将重置,这意味着绘制的线将消失,未命名的海龟将返回中心,但它不会返回默认的海龟颜色和形状 """ A simple drawing program Click and drag the center turtle to draw Click the colorful buttons to change the turtle's color, and then draw different shapes. Press the SPACEBAR key
""" A simple drawing program
Click and drag the center turtle to draw
Click the colorful buttons to change the
turtle's color,
and then draw different shapes.
Press the SPACEBAR key to reset the
drawing.
"""
from turtle import *
turtle_1 = Turtle()
turtle_2 = Turtle()
turtle_3 = Turtle()
def callback_1(x,y):
color("red")
shape("circle")
circle(100)
def callback_2(x,y):
color("blue")
shape("square")
circle(100,steps=4)
def callback_3(x,y):
color("green")
shape("triangle")
circle(100,steps=3)
def place_turtles():
turtle_1.color("red")
turtle_1.shape("circle")
turtle_1.penup()
turtle_1.goto(-200,-200)
turtle_2.color("blue")
turtle_2.shape("square")
turtle_2.penup()
turtle_2.goto(0,-200)
turtle_3.color("green")
turtle_3.shape("triangle")
turtle_3.penup()
turtle_3.goto(200,-200)
def start_over():
resetscreen()
place_turtles()
listen()
onkey(start_over, "space")
ondrag(goto)
place_turtles()
此代码允许用户拖动海龟,按下按钮,并在按下空格键时重置屏幕。然而,由于某些原因,重置屏幕也会重置海龟的颜色。我怎样才能防止这种情况发生
基本上,我想做的是,如果,比如说,用户点击蓝色方形按钮,然后重置屏幕以隐藏按钮绘制的形状,所有海龟都会返回到它们原来的位置,但未命名的海龟不会改变它以前的颜色和形状。如果需要进一步详细说明,请告诉我。本着“迟做总比不做好”的态度,我相信我已经修改了您的代码,以获得您想要的行为。我还修改了拖动逻辑,使您具有更好的绘图能力:
from turtle import Turtle, Screen, getturtle
def callback_1(x, y):
anonymous.color(*turtle_1.color())
anonymous.shape(turtle_1.shape())
anonymous.circle(100)
def callback_2(x, y):
anonymous.color(*turtle_2.color())
anonymous.shape(turtle_2.shape())
anonymous.circle(100, steps=4)
def callback_3(x, y):
anonymous.color(*turtle_3.color())
anonymous.shape(turtle_3.shape())
anonymous.circle(100, steps=3)
def setup_turtles():
turtle_1.onclick(callback_1)
turtle_1.color("red")
turtle_1.penup()
turtle_1.goto(-200, -200)
turtle_2.onclick(callback_2)
turtle_2.color("blue")
turtle_2.penup()
turtle_2.goto(0, -200)
turtle_3.onclick(callback_3)
turtle_3.color("green")
turtle_3.penup()
turtle_3.goto(200, -200)
def start_over():
colors = anonymous.color()
anonymous.reset()
anonymous.color(*colors)
def drag_handler(x, y):
anonymous.ondrag(None) # disable event inside event handler
anonymous.goto(x, y)
anonymous.ondrag(drag_handler) # reenable event on event handler exit
anonymous = getturtle()
anonymous.ondrag(drag_handler)
turtle_1 = Turtle(shape="circle")
turtle_2 = Turtle(shape="square")
turtle_3 = Turtle(shape="triangle")
setup_turtles()
screen = Screen()
screen.onkey(start_over, "space")
screen.listen()
screen.mainloop()
许多变化只是出于个人风格的原因。两个关键的变化是:只需重置匿名海龟本身,而不是屏幕;与其将goto
用作事件处理程序,不如将其封装在一个函数中,该函数在调用goto
期间关闭拖动事件。以一种“迟做总比不做好”的态度,我相信我已经重新编写了您的代码,以获得您想要的行为。我还修改了拖动逻辑,使您具有更好的绘图能力:
from turtle import Turtle, Screen, getturtle
def callback_1(x, y):
anonymous.color(*turtle_1.color())
anonymous.shape(turtle_1.shape())
anonymous.circle(100)
def callback_2(x, y):
anonymous.color(*turtle_2.color())
anonymous.shape(turtle_2.shape())
anonymous.circle(100, steps=4)
def callback_3(x, y):
anonymous.color(*turtle_3.color())
anonymous.shape(turtle_3.shape())
anonymous.circle(100, steps=3)
def setup_turtles():
turtle_1.onclick(callback_1)
turtle_1.color("red")
turtle_1.penup()
turtle_1.goto(-200, -200)
turtle_2.onclick(callback_2)
turtle_2.color("blue")
turtle_2.penup()
turtle_2.goto(0, -200)
turtle_3.onclick(callback_3)
turtle_3.color("green")
turtle_3.penup()
turtle_3.goto(200, -200)
def start_over():
colors = anonymous.color()
anonymous.reset()
anonymous.color(*colors)
def drag_handler(x, y):
anonymous.ondrag(None) # disable event inside event handler
anonymous.goto(x, y)
anonymous.ondrag(drag_handler) # reenable event on event handler exit
anonymous = getturtle()
anonymous.ondrag(drag_handler)
turtle_1 = Turtle(shape="circle")
turtle_2 = Turtle(shape="square")
turtle_3 = Turtle(shape="triangle")
setup_turtles()
screen = Screen()
screen.onkey(start_over, "space")
screen.listen()
screen.mainloop()
许多变化只是出于个人风格的原因。两个关键的变化是:只需重置匿名海龟本身,而不是屏幕;不要将
goto
用作事件处理程序,而是将其封装在一个函数中,该函数在goto
调用期间关闭拖动事件。什么是Turtle()
?我不认为它是turtle
包的一部分。它是您定义的函数/类吗?它是一个类,但不是我定义的类。我通常在需要使用turtle的休眠副本命名新turtle/draw时使用它。您的问题描述建议您需要在resetscreen()之前保存一些数据,然后在place_turtles之后使用这些数据来恢复您想要保留的部分。@TerryJanReedy是的,我想这就是我需要的。海龟的形状并没有重置,只是颜色而已。。我不知道我是怎么做到的。什么是Turtle()
?我不认为它是turtle
包的一部分。它是您定义的函数/类吗?它是一个类,但不是我定义的类。我通常在需要使用turtle的休眠副本命名新turtle/draw时使用它。您的问题描述建议您需要在resetscreen()之前保存一些数据,然后在place_turtles之后使用这些数据来恢复您想要保留的部分。@TerryJanReedy是的,我想这就是我需要的。海龟的形状并没有重置,只是颜色而已。。我不知道我是怎么做到的。