当前位置:   article > 正文

使用Python的Turtle库绘制草莓熊

使用Python的Turtle库绘制草莓熊
引言

Turtle库是Python标准库中一个非常有趣且实用的模块,它主要用于绘制图形和动画。Turtle图形学源于Logo语言,是一种基于命令的绘图方式。通过控制一个名为“海龟”的虚拟角色,在屏幕上移动和绘制,Turtle库可以轻松地教授基础编程概念,同时也能创造出令人惊叹的视觉艺术作品。本文将介绍如何使用Turtle库创建一个紫色小熊与风车的动态画面,包括Turtle库的基本使用,代码开发思路,以及重点代码的详细解释。

Turtle库简介

Turtle库提供了许多函数,如forward(), backward(), left(), right()等,用于控制海龟的移动方向和距离;penup(), pendown()用于控制是否在移动时绘制线条;pencolor(), fillcolor(), pensize()用于设置线条颜色、填充颜色和线条粗细。tracer()可以控制动画的刷新速度,而screensize(), setup(), title()则用于配置绘图窗口的大小、位置和标题。

开发思路

我们的目标是创建一个紫色小熊在旁边玩着风车的动画场景。首先,我们需要设定绘图区域的大小、背景颜色,以及调整绘图速度。接着,我们将定义两个主要函数:drawXiaoXiong()用于绘制小熊,fongche()用于绘制风车。最后,我们使用rotate()函数来实现风车的动态旋转效果。

代码运行效果

在这里插入图片描述

完整代码
import turtle as t
t.colormode(255)  # 颜色模式
t.speed(0)
t.screensize(850, 760, "white")  # 画布大小背景颜色
t.setup(width=850, height=760, startx=None, starty=None)  # 绘图窗口的大小和起始坐标
# 设置绘图窗口的标题
t.title("紫色小熊玩风车")
t.resizemode('noresize')
t.tracer(1)

scolor = ["#E6005C", "#00BFFF", "#538a30", "#F28500"]
qcolor = ["#FF007F", "#87CEFA", "#7fbc2b", "#FFA500"]
blsize = 80
bs = 2 ** 0.5 / 2 * blsize
zjb = blsize / 2
zjsjxxb = 2 ** 0.5 * zjb
length = 1.7 * blsize
width = 2 / 15 * blsize


def jump(x, y):
    t.penup()
    t.goto(x, y)
    t.pendown()


def rotate():
    drawXiaoXiong()
    # 画风车
    fongche()

    t.ontimer(rotate, t=100)  # 每隔100毫秒转一次
# 风车
def fongche():
    t.penup()
    t.goto(-205, -42)
    t.begin_fill()
    t.pensize(4)
    t.pencolor("#321320")
    t.fillcolor("#D2B48C")
    t.circle(15)
    t.end_fill()
    jump(-220, 80)
    t.setheading(-90)
    t.pensize(width)
    t.pencolor("#5f4a1d")
    t.forward(length)
    t.pensize(2)
    t.backward(length)
    t.setheading(90)

    for i in range(4):
        # 小等腰直角三角形
        t.color(scolor[i])
        t.begin_fill()
        t.forward(zjb)
        t.left(90)
        t.forward(zjb)
        t.left(135)
        t.forward(zjsjxxb)
        t.end_fill()
        # 大等腰直角三角形
        t.color(qcolor[i])
        t.begin_fill()
        t.backward(zjsjxxb)
        t.right(90)
        t.forward(bs)
        t.left(135)
        t.forward(blsize)
        t.end_fill()
        # 旋转180度后,画下一片风车叶片
        t.right(180)
        t.penup()


mling_circle_list = iter([  # 每段弧线(半径,弧角度数)
    (18, 360), (14, 360), (10, 360), (6, 360),
    (18, 360), (14, 360), (10, 360), (6, 360),
])


def mling_draw_eyeball(zb1, zb2, zb3, zb4):
    for zb, color_ in zip([zb1, zb2, zb3, zb4], ['#ffffff', '#482d08', '#000000', '#ffffff']):
        t.penup()
        t.goto(*zb)
        t.pendown()
        t.begin_fill()
        t.setheading(0)
        t.color(color_)
        t.pencolor('#000000')
        t.pensize(2)
        t.circle(*next(mling_circle_list))
        t.end_fill()

def drawXiaoXiong():
    t.penup()
    t.home()
    t.pencolor("#321320")
    t.fillcolor("#cb3263")
    t.pensize(4)
    t.goto(120, 110)
    t.pendown()
    t.begin_fill()
    t.goto(200, 0)
    t.left(-40)
    t.circle(-110, 105)
    t.left(75)
    t.goto(170, -110)
    t.left(-80)
    t.circle(30, 40)
    t.left(60)
    t.circle(-80, 70)
    t.left(83)
    t.circle(-35, 95)
    t.goto(60, -270)
    t.left(-80)
    t.circle(-65, 70)
    t.left(63)
    t.circle(35, 30)
    t.left(130)
    t.circle(-65, 70)
    t.goto(-120, -270)
    t.left(-110)
    t.circle(-35, 80)
    t.left(83)
    t.circle(-80, 50)
    t.left(60)
    t.circle(-80, 60)
    t.left(60)
    t.circle(30, 30)
    t.left(20)
    t.circle(80, 80)
    t.left(-105)
    t.circle(-70, 150)
    t.left(50)
    t.circle(-170, 50)
    t.goto(120, 110)
    t.end_fill()
    t.penup()
    p = t.home()
    t.pencolor("#321320")
    t.fillcolor("#ffffff")
    t.pensize(4)
    t.goto(90, 60)
    t.pendown()
    t.begin_fill()
    t.right(30)
    t.circle(-130, 360)
    t.end_fill()
    t.penup()
    p = t.home()
    t.pencolor("#321320")
    t.fillcolor("#f3d2ad")
    t.pensize(4)
    t.goto(-250, -55)
    t.seth(0)
    t.pendown()
    t.begin_fill()
    t.right(-55)
    t.circle(-45, 270)
    t.goto(-220, -75)
    t.goto(-250, -55)
    t.end_fill()

    t.penup()
    t.home()
    t.pencolor("#321320")
    t.fillcolor("#f3d2ad")
    t.pensize(4)
    t.goto(185, -90)
    t.pendown()
    t.begin_fill()
    t.right(140)
    t.circle(43, 95)
    t.goto(185, -90)
    t.end_fill()
    t.penup()
    t.seth(0)
    t.pencolor('#321320')
    t.fillcolor('#cb3263')
    t.pensize(4)
    t.begin_fill()
    t.goto(21, 0)
    t.pendown()
    t.circle(123, 134)
    t.left(-90)
    t.circle(40, 185)
    t.left(-60)
    t.circle(120, 60)
    t.left(-90)
    t.circle(50, 200)
    t.left(-90)
    t.circle(100, 100)
    t.left(-12)
    t.circle(100, 40)
    t.goto(21, 0)
    t.penup()
    t.end_fill()
    t.penup()
    t.goto(0, 0)
    t.seth(0)
    t.pencolor('#321320')
    t.fillcolor('#ffffff')
    t.pensize(4)
    t.begin_fill()
    t.goto(-70, 210)
    t.left(140)
    t.pendown()
    t.circle(30, 200)
    t.goto(-70, 210)
    t.penup()
    t.end_fill()
    t.penup()
    t.goto(0, 0)
    t.seth(0)
    t.pencolor('#321320')
    t.fillcolor('#ffffff')
    t.pensize(4)
    t.begin_fill()
    t.goto(90, 220)
    t.left(45)
    t.pendown()
    t.circle(22, 200)
    t.goto(90, 220)
    t.penup()
    t.end_fill()
    t.penup()
    t.goto(0, 0)
    t.seth(0)
    t.pencolor('#321320')
    t.fillcolor('#ffffff')
    t.pensize(4)
    t.begin_fill()
    t.left(-98)
    t.left(90)
    t.goto(18, 10)
    t.pendown()
    t.circle(100, 134)
    t.left(10)
    t.circle(110, 30)
    t.left(10)
    t.circle(160, 40)
    t.circle(85, 40)
    t.left(2)
    t.circle(95, 40)
    t.left(5)
    t.circle(95, 60)
    t.goto(18, 10)
    t.penup()
    t.end_fill()
    t.penup()
    t.home()
    t.pencolor("#321320")
    t.fillcolor("#8f3a52")
    t.pensize(2)
    t.goto(25, 240)
    t.pendown()
    t.begin_fill()
    t.goto(60, 235)
    t.left(30)
    t.fd(8)
    t.left(90)
    t.fd(22)
    t.circle(90, 8)
    t.left(20)
    t.circle(90, 8)
    t.left(20)
    t.circle(90, 20)
    t.left(40)
    t.circle(50, 20)
    t.end_fill()
    t.penup()
    t.pensize(12)
    t.goto(-2, 250)
    t.pencolor("#4D1F00")
    t.fillcolor("#4D1F00")
    t.pendown()
    t.goto(60, 240)
    t.end_fill()
    t.penup()
    t.home()
    t.pencolor("#321320")
    t.fillcolor("#8f3a52")
    t.pensize(2)
    t.goto(-55, 193)
    t.pendown()
    t.begin_fill()
    t.left(65)
    t.circle(-90, 25)
    t.goto(-10, 230)
    t.left(30)
    t.fd(8)
    t.left(90)
    t.fd(18)
    t.circle(90, 8)
    t.left(20)
    t.circle(90, 10)
    t.left(40)
    t.circle(90, 30)
    t.left(30)
    t.circle(40, 20)
    t.penup()
    t.end_fill()
    t.pensize(12)
    t.goto(-63, 195)
    t.pencolor("#4D1F00")
    t.fillcolor("#4D1F00")
    t.pendown()
    t.left(100)
    t.circle(-85, 45)
    t.end_fill()

    mling_draw_eyeball((-20, 180), (-23, 185), (-25, 188), (-30, 200))
    mling_draw_eyeball((30, 193), (27, 200), (25, 203), (20, 213))

    t.penup()
    t.home()
    t.pencolor("#321320")
    t.fillcolor("#8f3a52")
    t.pensize(3)
    t.goto(25, 105)
    t.pos()
    t.pendown()
    t.begin_fill()
    t.circle(85, 65)
    t.left(16)
    t.circle(30, 55)
    t.left(20)
    t.circle(145, 58)
    t.left(8)
    t.circle(20, 55)
    t.left(8)
    t.circle(50, 65)
    t.left(-5)
    t.circle(310, 8)
    t.end_fill()
    t.penup()
    t.goto(0, 0)
    t.seth(0)
    t.pencolor('#321320')
    t.fillcolor('#a93e54')
    t.pensize(3)
    t.begin_fill()
    t.left(-20)
    t.goto(9, 66)
    t.pendown()
    t.circle(68, 40)
    t.left(10)
    t.circle(65, 40)
    t.left(160)
    t.circle(-75, 85)
    t.left(158)
    t.circle(48, 37)
    t.goto(9, 66)
    t.penup()
    t.end_fill()
    t.color('#321320')
    t.penup()
    t.goto(260, 60)
    t.pendown()



if __name__ == "__main__":
    drawXiaoXiong()
    # 画风车
    fongche()
    t.hideturtle()
    t.done()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
重点代码详细解释
  • jump(x, y)函数:用于使海龟跳转到指定的坐标位置,不留下轨迹。
  • fongche()函数:负责风车的绘制,包括中心圆点、支撑杆和四片不同颜色的叶片。
  • drawXiaoXiong()函数:包含复杂的路径绘制逻辑,用于绘制小熊的身体、耳朵、眼睛等部位。
  • rotate()函数:使用ontimer()函数实现风车的周期性旋转,创建动态效果。
总结

通过本文,我们不仅学习了如何使用Turtle库来绘制复杂图形,而且还了解了如何通过编程实现动画效果。Turtle库的使用不仅限于教学,对于那些想要探索计算机图形学和动画制作的初学者来说,它也是一个极好的起点。通过实践,我们可以发现编程不仅仅是一项技能,更是一种创造美的艺术形式。

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/863795
推荐阅读
相关标签
  

闽ICP备14008679号