Python程序教程

您现在的位置是:首页 >  Python

当前栏目

2.海龟作图—-用Python绘图[通俗易懂]

海龟,作图,Python,绘图,通俗易懂
2025-04-11 08:58:06 时间

大家好,又见面了,我是你们的朋友全栈君。

2.1 第一个海龟程序

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

import turtle

t=turtle.Pen()

for x in range(1,100):  #1<=x<100

    t.forward(x)

    t.left(90)

2.2 旋转的海龟

python内建函数参考

#SquareSpiral2.py

import turtle

t=turtle.Pen()

for x in range(100): # 0<=x<100

    t.forward(x)

    t.left(91)

2.3 海龟画圆

#CircleSpiral1.py

import turtle

t=turtle.Pen()

for x in range(100):

    t.circle(x)

    t.left(91)

2.4 添加颜色

  • 添加红色
#SquareSpiral3.py

import turtle

t = turtle.Pen()

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中修改、保存、运行程序

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/154100.html原文链接:https://javaforall.cn