Python图形库Turtle绘制海绵宝宝
   一些代码   3 评论   9210 浏览

Python图形库Turtle绘制海绵宝宝

   一些代码   3 评论   9210 浏览

屏幕:690*580
以下代码在Python123通过

import turtle as t
import math
import random

#封装定位函数(左下角开始)
def main(x,y):
    t.setheading(0)
    t.penup()
    t.goto(x-690/2,y-580/2)
    t.pendown()
    
#封装椭圆函数
def ellipse(a,b,angle,steps,x,y):
    minAngle = (2*math.pi/360) * angle / steps
    t.penup()
    t.setpos(x+0,y-b)
    t.pendown()
    for i in range(steps):
        nextPoint = [x+a*math.sin((i+1)*minAngle),y+-b*math.cos((i+1)*minAngle)]
        t.setpos(nextPoint)

#封装旋转角度椭圆函数
def ellipseR(a,b,angle,steps,x,y,rotateAngle):
    minAngle = (2*math.pi/360) * angle / steps
    rotateAngle = rotateAngle/360*2*math.pi
    t.penup()
    t.setpos(x+b*math.sin(rotateAngle),y-b*math.cos(rotateAngle))
    t.pendown()
    for i in range(steps):
        nextPoint = [a*math.sin((i+1)*minAngle),-b*math.cos((i+1)*minAngle)]
        nextPoint = [x+nextPoint[0]*math.cos(rotateAngle)-nextPoint[1]*math.sin(rotateAngle),
                     y+nextPoint[0]*math.sin(rotateAngle)+nextPoint[1]*math.cos(rotateAngle)]
        t.setpos(nextPoint)


#蓝天
t.pensize(2)
t.color(152,245,255)
main(0,0)
t.fillcolor(152,245,255)
t.begin_fill()
t.fd(690)
t.left(90)
t.fd(580)
t.left(90)
t.fd(690)
t.left(90)
t.fd(580)
t.end_fill()

#气泡
main(160,400)
t.pensize(5)
t.color(255,255,255)
t.circle(80)

main(120,489)
t.pensize(5)
t.color(255,255,255)
t.fillcolor(255,255,255)
t.begin_fill()
t.circle(20)
t.end_fill()

main(300,440)
t.pensize(5)
t.color(255,255,255)
t.circle(30)

main(300,475)
t.pensize(5)
t.color(255,255,255)
t.begin_fill()
t.circle(7)
t.end_fill()


#水草
main(200,180)
r=100
temp=0
while temp<2*math.pi:
    t.color(random.randint(1,255),random.randint(1,255),random.randint(1,255))
    s=r*(1+math.sin(18*temp)/5)*(0.5+math.sin(6*temp)/2)
    x=s*math.cos(temp)-224
    y=s*math.sin(temp)-110
    t.goto(x,y)
    temp=temp+math.pi/64


#菠萝叶子
t.pensize(5)
t.color(34,127,58)
t.fillcolor(109,185,45)
main(600,430)
t.begin_fill()
ellipseR(20,80,500,60,170,180,50)
t.end_fill()

main(550,500)
t.begin_fill()
ellipseR(15,80,500,60,240,200,15)
t.end_fill()

main(550,500)
t.begin_fill()
ellipseR(15,80,500,60,289,200,-15)
t.end_fill()

main(600,430)
t.begin_fill()
ellipseR(20,80,500,60,360,180,-50)
t.end_fill()

main(600,430)
t.color(12,93,50)
t.pensize(5)
t.fillcolor(70,134,56)
t.begin_fill()
ellipseR(25,100,500,60,267,230,0)
t.end_fill()
main(600,430)
t.begin_fill()
ellipseR(25,100,500,60,194,230,30)
t.end_fill()
main(600,430)
t.begin_fill()
ellipseR(25,100,500,60,340,230,-30)
t.end_fill()




#菠萝屋
main(611,0)
t.pensize(5)
t.color(233,71,26)
t.fillcolor(241,137,26)
t.begin_fill()
a=7.6
for i in range(120):       
    if 0<=i<30 or 60<=i<90:
        a=a+0.2
        t.lt(3)
        t.fd(a)
    else:
        a=a-0.2
        t.lt(3)
        t.fd(a)
t.end_fill()

#交叉线
main(584,431)
t.setheading(-45)
t.color(233,69,26)
t.fd(300)

main(520,405)
t.setheading(-45)
t.color(233,69,26)
t.fd(300)

main(470,358)
t.setheading(-45)
t.color(233,69,26)
t.fd(500)

main(433,294)
t.setheading(-45)
t.color(233,69,26)
t.fd(500)

main(425,205)
t.setheading(-45)
t.color(233,69,26)
t.fd(500)


main(644,431)
t.setheading(-135)
t.color(233,69,26)
t.fd(310)

main(710,405)
t.setheading(-135)
t.color(233,69,26)
t.fd(384)

main(757,358)
t.setheading(-135)
t.color(233,69,26)
t.fd(404)

main(792,294)
t.setheading(-135)
t.color(233,69,26)
t.fd(380)

main(800,205)
t.setheading(-135)
t.color(233,69,26)
t.fd(400)

t.color(131,138,138)
t.fillcolor(169,169,169)
main(640,100)
t.begin_fill()
ellipse(53,67,500,60,270,-130)
t.end_fill()

t.pensize(3)
main(640,100)
t.begin_fill()
ellipse(44,62,500,60,270,-130)
t.end_fill()

main(615,163)
t.color(131,138,138)
t.fillcolor(131,138,138)
t.begin_fill()
t.circle(6)
t.end_fill()

t.pensize(4)
main(602,182)
t.setheading(67.5)
for i in range(8):
    t.left(67.5)
    t.fd(14)
    t.left(180)
    t.fd(14)
    t.left(67.5)
    t.fd(14)

#沙地
main(0,0)
t.left(90)
t.color(255,237,139)
t.fillcolor(255,237,139)
t.begin_fill()
t.fd(108)
t.right(90)
t.fd(690)
t.right(90)
t.fd(108)
t.right(90)
t.fd(690)
t.end_fill()

#棕线
t.color(152,245,255)
main(0,0)
t.left(90)
t.fd(108)
t.right(90)
t.pensize(5)
t.color(205,155,29)
t.fd(690)


#裤子
t.color(0,0,0)
main(217,165)
t.fillcolor(140,101,8)
t.begin_fill()
t.setheading(-90)
t.fd(42)
t.left(90)
t.fd(252)
t.left(90)
t.fd(42)
t.end_fill()

main(228,156)
t.begin_fill()
t.fillcolor(0,0,0)
t.fd(19)
t.right(90)
t.fd(11)
t.right(90)
t.fd(19)
t.right(90)
t.fd(11)
t.end_fill()

main(273,156)
t.begin_fill()
t.fillcolor(0,0,0)
t.fd(19)
t.right(90)
t.fd(11)
t.right(90)
t.fd(19)
t.right(90)
t.fd(11)
t.end_fill()

main(402,156)
t.begin_fill()
t.fillcolor(0,0,0)
t.fd(19)
t.right(90)
t.fd(11)
t.right(90)
t.fd(19)
t.right(90)
t.fd(11)
t.end_fill()

main(440,156)
t.begin_fill()
t.fillcolor(0,0,0)
t.fd(19)
t.right(90)
t.fd(11)
t.right(90)
t.fd(19)
t.right(90)
t.fd(11)
t.end_fill()

t.color(200,200,0)
main(272,100)
t.fillcolor(254,255,5)
t.begin_fill()
t.setheading(-90)
t.fd(30)
t.left(90)
t.fd(20)
t.left(90)
t.fd(30)
t.end_fill()

t.color(200,200,0)
main(392,100)
t.fillcolor(254,255,5)
t.begin_fill()
t.setheading(-90)
t.fd(30)
t.left(90)
t.fd(20)
t.left(90)
t.fd(30)
t.end_fill()

t.color(0,0,0)
main(260,123)
t.fillcolor(139,101,7)
t.begin_fill()
t.fd(44)
t.right(90)
t.fd(21)
t.right(90)
t.fd(44)
t.right(90)
t.fd(21)
t.end_fill()

main(380,123)
t.fillcolor(139,101,7)
t.begin_fill()
t.fd(44)
t.right(90)
t.fd(21)
t.right(90)
t.fd(44)
t.right(90)
t.fd(21)
t.end_fill()


t.color(169,169,169)
main(272,70)
t.fillcolor(255,255,255)
t.begin_fill()
t.setheading(-90)
t.fd(47)
t.left(90)
t.fd(20)
t.left(90)
t.fd(47)
t.end_fill()

t.color(169,169,169)
main(392,70)
t.fillcolor(255,255,255)
t.begin_fill()
t.setheading(-90)
t.fd(47)
t.left(90)
t.fd(20)
t.left(90)
t.fd(47)
t.end_fill()

main(272,63)
t.color(255,0,0)
t.fd(20)

main(392,63)
t.fd(20)

main(272,49)
t.color(0,0,255)
t.fd(20)

main(392,49)
t.fd(20)

main(215,30)
t.color(0,0,0)
t.fillcolor(0,0,0)
t.begin_fill()
t.fd(93)
t.right(90)
t.fd(30)
t.right(90)
t.fd(93)
t.right(90)
t.fd(30)
t.end_fill()

main(374,30)
t.color(0,0,0)
t.fillcolor(0,0,0)
t.begin_fill()
t.fd(93)
t.right(90)
t.fd(30)
t.right(90)
t.fd(93)
t.right(90)
t.fd(30)
t.end_fill()

main(236,6)
t.color(0,0,0)
t.begin_fill()
t.circle(20)
t.end_fill()

main(447,6)
t.color(0,0,0)
t.begin_fill()
t.circle(20)
t.end_fill()

#手
main(193,190)
t.color(200,200,0)
t.fillcolor(254,255,5)
t.begin_fill()
t.setheading(-90)
t.fd(62)
t.left(90)
t.fd(17)
t.left(90)
t.fd(62)
t.left(90)
t.fd(17)
t.end_fill()

main(477,190)
t.color(200,200,0)
t.fillcolor(254,255,5)
t.begin_fill()
t.setheading(-90)
t.fd(62)
t.left(90)
t.fd(17)
t.left(90)
t.fd(62)
t.left(90)
t.fd(17)
t.end_fill()

main(201,100)
t.color(200,200,0)
t.fillcolor(254,255,5)
t.begin_fill()
t.circle(18)
t.end_fill()

main(485,100)
t.color(200,200,0)
t.fillcolor(254,255,5)
t.begin_fill()
t.circle(18)
t.end_fill()

#衣服
t.pensize(5)
t.color(169,169,169)
main(217,210)
t.fillcolor(255,255,255)
t.begin_fill()
t.setheading(-90)
t.fd(43)
t.left(90)
t.fd(255)
t.left(90)
t.fd(43)
t.end_fill()

main(283,210)
t.setheading(-90)
t.fd(43)

main(413,210)
t.setheading(-90)
t.fd(43)

main(325,210)
t.setheading(-135)
t.fd(60)

main(369,210)
t.setheading(-45)
t.fd(60)

main(217,210)

t.fillcolor(255,255,255)
t.begin_fill()
main(217,210)
t.setheading(-150)
t.fd(40)
t.left(114)
t.fd(40)
t.left(128)
t.fd(30)
t.end_fill()

main(475,210)

t.fillcolor(255,255,255)
t.begin_fill()
main(475,210)
t.setheading(-45)
t.fd(40)
t.right(110)
t.fd(35)
t.right(115)
t.fd(30)
t.end_fill()


#海绵宝宝上半身轮廓与填充
main(217,461)
t.color(204,204,0)
t.fillcolor(254,255,5)
t.begin_fill()
t.right(40)
for i in range(5):
    t.circle(20,80)
    t.circle(-20,80)
t.setheading(-90)
t.left(40)
for i in range(5):
    t.circle(-20,80)
    t.circle(20,80)
t.setheading(-180)
t.right(40)
for i in range(5):
    t.circle(20,80)
    t.circle(-20,80)
t.setheading(90)
t.left(40)
for i in range(5):
    t.circle(-20,80)
    t.circle(20,80)
t.setheading(-180)
t.right(40)
t.end_fill()

#面部

t.pensize(10)
t.color(0,0,0)
main(276,408)
t.setheading(135)
t.fd(22)
main(301,413)
t.setheading(90)
t.fd(22)
main(320,406)
t.setheading(45)
t.fd(22)

main(373,406)
t.setheading(135)
t.fd(22)

main(397,415)
t.setheading(90)
t.fd(20)

main(420,407)
t.setheading(45)
t.fd(20)

t.pensize(5)
t.color(166,166,173)
t.setheading(0)
main(298,328)
t.fillcolor(255,255,255)
t.begin_fill()
t.circle(45)
t.end_fill()

t.color(166,166,173)
t.setheading(0)
main(397,328)
t.fillcolor(255,255,255)
t.begin_fill()
t.circle(45)
t.end_fill()

t.color(14,75,133)
t.setheading(0)
main(304,353)
t.fillcolor(31,144,255)
t.begin_fill()
t.circle(19)
t.end_fill()

main(393,353)
t.fillcolor(31,144,255)
t.begin_fill()
t.circle(19)
t.end_fill()

t.color(0,0,0)
main(304,366)
t.fillcolor(0,0,0)
t.begin_fill()
t.circle(6)
t.end_fill()

main(393,366)
t.fillcolor(0,0,0)
t.begin_fill()
t.circle(6)
t.end_fill()

main(247,293)
t.color(205,205,0)
t.circle(19)

main(241,305)
t.color(255,0,0)
t.circle(1)

main(251,305)
t.color(255,0,0)
t.circle(1)

main(246,315)
t.color(255,0,0)
t.circle(1)


main(448,293)
t.color(205,205,0)
t.circle(19)

main(442,305)
t.color(255,0,0)
t.circle(1)

main(453,305)
t.color(255,0,0)
t.circle(1)

main(448,315)
t.color(255,0,0)
t.circle(1)

main(330,303)
t.color(205,205,0)
t.begin_fill()
t.fillcolor(254,255,5)

t.setheading(90)
t.fd(46)
t.right(20)
t.circle(-20,150)
t.setheading(-90)
t.fd(41)
t.right(90)
t.fd(1)
t.end_fill()
main(0,0)

main(321,280)
t.color(166,166,166)
t.pensize(5)
t.fillcolor(255,255,255)
t.begin_fill()
t.right(90)
t.fd(22)
t.left(90)
t.fd(27)
t.left(90)
t.fd(22)
t.end_fill()

main(347,280)
t.color(166,166,166)
t.pensize(5)
t.fillcolor(255,255,255)
t.begin_fill()
t.right(90)
t.fd(22)
t.left(90)
t.fd(27)
t.left(90)
t.fd(22)
t.end_fill()

main(0,0)

t.pensize(3)
t.color(140,140,0)
main(275,310)
t.right(40)
t.circle(120,76)

t.pensize(2)
main(275,310)
t.right(45)
t.circle(110,85)

t.pensize(2)
main(275,310)
t.right(42)
t.circle(117,80)

#窗户
main(525,290)
t.begin_fill()
t.color(130,138,138)
t.fillcolor(171,171,171)
t.circle(43)
t.end_fill()

main(525,300)
t.begin_fill()
t.color(130,138,138)
t.fillcolor(147,207,187)
t.circle(33)
t.end_fill()

main(710,190)
t.begin_fill()
t.color(130,138,138)
t.fillcolor(171,171,171)
t.circle(43)
t.end_fill()

main(710,200)
t.begin_fill()
t.color(130,138,138)
t.fillcolor(147,207,187)
t.circle(33)
t.end_fill()


#红领带
main(325,200)
t.pensize(5)
t.color(132,27,32)
t.fillcolor(231,32,26)
t.begin_fill()
t.fd(47)
t.right(150)
t.fd(27)
t.right(60)
t.fd(27)
t.end_fill()

main(331,160)
t.pensize(5)
t.color(132,27,32)
t.fillcolor(231,32,26)
t.setheading(-20)
t.begin_fill()
t.right(35)
t.fd(30)
t.left(110)
t.fd(30)
t.left(70)
t.fd(30)
t.left(110)
t.fd(30)
t.end_fill()


#文字
main(280,510)
t.color('black')
t.write("不管你是否愿意相信,在美丽的比基尼海滩,", move=False, align="center", font=("KaiTi", 18,"bold"))
main(220,480)
t.write("每个岩洞下面,都藏着一个奇迹。", move=False, align="center", font=("KaiTi", 18,"bold"))

main(574,50)
t.write("能给宝宝一个   嘛?", move=False, align="center", font=("KaiTi", 18,"bold"))
main(574,24)
t.write("源码:yoyling点com", move=False, align="center", font=("KaiTi", 18,"bold"))

main(630,50)
t.color('red')
t.write("♥", move=False, align="center", font=("KaiTi", 30,"bold"))

main(0,0)

本文由 RawChen 发表, 最后编辑时间为:2021-03-22 00:13
如果你觉得我的文章不错,不妨鼓励我继续写作。

发表评论
选择表情
  1. 你真闲哈哈哈哈。

       Windows 10   Chrome 110
  2. 不错

       Android   Chrome 92
  3. 文章不错非常喜欢

       Windows 7   Chrome 78
Top