使用python画一只佩奇

时间:2023-03-09 06:44:39
使用python画一只佩奇
  1. 打开界面:

打开python shell界面。

使用python画一只佩奇

  1. 建立脚本:

单击"file"——"new file"来建立脚本。

使用python画一只佩奇

  1. 编写代码:

具体的代码如下。

  1. import turtle as t  
  2. ''''' 
  3. t.pu()  提起画笔 
  4. t.pd()  移动时绘制图形,缺省时也为绘制 
  5. t.seth  设置当前朝向为angle角度 
  6. t.begin_fill()  准备开始填充图形 
  7. t.color      同时设置pencolor=color1, fillcolor=color2 
  8. t.goto       设置笔的坐标 
  9. t.circle(70,20) 半径 度数 
  10. 15,124,215  乔治裤子颜色外面 
  11. 66,163,242  乔治裤子颜色里面 
  12. 134  196  247  天空的颜色 
  13. 123,245,95 草地的颜色 
  14. 253,6,6  鞋子外面 
  15. 253,70,70  鞋子里面 
  16. 130,119,100 泥坑 
  17. '''  
  18. r_a=0.8  
  19. wight = 1100  
  20. height = 700  
  21. #t.pensize(4)  
  22. t.hideturtle()  
  23. t.colormode(255)  
  24. t.color((255,155,192),"pink")  
  25. t.setup(wight,height)  
  26. t.speed(10)  
  27. def move_pen(x,y):  
  28.     t.pu()  
  29.     t.goto(x-wight/2+50,y-height/2+50)  
  30.     t.pd()  
  31. def pen_set(size,r1,g1,b1,r2=0,g2=0,b2=0):  
  32.     t.pensize(size)  
  33.     t.color((r1,g1,b1),(r2,g2,b2))  
  34. def draw_grid():  
  35.     pen_set(1,0,0,0,0,0,0)  
  36.     for i in range(20):  
  37.         move_pen(0+i*50,0)  
  38.         t.seth(90)  
  39.         t.fd(600)  
  40.     for i in range(12):  
  41.         move_pen(0,0+i*50)  
  42.         t.seth(0)  
  43.         t.fd(1000)  
  44. def draw_bg():  
  45.     #画草地  
  46.     move_pen(0,350)  
  47.     pen_set(4,123,245,95, 123,245,95)  
  48.     t.begin_fill()  
  49.     t.seth(-90)  
  50.     t.fd(350)  
  51.     t.seth(0)  
  52.     t.fd(1000)  
  53.     t.seth(90)  
  54.     t.fd(350)  
  55.     t.end_fill()  
  56.     #画天空  
  57.     move_pen(0,350)  
  58.     pen_set(4,134,196,247, 134,196,247)  
  59.     t.begin_fill()  
  60.     t.seth(90)  
  61.     t.fd(250)  
  62.     t.seth(0)  
  63.     t.fd(1000)  
  64.     t.seth(-90)  
  65.     t.fd(250)  
  66.     a=-180 + r_a  
  67.     for i in range(50):  
  68.         a = a - r_a/50  
  69.         t.seth(a)  
  70.         t.fd(500/50)  
  71.     a =180  
  72.     for i in range(50):  
  73.         a = a - r_a/50  
  74.         t.seth(a)  
  75.         t.fd(500/50)  
  76.     t.end_fill()  
  77. def draw_mud_pit():  
  78.     #画泥坑  
  79.     pen_set(5,130,119,100, 130,119,100)  
  80.     move_pen(350,150)  
  81.     t.begin_fill()  
  82.     t.seth(-180)  
  83.     t.circle(50,125)  
  84.     t.seth(-20)  
  85.     t.circle(350,60)  
  86.     t.seth(20)  
  87.     t.circle(50,30)  
  88.     t.seth(10)  
  89.     t.circle(50,30)  
  90.     t.seth(0)  
  91.     t.circle(50,30)  
  92.     t.seth(40)  
  93.     t.circle(50,90)  
  94.     t.seth(170)  
  95.     t.circle(500,45)  
  96.     t.end_fill()  
  97. def draw_shoes():  
  98.     pen_set(3,253,6,6, 253,70,70)  
  99.     move_pen(400,100)  
  100.     t.begin_fill()  
  101.     t.seth(0)  
  102.     t.fd(50)  
  103.     t.seth(87)  
  104.     t.fd(50)  
  105.     t.seth(180)  
  106.     t.fd(25)  
  107.     t.seth(-93)  
  108.     t.fd(20)  
  109.     t.seth(-180)  
  110.     t.fd(25)  
  111.     t.seth(-120)  
  112.     t.circle(45,38)  
  113.     t.end_fill()  
  114.     move_pen(470,100)  
  115.     t.begin_fill()  
  116.     t.seth(0)  
  117.     t.fd(50)  
  118.     t.seth(87)  
  119.     t.fd(50)  
  120.     t.seth(180)  
  121.     t.fd(25)  
  122.     t.seth(-93)  
  123.     t.fd(20)  
  124.     t.seth(-180)  
  125.     t.fd(25)  
  126.     t.seth(-120)  
  127.     t.circle(45,38)  
  128.     t.end_fill()  
  129. def draw_leg():   
  130.     pen_set(6,255,155,192, 255,155,192)  
  131.     move_pen(440,140)  
  132.     t.seth(90)  
  133.     t.fd(20)  
  134.     move_pen(510,140)  
  135.     t.seth(90)  
  136.     t.fd(20)  
  137. def draw_trousers():  
  138.     move_pen(400,300)  
  139.     pen_set(6,15,124,215, 66,163,242)  
  140.     t.begin_fill()  
  141.     d_a = 100  
  142.     a=-130   
  143.     for i in range(60):  
  144.         a = a + 2  
  145.         t.seth(a)  
  146.         t.fd(3)  
  147.     for i in range(14):  
  148.         a = a + 0.02  
  149.         t.seth(a)  
  150.         t.fd(2)  
  151.     a = 0-a  
  152.     for i in range(14):  
  153.         a = a + 0.02  
  154.         t.seth(a)  
  155.         t.fd(2)  
  156.     for i in range(60):  
  157.         a = a + 2.2  
  158.         t.seth(a)  
  159.         t.fd(3)  
  160.     t.end_fill()  
  161. def draw_tile():  
  162.     move_pen(550,177)  
  163.     pen_set(6,255,155,192, 255,155,192)  
  164.     a=-60   
  165.     for i in range(25):  
  166.         a = a + 4  
  167.         t.seth(a)  
  168.         t.fd(1)  
  169.     t.circle(5)  
  170.     a = -a  
  171.     for i in range(30):  
  172.         a = a + 4  
  173.         t.seth(a)  
  174.         t.fd(1)  
  175. def draw_hands():  
  176.     move_pen(550,250)  
  177.     pen_set(6,255,155,192, 255,155,192)  
  178.     t.seth(20)  
  179.     t.fd(70)  
  180.     move_pen(600,270)  
  181.     t.seth(60)  
  182.     t.fd(20)  
  183.     move_pen(600,270)  
  184.     t.seth(-20)  
  185.     t.fd(20)  
  186.     move_pen(380,250)  
  187.     t.seth(160)  
  188.     t.fd(50)  
  189.     move_pen(350,260)  
  190.     t.seth(100)  
  191.     t.fd(20)  
  192.     move_pen(350,260)  
  193.     t.seth(-140)  
  194.     t.fd(20)  
  195. def draw_face():  
  196.     move_pen(400,360)  
  197.     pen_set(4,255,155,192, 255,196,218)  
  198.     t.begin_fill()  
  199.     a=-120   
  200.     for i in range(20):  
  201.         a = a + 2.5  
  202.         t.seth(a)  
  203.         t.fd(2.2)  
  204.     for i in range(130):  
  205.         a = a + 1.3  
  206.         t.seth(a)  
  207.         t.fd(1.8)  
  208.     for i in range(35):  
  209.         a = a + 1.4  
  210.         t.seth(a)  
  211.         t.fd(2)  
  212.     for i in range(50):  
  213.         a = a + 0.35  
  214.         t.seth(a)  
  215.         t.fd(2)  
  216.     for i in range(50):  
  217.         a = a + 0.2  
  218.         t.seth(a)  
  219.         t.fd(2)  
  220.     n=0.4  
  221.     for i in range(180):  
  222.         if 0<=i<30 or 60<=i<90 or 120<=i<150 :  
  223.             n=n+0.08  
  224. 度  
  225.             t.fd(n) #向前走a的步长  
  226.         else:  
  227.             n=n-0.08  
  228.             t.lt(3)  
  229.             t.fd(n)  
  230.     a=-50  
  231.     for i in range(20):  
  232.         a = a + 2.8  
  233.         t.seth(a)  
  234.         t.fd(5)  
  235.     t.end_fill()  
  236. def draw_other():  
  237.     move_pen(310,440)  
  238.     pen_set(6,255,145,192, 255,145,192)  
  239.     t.begin_fill()  
  240.     t.circle(3)  
  241.     t.end_fill()  
  242.     move_pen(330,430)  
  243.     t.begin_fill()  
  244.     t.circle(3)  
  245.     t.end_fill()  
  246.     pen_set(6,255,145,192, 255,255,255)  
  247.     move_pen(410,425)  
  248.     t.begin_fill()  
  249.     t.circle(10)  
  250.     t.end_fill()  
  251.     move_pen(460,395)  
  252.     t.begin_fill()  
  253.     t.circle(10)  
  254.     t.end_fill()  
  255.     pen_set(6,0,0,0,0,0,0)  
  256.     move_pen(405,429)  
  257.     t.begin_fill()  
  258.     t.circle(3)  
  259.     t.end_fill()  
  260.     move_pen(455,399)  
  261.     t.begin_fill()  
  262.     t.circle(3)  
  263.     t.end_fill()  
  264.     move_pen(510,310)  
  265.     pen_set(6,255,155,192, 255,155,192)  
  266.     t.begin_fill()  
  267.     t.circle(25)  
  268.     t.end_fill()  
  269.     move_pen(410,340)  
  270.     pen_set(6,255,145,192, 255,145,192)  
  271.     a=-80   
  272.     for i in range(20):  
  273.         a = a + 6  
  274.         t.seth(a)  
  275.         t.fd(3)  
  276.     move_pen(430,445)  
  277.     pen_set(4,255,155,192, 255,196,218)  
  278.     t.begin_fill()  
  279.     a=120   
  280.     for i in range(40):  
  281.         a = a - 2  
  282.         t.seth(a)  
  283.         t.fd(1.2)  
  284.     a=-a  
  285.     for i in range(45):  
  286.         a = a - 2  
  287.         t.seth(a)  
  288.         t.fd(1.2)  
  289.     t.end_fill()  
  290.     move_pen(480,430)  
  291.     t.begin_fill()  
  292.     a=70   
  293.     for i in range(40):  
  294.         a = a -1.5  
  295.         t.seth(a)  
  296.         t.fd(1.5)  
  297.     a=-80  
  298.     for i in range(45):  
  299.         a = a -1.5  
  300.         t.seth(a)  
  301.         t.fd(1.5)  
  302.     t.end_fill()  
  303. draw_bg()  
  304. draw_mud_pit()  
  305. #draw_grid()  
  306. draw_leg()  
  307. draw_shoes()  
  308. draw_trousers()  
  309. draw_tile()  
  310. draw_hands()  
  311. draw_face()  
  312. draw_other()  
  313. t.mainloop()  

4.保存脚本:

使用python画一只佩奇

5执行脚本及效果:

使用python画一只佩奇

6.结果:

使用python画一只佩奇