Python中的海龟编程
“乌龟”是一个类似于绘图板的Python功能,它可以让我们命令乌龟在上面画图!我们可以使用turtle.forward(…) 和turtle.right(…) 之类的函数来移动海龟。常用的海龟方法有:Method Parameter Description Turtle() None Creates and returns a new turtle object forward() amount Moves the turtle forward by the specified amount backward() amount Moves the turtle backward by the specified amount right() angle Turns the turtle clockwise left() angle Turns the turtle counterclockwise penup() None Picks up the turtle’s Pen pendown() None Puts down the turtle’s Pen up() None Picks up the turtle’s Pen down() None Puts down the turtle’s Pen color() Color name Changes the color of the turtle’s pen fillcolor() Color name Changes the color of the turtle will use to fill a polygon heading() None Returns the current heading position() None Returns the current position goto() x, y Move the turtle to position x,y begin_fill() None Remember the starting point for a filled polygon end_fill() None Close the polygon and fill with the current fill color dot() None Leave the dot at the current position stamp() None Leaves an impression of a turtle shape at the current location shape() shapename Should be ‘arrow’, ‘classic’, ‘turtle’ or ‘circle’
使用 Turtle 绘图
为了使用turtle的方法和功能,我们需要导入turtle。“turtle”自带标准Python包,不需要外部安装。执行海龟程序的路线图遵循 4 个步骤:
- 导入海龟模块
- 创建一个海龟来控制。
- 使用海龟方法四处绘制。
- 运行turtle.done()。
所以如上所述,在我们可以使用turtle之前,我们需要导入它。我们将其导入为:
from turtle import *
# or
import turtle
导入海龟库并为我们提供所有海龟功能后,我们需要创建一个新的绘图板(窗口)和一个海龟。我们将窗口称为 wn,将海龟称为 skk。所以我们编码为:
wn = turtle.Screen()
wn.bgcolor("light green")
wn.title("Turtle")
skk = turtle.Turtle()
现在我们已经创建了窗口和海龟,我们需要移动海龟。为了在 skk 所面对的方向上向前移动 100 个像素,我们编写代码:
skk.forward(100)
我们已经将 skk 向前移动了 100 像素,太棒了!现在我们使用 done()函数完成程序,我们完成了!
turtle.done()
因此,我们创建了一个绘制 100 像素长的线的程序。我们可以使用海龟方法绘制各种形状并填充不同的颜色。有大量的函数和程序需要使用Python中的 turtle 库进行编码。让我们学习画一些基本的形状。
形状1:方形
Python
# Python program to draw square
# using Turtle Programming
import turtle
skk = turtle.Turtle()
for i in range(4):
skk.forward(50)
skk.right(90)
turtle.done()
Python3
# Python program to draw star
# using Turtle Programming
import turtle
star = turtle.Turtle()
star.right(75)
star.forward(100)
for i in range(4):
star.right(144)
star.forward(100)
turtle.done()
Python
# Python program to draw hexagon
# using Turtle Programming
import turtle
polygon = turtle.Turtle()
num_sides = 6
side_length = 70
angle = 360.0 / num_sides
for i in range(num_sides):
polygon.forward(side_length)
polygon.right(angle)
turtle.done()
Python
# Python program to draw
# Spiral Square Outside In and Inside Out
# using Turtle Programming
import turtle #Outside_In
wn = turtle.Screen()
wn.bgcolor("light green")
wn.title("Turtle")
skk = turtle.Turtle()
skk.color("blue")
def sqrfunc(size):
for i in range(4):
skk.fd(size)
skk.left(90)
size = size-5
sqrfunc(146)
sqrfunc(126)
sqrfunc(106)
sqrfunc(86)
sqrfunc(66)
sqrfunc(46)
sqrfunc(26)
Python
import turtle #Inside_Out
wn = turtle.Screen()
wn.bgcolor("light green")
skk = turtle.Turtle()
skk.color("blue")
def sqrfunc(size):
for i in range(4):
skk.fd(size)
skk.left(90)
size = size + 5
sqrfunc(6)
sqrfunc(26)
sqrfunc(46)
sqrfunc(66)
sqrfunc(86)
sqrfunc(106)
sqrfunc(126)
sqrfunc(146)
Python
# Python program to user input pattern
# using Turtle Programming
import turtle #Outside_In
import turtle
import time
import random
print ("This program draws shapes based on the number you enter in a uniform pattern.")
num_str = input("Enter the side number of the shape you want to draw: ")
if num_str.isdigit():
squares = int(num_str)
angle = 180 - 180*(squares-2)/squares
turtle.up
x = 0
y = 0
turtle.setpos(x, y)
numshapes = 8
for x in range(numshapes):
turtle.color(random.random(), random.random(), random.random())
x += 5
y += 5
turtle.forward(x)
turtle.left(y)
for i in range(squares):
turtle.begin_fill()
turtle.down()
turtle.forward(40)
turtle.left(angle)
turtle.forward(40)
print (turtle.pos())
turtle.up()
turtle.end_fill()
time.sleep(11)
turtle.bye()
Python
# Python program to draw
# Spiral Helix Pattern
# using Turtle Programming
import turtle
loadWindow = turtle.Screen()
turtle.speed(2)
for i in range(100):
turtle.circle(5*i)
turtle.circle(-5*i)
turtle.left(i)
turtle.exitonclick()
Python
# Python program to draw
# Rainbow Benzene
# using Turtle Programming
import turtle
colors = ['red', 'purple', 'blue', 'green', 'orange', 'yellow']
t = turtle.Pen()
turtle.bgcolor('black')
for x in range(360):
t.pencolor(colors[x%6])
t.width(x//100 + 1)
t.forward(x)
t.left(59)
输出:
形状 2:星形
Python3
# Python program to draw star
# using Turtle Programming
import turtle
star = turtle.Turtle()
star.right(75)
star.forward(100)
for i in range(4):
star.right(144)
star.forward(100)
turtle.done()
输出:
形状3:六边形
Python
# Python program to draw hexagon
# using Turtle Programming
import turtle
polygon = turtle.Turtle()
num_sides = 6
side_length = 70
angle = 360.0 / num_sides
for i in range(num_sides):
polygon.forward(side_length)
polygon.right(angle)
turtle.done()
输出:
访问 pythonturtle.org 来体验 Turtle,而无需预安装Python 。 PythonTurtle 中的 shell 是一个完整的Python shell,您几乎可以使用标准Python shell 做任何事情。您可以制作循环、定义函数、创建类等。
您可以在此处访问这些代码以获取精彩的海龟程序
一些惊人的海龟计划
1. 内外螺旋方形
Python
# Python program to draw
# Spiral Square Outside In and Inside Out
# using Turtle Programming
import turtle #Outside_In
wn = turtle.Screen()
wn.bgcolor("light green")
wn.title("Turtle")
skk = turtle.Turtle()
skk.color("blue")
def sqrfunc(size):
for i in range(4):
skk.fd(size)
skk.left(90)
size = size-5
sqrfunc(146)
sqrfunc(126)
sqrfunc(106)
sqrfunc(86)
sqrfunc(66)
sqrfunc(46)
sqrfunc(26)
Python
import turtle #Inside_Out
wn = turtle.Screen()
wn.bgcolor("light green")
skk = turtle.Turtle()
skk.color("blue")
def sqrfunc(size):
for i in range(4):
skk.fd(size)
skk.left(90)
size = size + 5
sqrfunc(6)
sqrfunc(26)
sqrfunc(46)
sqrfunc(66)
sqrfunc(86)
sqrfunc(106)
sqrfunc(126)
sqrfunc(146)
输出:
https://www.youtube.com/watch?v=QPf
2. 用户输入模式
Python
# Python program to user input pattern
# using Turtle Programming
import turtle #Outside_In
import turtle
import time
import random
print ("This program draws shapes based on the number you enter in a uniform pattern.")
num_str = input("Enter the side number of the shape you want to draw: ")
if num_str.isdigit():
squares = int(num_str)
angle = 180 - 180*(squares-2)/squares
turtle.up
x = 0
y = 0
turtle.setpos(x, y)
numshapes = 8
for x in range(numshapes):
turtle.color(random.random(), random.random(), random.random())
x += 5
y += 5
turtle.forward(x)
turtle.left(y)
for i in range(squares):
turtle.begin_fill()
turtle.down()
turtle.forward(40)
turtle.left(angle)
turtle.forward(40)
print (turtle.pos())
turtle.up()
turtle.end_fill()
time.sleep(11)
turtle.bye()
3. 螺旋螺旋图案
Python
# Python program to draw
# Spiral Helix Pattern
# using Turtle Programming
import turtle
loadWindow = turtle.Screen()
turtle.speed(2)
for i in range(100):
turtle.circle(5*i)
turtle.circle(-5*i)
turtle.left(i)
turtle.exitonclick()
输出:
4.彩虹苯
Python
# Python program to draw
# Rainbow Benzene
# using Turtle Programming
import turtle
colors = ['red', 'purple', 'blue', 'green', 'orange', 'yellow']
t = turtle.Pen()
turtle.bgcolor('black')
for x in range(360):
t.pencolor(colors[x%6])
t.width(x//100 + 1)
t.forward(x)
t.left(59)
输出:
使用 Turtle 编程的树
参考:
- Python 3 和 2 的海龟文档
- eecs.wsu.edu [PDF] !