如何仅使用内置函数移动海龟图形绘制对象?

How do I move a turtle-graphics drawn object using only built in functions?

我想让乌龟绘制的对象移动,但我不能使用任何下载的模块(出于我不想深入的原因)。我该怎么做?

我总体上同意@martineau 的评论:

If you do the graphical drawing correctly using only commands which are relative to the current position, you can put all of them in a function and just before calling it, position the turtle to the location you want them all to be relative to.

但如果您的绘图足够简单,还有另一种方法。您可以制作一个 您的绘图的海龟光标,将海龟切换到该光标并使您的绘图执行海龟光标本身可以执行的任何移动。

我将使用此 An Hour of Code == An Hour of Fun 页面中的房屋图纸进行说明:

以下代码无形地绘制了房子并将其作为乌龟光标藏起来。然后它将海龟形状设置为房屋图纸并让房屋旋转。字面上地。把房子旋转一圈后,边移动边剪切到图的右上角:

from turtle import Turtle, Screen, Shape

screen = Screen()
screen.bgcolor('SkyBlue')

shape = Shape('compound')

turtle = Turtle(visible=False)
turtle.speed('fastest')
turtle.penup()

turtle.goto(-100, 0)

turtle.begin_poly()
turtle.goto(-100, 50)
turtle.goto(100, 50)
turtle.goto(100, 0)
turtle.goto(-100, 0)
turtle.end_poly()
shape.addcomponent(turtle.get_poly(), 'red')

turtle.goto(-100, 50)

turtle.begin_poly()
turtle.goto(0, 100)
turtle.goto(100, 50)
turtle.goto(-100, 50)
turtle.end_poly()
shape.addcomponent(turtle.get_poly(), 'brown')

turtle.goto(-40, 0)

turtle.begin_poly()
turtle.goto(-40, 30)
turtle.goto(-20, 30)
turtle.goto(-20, 0)
turtle.goto(-40, 0)
turtle.end_poly()
shape.addcomponent(turtle.get_poly(), 'orange')

turtle.goto(20, 20)

turtle.begin_poly()
turtle.goto(20, 40)
turtle.goto(50, 40)
turtle.goto(50, 20)
turtle.goto(20, 20)
turtle.end_poly()
shape.addcomponent(turtle.get_poly(), 'white')

screen.register_shape('house', shape)

turtle.reset()
turtle.penup()

# Now we can move our house in any manner that we move a turtle:

turtle.shape('house')

for theta in range(0, 360, 10):
    turtle.setheading(theta)

turtle.setheading(90)
turtle.shearfactor(0.5)
turtle.goto(300, 300)
turtle.shearfactor(0)

screen.mainloop()

同样,此方法仅适用于由填充多边形组成的简单图形。

以防对其他人有好处。如果您愿意创建一个网格并在每个像素级别进行绘制,我编写了一个函数来对表示图形图像的列表列表(或元组的元组)进行排序。无论您输入哪个坐标,它都会一次绘制一个像素,并将图像以提供的坐标为中心。

这不是 real-world 使用的最佳解决方案,但仍然是一个功能齐全的概念证明

# Snake head made from drawing pixel by pixel, with optional directionality
import turtle
import time

screen = turtle.Screen()
screen.bgcolor("#000000")
screen.tracer(0)

#              0 = black  1 = green  2 = Yellow  3 = red
head_colors = ["#000000", "#00AA66", "#FFFF00", "#FF0033"]

head_pixels = [
    [0,0,0,0,0,0,0,1,0,3,3,3,0,1,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0],
    [0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0],
    [0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0],
    [0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0],
    [0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0],
    [0,0,1,1,1,0,0,1,1,1,1,1,1,1,0,0,1,1,1,0,0],
    [0,1,1,1,0,0,0,0,1,1,1,1,1,0,0,0,0,1,1,1,0],
    [0,1,1,0,0,2,2,0,0,1,1,1,0,0,2,2,0,0,1,1,0],
    [0,1,1,0,0,2,2,0,0,1,1,1,0,0,2,2,0,0,1,1,0],
    [1,1,1,1,0,0,0,0,1,1,1,1,1,0,0,0,0,1,1,1,1],
    [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
]

def turtle_graphic(colors, pixel_data, turt, go=(0,0), d="up"):
    turt.hideturtle()
    y = len(pixel_data) / 2
    x = len(pixel_data[0]) / 2
    turt.clear()

    if d == "left":
        turt.left(90)
    elif d == "right":
        turt.right(90)
    elif d == "down":
        turt.left(180)

    for row in range(len(pixel_data)):
        if d == "down":
            turt.goto(go[0]+x-1,go[1]-y+row)
        elif d == "left":
            turt.goto(go[0]-x+row,go[1]-y)
        elif d == "right":
            turt.goto(go[0]+x-row-1,go[1]+y)
        else:
            turt.goto(go[0]-x,go[1]+y-row)
        for pixel in pixel_data[row]:
            turt.pendown()
            turt.color(colors[pixel])
            turt.forward(1)
            turt.penup()
    
    turt.goto(go[0],go[1])
    if d == "left":
        turt.right(90)
    elif d == "right":
        turt.left(90)
    elif d == "down":
        turt.left(180)

snake_head = turtle.Turtle()
snake_head.speed(0)

direction = ["up", "right", "down", "left"]
index = 0

while True:
    screen.update()

    turtle_graphic(head_colors, head_pixels, snake_head, (0,0), direction[index])
    if index < 3:
        index +=1
    else:
        index = 0

    time.sleep(0.5)