如何让一只乌龟在另一只乌龟上方绘制?
How to make a turtle draw above another one?
我正在做一个项目,其中有一只乌龟画了一条黑线,但紧随其后的是另一只乌龟画了一条白线。我想让这条白线画在黑线上以“擦除”黑线,但黑线仍然在白线上方。
我正在 trinket.io
上编程
这是我的代码:
import turtle
#create elements
pen = turtle.Turtle()
follow = turtle.Turtle()
screen = turtle.Screen()
#create variables
positions = [0,-180,0,-180,0,-180,0,-180,0,-180,0,-180,0,-180,0,-180,0,-180,0,-180]
#moving functions
def move_left():
if pen.xcor() > -180:
pen.goto(pen.xcor()-10,pen.ycor())
def move_right():
if pen.xcor() < 180:
pen.goto(pen.xcor()+10,pen.ycor())
def move_up():
if pen.ycor() < 180:
pen.goto(pen.xcor(),pen.ycor()+10)
def move_down():
if pen.ycor() > -180:
pen.goto(pen.xcor(),pen.ycor()-10)
#pen setup
pen.speed(1000)
pen.penup()
pen.goto(0,-180)
pen.left(90)
pen.pendown()
follow.left(90)
follow.color("#FFFFF1")
follow.width(50)
#adjust frame refresh settings
pen.tracer(0,0)
rate = 0
refresh = 0
#game functionality
def game():
screen.onkey(move_left, 'left')
screen.onkey(move_right, 'right')
screen.onkey(move_up, 'up')
screen.onkey(move_down, 'down')
follow.goto(positions[0],positions[1])
positions.append(pen.xcor())
positions.append(pen.ycor())
positions.pop(0)
positions.pop(0)
#wait for keys
screen.listen()
#loop
while True:
game()
if refresh < rate:
refresh = refresh + 1
else:
pen.update()
#print(positions)
refresh = 0
如何修复我的程序?
您只需调用每个 screen.onkey()
实例一次,在循环中(间接)调用它们不会获得任何好处。此外,不清楚为什么 positions
被预初始化为十个位置。
我猜你想要一只留下踪迹的乌龟,但那条踪迹的长度有限,随着新踪迹的铺设,它的尽头会消失。根据您的描述,您希望它在 trinket.io 乌龟中工作,这意味着我们不能使用像 stamps
这样的功能来使它更清洁。
下面是我对您的代码进行的修改,我已经在 trinket.io 上进行了测试,它留下了 20 个线段。最旧的线段随着新线段的放置而消失:
from turtle import Screen, Turtle
NUMBER_SEGMENTS = 20
# moving functions
def truncate():
if len(positions) >= NUMBER_SEGMENTS:
follower.setposition(positions.pop(0))
screen.update()
def move_left():
pen.setheading(180)
if pen.xcor() > -180:
pen.forward(10)
positions.append(pen.position())
truncate()
def move_right():
pen.setheading(0)
if pen.xcor() < 180:
pen.forward(10)
positions.append(pen.position())
truncate()
def move_up():
pen.setheading(90)
if pen.ycor() < 180:
pen.forward(10)
positions.append(pen.position())
truncate()
def move_down():
pen.setheading(270)
if pen.ycor() > -180:
pen.forward(10)
positions.append(pen.position())
truncate()
# create elements
screen = Screen()
# adjust frame refresh settings
screen.tracer(0)
refresh = 0
# pen setup
pen = Turtle()
pen.speed('fastest')
pen.penup()
pen.sety(-180)
pen.setheading(90)
pen.pendown()
follower = pen.clone()
follower.hideturtle()
follower.width(3)
follower.color('#F9F9F9')
# create variables
positions = []
# wait for keys
screen.onkey(move_left, 'Left')
screen.onkey(move_right, 'Right')
screen.onkey(move_up, 'Up')
screen.onkey(move_down, 'Down')
screen.listen()
screen.update()
screen.mainloop()
不完美,但是trinket.io有点束缚我们的爪子。这大致就是您要尝试做的事情吗?
我正在做一个项目,其中有一只乌龟画了一条黑线,但紧随其后的是另一只乌龟画了一条白线。我想让这条白线画在黑线上以“擦除”黑线,但黑线仍然在白线上方。
我正在 trinket.io
上编程这是我的代码:
import turtle
#create elements
pen = turtle.Turtle()
follow = turtle.Turtle()
screen = turtle.Screen()
#create variables
positions = [0,-180,0,-180,0,-180,0,-180,0,-180,0,-180,0,-180,0,-180,0,-180,0,-180]
#moving functions
def move_left():
if pen.xcor() > -180:
pen.goto(pen.xcor()-10,pen.ycor())
def move_right():
if pen.xcor() < 180:
pen.goto(pen.xcor()+10,pen.ycor())
def move_up():
if pen.ycor() < 180:
pen.goto(pen.xcor(),pen.ycor()+10)
def move_down():
if pen.ycor() > -180:
pen.goto(pen.xcor(),pen.ycor()-10)
#pen setup
pen.speed(1000)
pen.penup()
pen.goto(0,-180)
pen.left(90)
pen.pendown()
follow.left(90)
follow.color("#FFFFF1")
follow.width(50)
#adjust frame refresh settings
pen.tracer(0,0)
rate = 0
refresh = 0
#game functionality
def game():
screen.onkey(move_left, 'left')
screen.onkey(move_right, 'right')
screen.onkey(move_up, 'up')
screen.onkey(move_down, 'down')
follow.goto(positions[0],positions[1])
positions.append(pen.xcor())
positions.append(pen.ycor())
positions.pop(0)
positions.pop(0)
#wait for keys
screen.listen()
#loop
while True:
game()
if refresh < rate:
refresh = refresh + 1
else:
pen.update()
#print(positions)
refresh = 0
如何修复我的程序?
您只需调用每个 screen.onkey()
实例一次,在循环中(间接)调用它们不会获得任何好处。此外,不清楚为什么 positions
被预初始化为十个位置。
我猜你想要一只留下踪迹的乌龟,但那条踪迹的长度有限,随着新踪迹的铺设,它的尽头会消失。根据您的描述,您希望它在 trinket.io 乌龟中工作,这意味着我们不能使用像 stamps
这样的功能来使它更清洁。
下面是我对您的代码进行的修改,我已经在 trinket.io 上进行了测试,它留下了 20 个线段。最旧的线段随着新线段的放置而消失:
from turtle import Screen, Turtle
NUMBER_SEGMENTS = 20
# moving functions
def truncate():
if len(positions) >= NUMBER_SEGMENTS:
follower.setposition(positions.pop(0))
screen.update()
def move_left():
pen.setheading(180)
if pen.xcor() > -180:
pen.forward(10)
positions.append(pen.position())
truncate()
def move_right():
pen.setheading(0)
if pen.xcor() < 180:
pen.forward(10)
positions.append(pen.position())
truncate()
def move_up():
pen.setheading(90)
if pen.ycor() < 180:
pen.forward(10)
positions.append(pen.position())
truncate()
def move_down():
pen.setheading(270)
if pen.ycor() > -180:
pen.forward(10)
positions.append(pen.position())
truncate()
# create elements
screen = Screen()
# adjust frame refresh settings
screen.tracer(0)
refresh = 0
# pen setup
pen = Turtle()
pen.speed('fastest')
pen.penup()
pen.sety(-180)
pen.setheading(90)
pen.pendown()
follower = pen.clone()
follower.hideturtle()
follower.width(3)
follower.color('#F9F9F9')
# create variables
positions = []
# wait for keys
screen.onkey(move_left, 'Left')
screen.onkey(move_right, 'Right')
screen.onkey(move_up, 'Up')
screen.onkey(move_down, 'Down')
screen.listen()
screen.update()
screen.mainloop()
不完美,但是trinket.io有点束缚我们的爪子。这大致就是您要尝试做的事情吗?