2.海龟作图----用Python绘图

2.1 第一个海龟程序

# SquareSpiral1.py 画一个正方形螺旋线

import turtle

t= turtle.Turtle()
for x in range(1,100):  #1<=x<100
    t.forward(x)
    t.left(90)

2.2 旋转的海龟

python内建函数参考

import turtle

t=turtle.Turtle()
for x in range(1,100):  #1<=x<100
    t.forward(x)
    t.left(91)

2.3 海龟画圆

#CircleSpiral1.py

import turtle as tt

t=tt.Turtle()

for x in range(100):

    t.circle(x)
    t.left(91)

2.4 添加颜色

  • 添加红色
#SquareSpiral3.py

import turtle

t=turtle.Turtle()
t.pencolor("red")

for x in range(100):

    t.forward(x)
    t.left(91)

一个四色螺旋线

#ColorSquareSpiral.py

import turtle

t = turtle.Pen()
colors = ["red", "yellow", "blue", "green"]

for x in range(100):

    t.pencolor(colors[x%4])
    t.forward(x)
    t.left(91)
  • 修改背景色
#ColorSquareSpiral2.py

import turtle

t=turtle.Pen()

turtle.bgcolor('black')   #修改背景色

colors=['red', 'yellow', 'blue', 'green']

for x in range(200):

    t.pencolor(colors[x%4])

    t.forward(x)

    t.left(91)

2.5 一个变量搞定一切

# ColorSpiral.py,修改sides,得到不同边数的螺旋线

import turtle

t = turtle.Pen()

turtle.bgcolor("black")

# You can choose between 2 and 6 sides for some cool shapes!

sides = 6

colors = ["red", "yellow", "blue", "orange", "green", "purple"]

for x in range(360):

    t.pencolor(colors[x%sides])

    t.forward(x * 3/sides + x)

    t.left(360/sides + 1)

    t.width(x*sides/200)  #海龟钢笔的宽度

2.6 本章应掌握的知识和技能

  • 用Turtle库绘制简单的图形
  • 使用变量来存储简单的数值和字符串
  • 在IDLE中修改、保存、运行程序
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值