介绍
在这个教程中,教大家如何画一个鸡蛋,方法如下:

步骤一:
从上图中可以看到,画鸡蛋需要绘制4条弧线。红色的弧线是一个朝上的半圆。为了画出这个弧线,需要抬起钢笔,移动到红色弧线的左端,设置heading为270度,画一个180度的圆。代码片段如下:
turtle.up()
turtle.goto(-100,-20)
turtle.down()
turtle.seth(270)
turtle.color('red')
turtle.circle(100,180)
步骤二:
目前画笔位于红色弧线的右端。从这个位置继续,绘制一个45度的圆弧,半径是前半圆的两倍,用蓝色表示。代码片段如下:
turtle.color('blue')
turtle.circle(200,45)
步骤三:
画顶部的绿色弧线,它是一个四分之一圆(90度),半径约为第一个圆的0.586。更准确地说,半径是(2 - √2)乘以第一个圆的大小。代码片段如下:
turtle.color('green')
turtle.circle(100 * (2 - 2 ** 0.5), 90)
步骤四:
画最后一条蓝弧,与第二部的蓝弧对称。下面是绘制这个鸡蛋形状的完整代码:
import turtle
turtle.pensize(5)
turtle.up()
turtle.goto(-100, -20)
turtle.down()
turtle.seth(270)
turtle.color('red')
turtle.circle(100, 180)
turtle.color('blue')
turtle.circle(200, 45)
turtle.color('green')
turtle.circle(100 * (2 - 2 ** 0.5), 90)
turtle.color('blue')
turtle.circle(200, 45)
turtle.done()
定义画鸡蛋的方法
如果在画鸡蛋时,想指定鸡蛋的大小、位置,甚至倾斜角度,应该怎么做呢?
解决方案是创建一个draw_egg()函数,它接受这些值作为参数。下面是定义draw_egg()函数并绘制三个不同鸡蛋的完整代码。
from turtle import *
def draw_egg(x, y, size, tilt):
fillcolor("#e0be90")
pencolor("#f0e4d4")
begin_fill()
up()
goto(x, y)
down()
seth(270 + tilt)
circle(size, 180)
circle(2 * size, 45)
circle(0.586 * size, 90)
circle(2 * size, 45)
end_fill()
if __name__ == '__main__':
screen = Screen()
screen.setup(800, 600)
screen.title('*今条头日**-cloudcoder出品')
speed(1)
# hideturtle()
# screen.tracer(0, 0)
pensize(5)
draw_egg(-350, 10, 100, -10)
draw_egg(-100, 10, 120, 0)
draw_egg(200, 10, 80, 10)
done()

画很多鸡蛋
效果图

源代码如下:
from turtle import *
import turtle
import random
import math
def draw_star(size):
down()
for i in range(5):
forward(size)
right(144)
def draw_egg(x, y, size, tilt, color):
fillcolor(color)
begin_fill()
up()
goto(x, y)
down()
seth(270 + tilt)
circle(size, 180)
circle(2 * size, 45)
circle(0.586 * size, 90)
circle(2 * size, 45)
end_fill()
for i in range(10):
up()
goto(x + size + size / 2 * math.sin(random.randint(0, 90)),
y + size / 2 * math.sin(random.randint(0, 90)))
down()
seth(360 - tilt)
draw_star(7)
def main():
pencolor("white")
colors = ["red", "purple", "blue", "green", "orange", "yellow"]
for h in range(1, 5):
for i in range(1, 7):
color = colors[i * h % 6]
draw_egg(-470 + 120 * i, 370 - h * 150, 45, random.randint(-15, 15), color)
done()
if __name__ == '__main__':
screen = Screen()
screen.setup(800, 600)
screen.title('*今条头日**-cloudcoder出品')
speed("fastest")
# hideturtle()
screen.tracer(0, 0)
bgcolor('royal blue')
pensize(1)
main()