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

时间:2017-11-06 19:17:55

标签: python turtle-graphics

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

2 个答案:

答案 0 :(得分:1)

我同意@ martineau的评论:

  

如果仅使用命令正确执行图形绘制   相对于当前位置,你可以把它们全部放在一个   功能,并在调用它之前,将乌龟放置到   你希望他们都相对的位置。

但如果您的绘图足够简单,那么还有另一种方法。你可以创建一个 你的绘图的乌龟光标,将乌龟切换到那个光标,让你的绘图做乌龟光标本身可以做的任何移动。

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

enter image description here

以下代码无形地绘制房屋并将其作为乌龟光标存放。然后它将龟形状设置为房屋绘图并将房子旋转。从字面上看。将房子旋转成圆形后,将其剪切,同时将其移动到图纸的右上角:

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

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

答案 1 :(得分:0)

以防他人受惠。如果您愿意创建一个网格并以每个像素为单位绘制,我编写了一个函数,该函数将对代表图形图像的列表(或元组的元组)进行排序。无论您输入什么坐标,它都会一次绘制一个像素,并将图像居中到所提供的坐标。

这不是在现实世界中使用的最佳解决方案,但仍然是功能齐全的概念证明

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