乌龟绘图小游戏


前言

乌龟绘图是一个Python标准库,提供了一种简单的方式来使用图形化窗口进行绘图。本文简单介绍了乌龟绘图的使用方法


一、乌龟绘图是什么?

乌龟绘图是一种基于Python编程语言的绘图工具,它提供了一个简单而强大的绘图窗口,可以在其中创建各种图形和动画。

乌龟绘图的核心概念是一个“乌龟”的概念,这个乌龟可以在绘图窗口中移动并画出各种形状。通过简单的指令,我们可以让乌龟前进、后退、转向、改变颜色等。

乌龟绘图非常适合初学者学习编程和图形设计,因为它的语法简单易懂,并且可以实时看到绘图结果。通过乌龟绘图,我们可以创建各种有趣的图形,如正方形、圆形、螺旋线等。

乌龟绘图还可以与其他Python库和模块结合使用,从而实现更加复杂的图形和动画效果。它可以作为学习Python编程和创作艺术作品的一种有趣工具。

二、使用方法

在安装好的python文件夹下有一些乌龟绘图的示例模板,我们可以通过这些实力模板进行模仿学习制作自己的绘图。

示例模板存放在python文件夹下Lib \ turtledemo

下面以其中yinyang为模板进行学习,下面为其全部代码

1.yinyang模板

代码如下(示例):

#!/usr/bin/env python3
"""       turtle-example-suite:

            tdemo_yinyang.py

Another drawing suitable as a beginner's
programming example.

The small circles are drawn by the circle
command.

"""

from turtle import * # 模板导入

def yin(radius, color1, color2):
    width(3) # 画笔宽度
    color("black", color1) # 画笔颜色
    begin_fill() # 开始填充
    circle(radius/2., 180)
    circle(radius, 180)
    left(180) # 画笔向左旋转180circle(-radius/2., 180) # 画圆
    end_fill() # 结束填充
    left(90)
    up() # 抬起画笔
    forward(radius*0.35) # 前进
    right(90)
    down() # 放下画笔
    color(color1, color2)
    begin_fill()
    circle(radius*0.15)
    end_fill()
    left(90)
    up()
    backward(radius*0.35)
    down()
    left(90)

def main():
    reset()
    yin(200, "black", "white")
    yin(200, "white", "black")
    ht()
    return "Done!"

if __name__ == '__main__': 
    main()
    mainloop() # 循环设置(使画板停留,不让其一结束就关闭)

模板结果:
yinyang.py运行结果---太极图

通过上方代码可以总结出:

  • 导入模块 import turtle
  • 指定画笔宽度 turtle.width()
  • 指定画笔颜色 turtle.color(“画笔颜色”, “填充颜色”)
  • 开始填充 turtle.begin_fill()
  • 结束填充 turtle.end_fill()
  • 画一个完整的圆形 turtle.circle(100)
  • 向左旋转x度 turtle.left(x)
  • 向右旋转 turtle.right()
  • 向前爬行100个像素 turtle.forward(100)
  • 抬起画笔 turtle.up()
  • 放下画笔 turtle.down()
  • 设置循环 turtle.mainloop()

2.自己制作

代码如下(示例):

# 导入模块
import turtle
# 指定画笔宽度
turtle.width(15)
# 指定画笔颜色
turtle.color("pink", "light grey")
# 开始填充
turtle.begin_fill()

turtle.left(60)
turtle.forward(100) # 向前爬行100个像素
turtle.right(60)
turtle.forward(100)
turtle.right(60)
turtle.forward(100)
turtle.right(60)
turtle.forward(400)
turtle.right(120)
turtle.forward(400)
turtle.right(60)
turtle.forward(100)
turtle.right(60)
turtle.forward(100)
turtle.right(60)
turtle.forward(100)

#抬起画笔
turtle.up()

turtle.right(30)
turtle.forward(350)

# 放下画笔
turtle.down()

turtle.forward(100)

# 结束填充
turtle.end_fill()

# 设置循环
turtle.mainloop()

运行结果:
结果:爱心


总结

以上就是今天要讲的内容,本文仅仅简单介绍了如何进行乌龟绘图,而python的乌龟绘图包提供了大量的示例模板,我们可以通过这些示例模板进行更进一步的学习。

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值