【问题标题】:Conceptual bug in a turtle program using resetscreen()使用 resetscreen() 的海龟程序中的概念错误
【发布时间】:2015-10-24 00:18:26
【问题描述】:

我试图满足的标准: 当用户按下空格键时屏幕会重置,这意味着绘制的线条消失并且未命名的海龟返回中心,但它不会返回到默认的海龟颜色和形状!强>

""" 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()

此代码允许用户在按下空格键时拖动海龟、按下按钮和重置屏幕。但是,出于某种原因,重置屏幕也会重置海龟的颜色。我怎样才能防止这种情况发生?

基本上我想要发生的情况是,比如说,用户点击蓝色方形按钮,然后重置屏幕以隐藏按钮绘制的形状,所有海龟都回到原来的位置,但未命名的海龟不会改变其先前的颜色和形状。如果我需要进一步详细说明,请告诉我。

【问题讨论】:

  • 什么是Turtle()?我不认为它是turtle 包的一部分。它是你定义的函数/类吗?
  • 这是一个类,但不是我定义的。当我需要使用海龟的休眠副本命名新海龟/绘图时,我通常会使用它
  • 你的问题描述建议你需要在resetscreen()之前保存一些数据,然后用place_turtles之后的数据来恢复你想要保留的部分。
  • @TerryJanReedy 是的,我想这就是我需要的。乌龟的形状没有重置,只是颜色……我不知道我是怎么做到的。

标签: python python-3.x turtle-graphics


【解决方案1】:

以“迟到总比没有好”的态度,我相信我已经修改了您的代码以获得您想要的行为。我还重新设计了您的拖动逻辑,为您提供更好的绘图能力:

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 调用期间关闭拖动事件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    • 2023-03-21
    • 2018-05-30
    • 2020-06-12
    相关资源
    最近更新 更多