gpt4 book ai didi

python - turtle 和倾斜物体

转载 作者:行者123 更新时间:2023-11-28 22:53:32 25 4
gpt4 key购买 nike

大家好,我的类(class)提出了一个问题,要求我使用眼睛、嘴巴和头部的锯齿状函数创建一个笑脸。在那之后,他们希望我们将它绘制 10 次,相互稍微重叠,并让它在每次重复时向左倾斜 10 度。我知道如何进行 for 循环,我遇到的问题是倾斜。下面是我目前所拥有的。你能为我指明倾斜的正确方向吗?

import turtle
s=turtle.Screen()
p=turtle.Turtle()

def happymouth(p,x,y):
p.setheading(-60)
jump(p,x-60.62,y+65)
p.circle(70,120)

def eyes(p,x,y):
jump(p,x+35,y+120)
p.dot(25)
jump(p,x-35,y+120)
p.dot(25)

def jump(p,x,y):
p.up()
p.goto(x,y)
p.down()


def emoticon(p,x,y):
p=turtle.Turtle()
s=turtle.Screen()
p.pensize(3)
p.setheading(0)
jump(p,x,y)
p.circle(100)
eyes(p,x,y)
happymouth(p,x,y)
jump(p,x,y)

最佳答案

你可以做到这一点,但它需要你重新考虑你的绘图逻辑。为了让表情符号在 10 度旋转下保持稳定,绘制表情符号时 turtle 的位置必须是相对的,而不是绝对的。没有 turtle.goto(),没有 jump(turtle, x, y)。然后为了在页面上适合你的十个表情符号,你也需要使大小相对,而不是绝对。这是执行此操作的返工:

from turtle import Turtle, Screen

def jump(turtle, x, y):
turtle.up()
turtle.goto(x, y)
turtle.down()

def head(turtle, size):
# to draw circle with current position as center, have to adjust the y position
turtle.up()
turtle.right(90)
turtle.forward(size)
turtle.left(90)
turtle.color("black", "yellow")
turtle.down()

turtle.begin_fill()
turtle.circle(size)
turtle.end_fill()

# return to the center of the circle
turtle.up()
turtle.color("black")
turtle.left(90)
turtle.forward(size)
turtle.right(90)
turtle.down()

def eyes(turtle, size):
turtle.up()
turtle.forward(0.35 * size)
turtle.left(90)
turtle.forward(0.2 * size)
turtle.right(90)
turtle.down()

turtle.dot(0.25 * size)

turtle.up()
turtle.backward(0.7 * size)
turtle.down()

turtle.dot(0.25 * size)

turtle.up()
turtle.forward(0.35 * size)
turtle.right(90)
turtle.forward(0.2 * size)
turtle.left(90)
turtle.down()

def happymouth(turtle, size):
turtle.up()
turtle.left(180)
turtle.forward(0.6 * size)
turtle.left(90)
turtle.forward(0.35 * size)
turtle.left(90)
turtle.down()

turtle.right(60)
turtle.circle(0.7 * size, 120)

turtle.up()
turtle.circle(0.7 * size, 240)
turtle.left(60)
turtle.forward(0.6 * size)
turtle.left(90)
turtle.forward(0.35 * size)
turtle.right(90)
turtle.down()

def emoticon(turtle, size):
turtle.pensize(0.03 * size)
head(turtle, size)
eyes(turtle, size)
happymouth(turtle, size)

screen = Screen()
yertle = Turtle()

width, height = screen.window_width(), screen.window_height()

yertle.setheading(-50)

for xy in range(-5, 5):
jump(yertle, xy * width / 10, xy * height / 10)

emoticon(yertle, 60)

yertle.setheading(yertle.heading() + 10)

screen.exitonclick()

上面的代码在绘制方面没有优化——它总是返回到中心以确保每个组件都是相对于它绘制的。但它基本上有效:

enter image description here

有一种完全不同的方法可以解决这个问题,它允许我们使用绝对 turtle.goto() 但它有自己的困难。我们可以将 turtle 本身设置为表情符号,并在页面上标记它。这也允许我们忽略相对大小,因为 turtle 游标有自己的大小调整能力:

from turtle import Turtle, Screen, Shape

def jump(turtle, x, y):
turtle.up()
turtle.goto(x, y)
turtle.down()

def head(turtle, shape, x, y):
jump(turtle, x, y - 100)
turtle.begin_poly()
turtle.circle(100)
turtle.end_poly()
shape.addcomponent(turtle.get_poly(), "yellow", "black")

def happymouth(turtle, shape, x, y):
turtle.setheading(-60)
jump(turtle, x - 60, y - 35)
turtle.begin_poly()
turtle.circle(70, 120)
turtle.end_poly()
shape.addcomponent(turtle.get_poly(), "black")
turtle.setheading(90)

def eyes(turtle, shape, x, y):
jump(turtle, x + 35, y + 20)
turtle.begin_poly()
turtle.circle(13)
turtle.end_poly()
shape.addcomponent(turtle.get_poly(), "black")

jump(turtle, x - 35, y + 20)
turtle.begin_poly()
turtle.circle(13)
turtle.end_poly()
shape.addcomponent(turtle.get_poly(), "black")

def emoticon(turtle, x, y):
shape = Shape("compound")

head(turtle, shape, x, y)
eyes(turtle, shape, x, y)
happymouth(turtle, shape, x, y)

screen.register_shape("emoticon", shape)

screen = Screen()
yertle = Turtle(visible="False")
emoticon(yertle, 0, 0)
yertle.shape("emoticon")

yertle.clear()

yertle.shapesize(0.6, 0.6)

width, height = screen.window_width(), screen.window_height()

yertle.setheading(50)

for xy in range(-5, 5):
jump(yertle, xy * width / 10, xy * height / 10)

yertle.stamp()
yertle.setheading(yertle.heading() + 10)

screen.exitonclick()

不幸的是,使用 turtle.*_poly() 完成的邮票只能由封闭的多边形组成,这意味着表情符号的微笑会有所变化:

enter image description here

玩得开心!

关于python - turtle 和倾斜物体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19354431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com