简体   繁体   English

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

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

I want to make a turtle-drawn object move, but I can't use any downloaded modules (for reasons I don't want to get into).我想让乌龟绘制的对象移动,但我不能使用任何下载的模块(因为我不想进入的原因)。 How would I do this?我该怎么做?

I agree with @martineau's comment in general:我总体上同意@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.如果您仅使用与当前位置相关的命令正确地进行图形绘制,您可以将所有这些命令放在一个函数中,并在调用它之前,将海龟定位到您希望它们都相对的位置。

But if your drawing is sufficiently simple, there's an alternative approach.但是,如果您的绘图足够简单,还有另一种方法。 You can make a turtle cursor that is your drawing, switch the turtle to that cursor and make your drawing do any move that the turtle cursor itself can do.您可以制作一个海龟光标作为您的绘图,将海龟切换到该光标并使您的绘图执行海龟光标本身可以执行的任何移动。

I'm going to illustrate using this house drawing from this An Hour of Code == An Hour of Fun page:我将使用来自这个代码一小时 == 一个小时的乐趣页面的房子图来说明:

在此处输入图片说明

The following code invisibly draws the house and stashes it as a turtle cursor.以下代码无形地绘制了房子并将其作为海龟光标隐藏起来。 It then sets the turtle shape to the house drawing and takes the house for a spin.然后将海龟形状设置为房屋图纸并旋转房屋。 Literally.字面上地。 After rotating the house in a circle, it shears it while moving it to the upper right of the drawing:将房子旋转一圈后,将其剪切,同时将其移动到图形的右上角:

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

Again, this approach only works for simple drawings composed of filled polygons.同样,这种方法仅适用于由填充多边形组成的简单绘图。

In case it benefits anyone else.万一它有利于其他人。 In the event you are willing to create a grid and draw at the per pixel level, I wrote a function that will sort through a list of lists (or tuple of tuples) which represent a graphical image.如果您愿意创建一个网格并在每个像素级别进行绘制,我编写了一个函数来对代表图形图像的列表(或元组元组)进行排序。 It draws a single pixel at a time, at whichever coordinates you input, and it centers the image to the provided coordinates.它一次绘制一个像素,无论您输入哪个坐标,并将图像居中到提供的坐标。

This is not an optimal solution for real-world use, but is a fully functioning proof of concept nonetheless这不是实际使用的最佳解决方案,但仍然是一个功能齐全的概念证明

# 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM