【发布时间】: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