尝试在Python中缩放/改变乌龟图像的方向
我用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()
1 个回答
1
除了@ggorlen通常给出的很棒的建议(点赞),你还需要避免使用像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()