尝试在Python中缩放/改变海龟图像的方向

问题描述 投票:0回答:1

我在 Python 中使用 Turtle 制作了一张蜜蜂的图像。在理想的世界中,我想知道如何缩放蜜蜂的大小,同时保持所有尺寸的比例。 我还想知道如何轻松更改该图像的方向。

我对编码相当陌生,所以我仍处于学习阶段。我知道可能有一个非常明显的答案,但我只是没有将其联系起来。任何帮助将不胜感激。

def visualise_data(rename_me_in_task_1b):
def draw_body(x, y, step):
# Body outline
penup()
pencolor('black')
fillcolor('gold')
goto(x, y)
pendown()
begin_fill()
setheading(90)
circle((step * 5), 180)
forward(step * 10)
circle((step * 5), 180)
forward(step * 10)
end_fill()
# Draw Stripe 1
fillcolor('black')
begin_fill()
setheading(180)
forward(step * 10)
setheading(270)
forward(step * 4)
setheading(0)
forward(step * 10)
setheading(90)
forward(step * 4)
end_fill()
# Goto Stripe 2
penup()
setheading(180)
forward(step * 10)
setheading(270)
forward(step * 6)
# Draw Stripe 2
setheading(0)
pendown()
begin_fill()
forward(step * 10)
setheading(270)
forward(step * 4)
setheading(180)
forward(step * 10)
setheading(90)
forward(step * 4)
end_fill()
penup()
# Draw stinger
goto((x - 50), (y - (step * 15)))
setheading(270)
pendown()
width(5)
forward(step)
penup()
width(3)
# Draw right wing
goto(x, (y * 0.66))
setheading(25)
fillcolor('gainsboro')
pendown()
begin_fill()
forward(step * 5)
circle((-step * 3.5), 180)
goto(x, (-y * 0.66))
goto(x, (y * 0.66))
end_fill()
penup()
# Draw left wing
goto(-x, (y * 0.66))
setheading(155)
pendown()
begin_fill()
forward(step * 5)
circle((step * 3.5), 180)
goto(-x, (-y * 0.66))
end_fill()
def draw_head(x, y, step):
# Head outline
width(3)
pencolor('black')
fillcolor('gold')
penup()
goto((x - 50), (y * 1.5))
pendown()
begin_fill()
setheading(0)
circle((step * 6), 360)
end_fill()
penup()
# Eyes
goto((x * 0.5), (y * (step * 0.275)))
pendown()
fillcolor('white')
begin_fill()
circle(step, 360)
penup()
goto((-x * 0.5), (y * (step * 0.275)))
pendown()
circle(step, 360)
end_fill()
penup()
# Pupils
goto((-x * 0.5), (y * (step * 0.29)))
pendown()
fillcolor('black')
begin_fill()
circle((step * 0.3), 360)
penup()
forward(step * 5)
pendown()
circle((step * 0.3), 360)
end_fill()
penup()
# Mouth
setheading(270)
forward(step * 3)
pendown()
setheading(270)
circle((-step * 2.5), 180)
penup()
# Antennae
goto((-x * 0.3), (y * (step * 0.36)))
setheading(100)
pendown()
forward(step * 4)
setheading(220)
forward(step * 2)
penup()
goto((x * 0.3), (y * (step * 0.36)))
setheading(80)
pendown()
forward(step * 4)
setheading(320)
forward(step * 2)
penup()
speed(0)
width(3)
title('The Australian Honey Bees-nest/Business')
def draw_main_body(x, y, step):
draw_body(x, y, step)
draw_head(x, y, step)
write("Australia's Honey production is abundant!", move=True, align='left',
font=("Arial", 8, "bold"))
draw_main_body(50, 50, 10)
hideturtle()
done()
mainloop()
python turtle-graphics python-turtle
1个回答
1
投票

除了@ggorlen通常的出色建议(+1)之外,您还必须避免像

setheading()
这样的命令,而应使用
left()
和/或
right()
。您还需要考虑将缩放应用于笔宽度等内容。您可能还需要“撤消”(用笔向上)一些东西,以使您的乌龟回到已知位置继续绘制其他物品。

作为示例,下面是对缩放和旋转的

draw_head()
函数的修改:

from turtle import Screen, Turtle

def draw_head(scale):

    # Head outline
    turtle.fillcolor('gold')
    turtle.width(scale * 0.3)
    turtle.penup()
    turtle.right(90)
    turtle.forward(scale * 6)
    turtle.left(90)
    turtle.pendown()
    turtle.begin_fill()
    turtle.circle(scale * 6, 360)
    turtle.end_fill()
    turtle.penup()

    # Eyes
    turtle.fillcolor('white')
    turtle.right(90)
    turtle.backward(scale * 7)
    turtle.left(90)

    # Left Eye
    turtle.forward(scale * 2.5)
    turtle.pendown()
    turtle.begin_fill()
    turtle.circle(scale, 360)
    turtle.end_fill()
    turtle.penup()

    # Right Eye
    turtle.backward(scale * 5)
    turtle.pendown()
    turtle.begin_fill()
    turtle.circle(scale, 360)
    turtle.end_fill()
    turtle.penup()

    # Pupils
    turtle.fillcolor('black')
    turtle.forward(scale * 2.5)
    turtle.left(90)
    turtle.forward(scale * 0.66)
    turtle.right(90)

    # Left Pupil
    turtle.forward(scale * 2.5)
    turtle.pendown()
    turtle.begin_fill()
    turtle.circle(scale * 0.3, 360)
    turtle.end_fill()
    turtle.penup()

    # Right Pupil
    turtle.backward(scale * 5)
    turtle.pendown()
    turtle.begin_fill()
    turtle.circle(scale * 0.3, 360)
    turtle.end_fill()
    turtle.penup()

    # Mouth
    turtle.right(90)
    turtle.forward(scale * 3)
    turtle.pendown()
    turtle.circle(scale * 2.5, 180)
    turtle.penup()

    # Antennae
    turtle.forward(scale * 5.5)

    # Left Antenna
    turtle.right(10)
    turtle.pendown()
    turtle.forward(scale * 4)
    turtle.right(120)
    turtle.forward(scale * 2)
    turtle.penup()

    turtle.backward(scale * 2)
    turtle.left(120)
    turtle.backward(scale * 4)
    turtle.left(100)
    turtle.forward(scale * 5)
    turtle.right(90)

    # Right Antenna
    turtle.left(10)
    turtle.pendown()
    turtle.forward(scale * 4)
    turtle.left(120)
    turtle.forward(scale * 2)
    turtle.penup()

def draw_main_body(x, y, scale):
    turtle.goto(x, y)
    draw_head(scale)

screen = Screen()
screen.title('The Australian Honey Bees-nest/Business')

turtle = Turtle()
turtle.speed('fastest')

draw_main_body(0, 0, 15)

turtle.setheading(45)
draw_main_body(-200, -200, 10)

turtle.setheading(180)
draw_main_body(200, -200, 5)

turtle.hideturtle()
screen.exitonclick()

© www.soinside.com 2019 - 2024. All rights reserved.