分段的Python龟图

时间:2017-04-02 09:15:49

标签: python turtle-graphics

有没有办法我们可以在盒子里画一些东西,这样当盒子排列时它就能创造一个完整的画面。我已经绘制了一个图像,现在我想要分成四个框,以便每个框绘制一部分图片:

from turtle import *
y=0

def head():
    color('green')
    fd(160)
    x=xcor()
    seth(90)
    begin_fill()
    #color('green')
    circle(x/2,180)
    end_fill()
    penup()
    goto(33,37)
    pendown()
    dot(13,'black')
    penup()
    goto(126,37)
    pendown()
    dot(13,'black')
    penup()
    home()
    pendown()
    hideturtle()
    fd(160)
    seth(90)
    circle(x/2,60)
    right(90)
    pensize(5)
    fd(30) 

    penup()
    home()
    #pendown()
    hideturtle()
    fd(160)
    seth(90)
    circle(x/2,120)
    right(90)
    pensize(5)
    pendown()
    fd(30)
    penup()
    home()
    penup()

def body():
    pensize(0)

    home()
    showturtle()
    goto(0,-7)
    pendown()
    begin_fill()
    fd(160)
    right(90)
    fd(120)
    right(90)
    fd(160)
    y=ycor()
    right(90)
    fd(120)
    end_fill()

def legs():
    penup()
    #color('red')
    goto(33,-169)
    pendown()
    pensize(32)
    fd(43)
    penup()
    goto(130,-169)
    pendown()
    fd(43)
    penup()

def hands():
    home()
    pensize(30)
    goto(-18,-77)
    pendown()
    left(90)
    fd(65)
    penup()
    goto(179,77)
    pendown()
    fd(65)
    penup()
    fd(100)
    hideturtle()
    circle(100)
    circle(100,360,59)


hideturtle()

head()
body()
legs()
hands()
done()

1 个答案:

答案 0 :(得分:0)

为了最大程度地控制图像质量,最好的方法是将图像外部分割成单独的GIF图像,然后使用register_shape()shape()方法将它们加载到光标中。

但是,如果你真的想画它们,这是一个粗略的例子。我没有使用'盒子'但只是绘制形状的元素,然后可以将其拖动以生成最终图形:

from turtle import *

ANTENNA_WIDTH = 5
ANTENNA_HEIGHT = 30
HEAD_RADIUS = 80

def head():

    begin_poly()

    setheading(90)
    circle(HEAD_RADIUS, 60)
    current_heading = heading()
    setheading(60)
    forward(ANTENNA_HEIGHT)
    backward(ANTENNA_HEIGHT)
    setheading(current_heading)
    circle(HEAD_RADIUS, 60)
    current_heading = heading()
    setheading(120)
    forward(ANTENNA_HEIGHT)
    backward(ANTENNA_HEIGHT)
    setheading(current_heading)
    circle(HEAD_RADIUS, 60)

    end_poly()

    register_shape("head", get_poly())

    reset()

BODY_HEIGHT = 120

def body():

    begin_poly()

    right(90)
    forward(BODY_HEIGHT)
    right(90)
    forward(HEAD_RADIUS * 2)
    right(90)
    forward(BODY_HEIGHT)

    end_poly()

    register_shape("body", get_poly())

    reset()

LEG_LENGTH = 43
LEG_WIDTH = 32

def legs():

    begin_poly()

    forward(LEG_WIDTH)
    setheading(-90)
    forward(LEG_LENGTH)
    circle(LEG_WIDTH / 2, 180)
    forward(LEG_LENGTH)
    setheading(0)

    forward(LEG_WIDTH)
    setheading(-90)
    forward(LEG_LENGTH)
    circle(LEG_WIDTH / 2, 180)
    forward(LEG_LENGTH)
    setheading(0)

    forward(LEG_WIDTH)

    end_poly()

    register_shape("legs", get_poly())

    reset()

ARM_WIDTH = 30
ARM_LENGTH = 65

def arm():

    begin_poly()

    setheading(90)
    circle(ARM_WIDTH / 2, 180)
    forward(ARM_LENGTH)
    circle(ARM_WIDTH / 2, 180)

    end_poly()

    register_shape("arm", get_poly())

    reset()

head()
head_turtle = Turtle("head")
head_turtle.color("green")
head_turtle.setheading(90)
head_turtle.penup()
head_turtle.goto(150, 150)

body()
body_turtle = Turtle("body")
body_turtle.color("green")
body_turtle.setheading(90)
body_turtle.penup()
body_turtle.goto(-150, -150)

legs()
legs_turtle = Turtle("legs")
legs_turtle.color("green")
legs_turtle.setheading(90)
legs_turtle.penup()
legs_turtle.goto(150, -150)

arm()
left_arm_turtle = Turtle("arm")
left_arm_turtle.color("green")
left_arm_turtle.setheading(90)
left_arm_turtle.penup()
left_arm_turtle.goto(-150, 150)

right_arm_turtle = Turtle("arm")
right_arm_turtle.color("green")
right_arm_turtle.setheading(90)
right_arm_turtle.penup()
right_arm_turtle.goto(0, 0)

hideturtle()

def drag_handler(turtle, x, y):
    turtle.ondrag(None)  # disable ondrag event inside drag_handler
    turtle.goto(x, y)
    turtle.ondrag(lambda x, y, turtle=turtle: drag_handler(turtle, x, y))

head_turtle.ondrag(lambda x, y: drag_handler(head_turtle, x, y))
body_turtle.ondrag(lambda x, y: drag_handler(body_turtle, x, y))
legs_turtle.ondrag(lambda x, y: drag_handler(legs_turtle, x, y))
left_arm_turtle.ondrag(lambda x, y: drag_handler(left_arm_turtle, x, y))
right_arm_turtle.ondrag(lambda x, y: drag_handler(right_arm_turtle, x, y))

mainloop()

虽然海龟文档显示只使用goto()作为拖动事件处理程序,但您对结果不满意。上面的处理程序只是一个goto()包含在代码中,可以禁用和重新启用事件,以便在您拖动元素时不会叠加。

开始屏幕

enter image description here

拖延后的屏幕

enter image description here