用Python的Turtle画哆啦A梦

这是我几年前为了练习python的turtle库而画的,今天翻出了代码,分享给大家。

这是我初学python时画的,当时还没有面向对象的概念,也没有采取类方法之类,纯原始手工,供大家参考。

若有兴趣可以自行优化简洁代码,有时间我也会重新写一遍。

画出来的效果如下图:

代码如下:

# * -- utf-8 -- *
# Author: Tang

import turtle as t

t.speed(10)
t.pensize(8)
t.hideturtle()
t.screensize(500, 500, bg='white')

# 猫脸
t.fillcolor('#00A1E8')
t.begin_fill()
t.circle(120)
t.end_fill()

t.pensize(3)
t.fillcolor('white')
t.begin_fill()
t.circle(100)
t.end_fill()

t.pu()
t.home()
t.goto(0, 134)
t.pd()
t.pensize(4)
t.fillcolor("#EA0014")
t.begin_fill()
t.circle(18)
t.end_fill()

t.pu()
t.goto(7, 155)
t.pensize(2)
t.color('white', 'white')
t.pd()
t.begin_fill()
t.circle(4)
t.end_fill()

t.pu()
t.goto(-30, 160)
t.pensize(4)
t.pd()
t.color('black', 'white')
t.begin_fill()
a = 0.4
for i in range(120):
    if 0 <= i < 30 or 60 <= i < 90:
        a = a+0.08
        t.lt(3) #向左转3度
        t.fd(a) #向前走a的步长
    else:
        a = a-0.08
        t.lt(3)
        t.fd(a)
t.end_fill()

t.pu()
t.goto(30, 160)
t.pensize(4)
t.pd()
t.color('black', 'white')
t.begin_fill()
for i in range(120):
    if 0 <= i < 30 or 60 <= i < 90:
        a = a+0.08
        t.lt(3)  # 向左转3度
        t.fd(a)  # 向前走a的步长
    else:
        a = a-0.08
        t.lt(3)
        t.fd(a)
t.end_fill()

t.pu()
t.goto(-38,190)
t.pensize(8)
t.pd()
t.right(-30)
t.forward(15)
t.right(70)
t.forward(15)

t.pu()
t.goto(15, 185)
t.pensize(4)
t.pd()
t.color('black', 'black')
t.begin_fill()
t.circle(13)
t.end_fill()

t.pu()
t.goto(13, 190)
t.pensize(2)
t.pd()
t.color('white', 'white')
t.begin_fill()
t.circle(5)
t.end_fill()

t.pu()
t.home()
t.goto(0, 134)
t.pensize(4)
t.pencolor('black')
t.pd()
t.right(90)
t.forward(40)

t.pu()
t.home()
t.goto(0, 124)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(10)
t.forward(80)

t.pu()
t.home()
t.goto(0, 114)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(6)
t.forward(80)

t.pu()
t.home()
t.goto(0,104)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(0)
t.forward(80)

# 左边的胡子
t.pu()
t.home()
t.goto(0,124)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(170)
t.forward(80)

t.pu()
t.home()
t.goto(0, 114)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(174)
t.forward(80)

t.pu()
t.home()
t.goto(0, 104)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(180)
t.forward(80)

t.pu()
t.goto(-70, 70)
t.pd()
t.color('black', 'red')
t.pensize(6)
t.seth(-60)
t.begin_fill()
t.circle(80,40)
t.circle(80,80)
t.end_fill()

t.pu()
t.home()
t.goto(-80,70)
t.pd()
t.forward(160)

t.pu()
t.home()
t.goto(-50,50)
t.pd()
t.pensize(1)
t.fillcolor("#eb6e1a")
t.seth(40)
t.begin_fill()
t.circle(-40, 40)
t.circle(-40, 40)
t.seth(40)
t.circle(-40, 40)
t.circle(-40, 40)
t.seth(220)
t.circle(-80, 40)
t.circle(-80, 40)
t.end_fill()

# 领带
t.pu()
t.goto(-70, 12)
t.pensize(14)
t.pencolor('red')
t.pd()
t.seth(-20)
t.circle(200, 30)
t.circle(200, 10)

# 铃铛
t.pu()
t.goto(0, -46)
t.pd()
t.pensize(3)
t.color("black", '#f8d102')
t.begin_fill()
t.circle(25)
t.end_fill()


t.pu()
t.goto(-5, -40)
t.pd()
t.pensize(2)
t.color("black", '#79675d')
t.begin_fill()
t.circle(5)
t.end_fill()

t.pensize(3)
t.right(115)
t.forward(7)

t.mainloop()

转载请注明出处

  • 36
    点赞
  • 166
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
当然可以!以下是用Pythonturtle模块来哆啦A梦的示例代码: ```python import turtle # 创建布和笔 canvas = turtle.Screen() pen = turtle.Turtle() # 设置笔的初始位置和角度 pen.penup() pen.goto(-100, 100) pen.pendown() pen.setheading(0) # 设置笔的颜色和线条宽度 pen.color('blue') pen.pensize(3) # 哆啦A梦的头部 pen.circle(100) # 哆啦A梦的眼睛 pen.penup() pen.goto(-50, 150) pen.pendown() pen.circle(10) pen.penup() pen.goto(-150, 150) pen.pendown() pen.circle(10) # 哆啦A梦的胡须 pen.penup() pen.goto(-100, 100) pen.pendown() pen.setheading(-30) pen.circle(80, 60) pen.penup() pen.goto(-100, 100) pen.pendown() pen.setheading(-150) pen.circle(80, 60) pen.penup() pen.goto(-100, 100) pen.pendown() pen.setheading(180) pen.circle(80, 60) # 哆啦A梦的身体 pen.penup() pen.goto(-100, 100) pen.pendown() pen.setheading(-90) pen.forward(150) pen.setheading(0) pen.circle(50, 180) pen.setheading(90) pen.forward(150) # 哆啦A梦的手和脚 pen.penup() pen.goto(-100, -50) pen.pendown() pen.setheading(-90) pen.forward(50) pen.setheading(-180) pen.forward(50) pen.setheading(90) pen.forward(50) pen.setheading(0) pen.circle(50, 90) # 哆啦A梦的口袋 pen.penup() pen.goto(-50, -50) pen.pendown() pen.setheading(0) pen.forward(100) pen.setheading(-90) pen.forward(40) pen.setheading(-180) pen.forward(100) # 哆啦A梦的铃铛 pen.penup() pen.goto(-50, -150) pen.pendown() pen.setheading(0) pen.circle(20) # 结束绘制 turtle.done() ``` 运行以上代码,即可在布上看到绘制出的哆啦A梦图案。希望对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值