用turtle库绘制奥特曼

1. 导入库

import turtle as t
  • 这行代码导入了turtle库,并将其命名为t,以便在后续代码中更方便的调用。

2. 设置画布大小和颜色

t.setup(500, 600)
t.color('black')
  • t.setup(500, 600)设置画布的宽度为500像素,高度为600像素。
  • t.color('black')将画笔的颜色设置为黑色。

3. 定义跳转函数

def jump(x, y):
    t.pu()  # 提起画笔
    t.goto(x, y)  # 移动到指定坐标
    t.pd()  # 落下画笔
  • 这个函数用于将画笔移动到指定的坐标(x, y)而不绘制轨迹。在绘制复杂图形时非常有用。

4. 绘制图形的各个部分

4.1 画圆形及其它图形
jump(-23, -68)
t.pensize(3)
t.circle(80)
  • jump函数将画笔移动到(-23, -68),然后设置画笔的粗细为3,接着绘制半径为80的圆。

接下来的代码部分通过重复调用jumpt.circle等方法,逐步绘制了多种形状。由于这些指令组合复杂,代码看似杂乱,但遵循相同的模式进行跳转和绘制。

4.2 填充颜色
t.fillcolor('#CB2725')
t.begin_fill()
t.circle(100, 15)
...
t.end_fill()
  • t.fillcolor()设定填充颜色,再用begin_fill()end_fill()包裹绘制指令,使指定区域被填充指定颜色。

5. 更多绘制和填充

代码继续通过多次调用jumpt.circle完成更多形状的绘制和颜色的填充。在这部分中,还有一些对setheading()方法的调用,用于改变画笔的方向,这使得图形更加复杂和丰富。

6. 最后的绘制和隐藏画笔

t.hideturtle()
t.done()
  • t.hideturtle()使得画笔在绘制完成后隐藏,以便清晰查看最终图形。
  • t.done()结束绘图,保持窗口开启。

 完整代码:

import turtle as t

# 设置画布大小和颜色
t.setup(500, 600)
t.color('black')


def jump(x, y):
    # 移动到指定坐标,不绘制轨迹
    t.pu()
    t.goto(x, y)
    t.pd()


# 绘制图形的各个部分
jump(-23, -68)
t.pensize(3)
t.circle(80)
jump(-100, 32)
t.circle(-25, -180)
jump(56, 29)
t.circle(25, -185)

jump(-44, 100)
t.setheading(90)
t.circle(-21, 180)
t.circle(-200, 20)
t.circle(-10, 140)
t.circle(-200, 20)

jump(-58, -60)
t.left(125)
t.circle(100, 50)

# 填充颜色
t.fillcolor('#CB2725')
t.begin_fill()
t.circle(100, 15)
t.circle(10, 180)
t.goto(-76, -137)
t.goto(-96, -123)
t.end_fill()

jump(-72, -96)
t.setheading(255)
t.circle(200, 30)
t.circle(15, 150)
t.fd(25)

jump(24, -97)
t.setheading(285)
t.circle(-200, 30)
t.circle(-15, 150)
t.fd(25)

jump(-77, -132)
t.setheading(270)
t.fillcolor('#CB2725')
t.begin_fill()
t.circle(53, 180)
t.goto(-77, -132)
t.end_fill()

jump(-60, -61)
t.setheading(270)
t.fillcolor('#CB2725')
t.begin_fill()
t.circle(35, 180)
t.end_fill()

jump(12, -63)
t.setheading(0)
t.circle(100, 20)

t.fillcolor('#CB2725')
t.begin_fill()
t.circle(100, 15)
t.circle(-15, 160)
t.circle(-120, 20)
t.end_fill()

t.circle(-120, 20)

jump(-62, -17)
t.seth(0)
t.fillcolor('#D1AF2C')
t.begin_fill()
t.circle(25)
t.end_fill()

jump(17, -17)
t.fillcolor('#D1AF2C')
t.begin_fill()
t.circle(25)
t.end_fill()

jump(113, 159)
t.seth(0)
t.color('red')
t.fillcolor('red')
t.begin_fill()
t.seth(45)
t.circle(-40, 180)
t.fd(2 * 40)
t.seth(135)
t.fd(2 * 40)
t.seth(135)
t.circle(-40, 180)
t.end_fill()

jump(114, 45)
t.goto(89, -47)

jump(-35, -100)
t.fillcolor('white')
t.begin_fill()
t.circle(10)
t.end_fill()
t.hideturtle()

# 完成绘制
t.done()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值