📜  使用海龟模块打印 HELLO

📅  最后修改于: 2022-05-13 01:54:47.996000             🧑  作者: Mango

使用海龟模块打印 HELLO

Turtle 是一个Python模块,它允许我们通过将其导入Python并使用海龟模块的内置函数来绘制各种几何图形。我们可以使用turtle.forward(…) 和turtle.right(…) 之类的函数来移动海龟。 Turtle 是一种初学者友好的方式来学习Python ,通过运行一些基本命令并查看海龟以图形方式进行操作。它就像一个绘图板,可以让你在上面画画。 turtle 模块可以以面向对象和面向过程的方式使用。

使用的海龟方法如下:

用于移动海龟的函数

FunctionDescription
forward(no. of steps)This function is used to move the turtle forward the specified number of steps.
backward(no. of steps)This function is used to move the turtle forward the specified number of steps.
left(angle)This function is used to rotate the turtle left/anti-clockwise with the specified angle.
right(angle)This function is used to rotate the turtle right/clockwise with the specified angle.

用于了解海龟状态的函数:

position() - 此函数用于获取海龟指针的坐标/位置。这里不需要参数,因为这个函数不是用来输入的,而是用来输出龟在控制台窗口中的位置。

用于着色的函数:

FunctionDescription
bgcolor(“color”)This function is used to give background color to the turtle window.
pencolor(“color”)This function is used to give color to the turtle pen.
fillcolor(“color”)This function is used to fill color in any closed shape.
color(pencolor, fillcolor)This is a shorthand function used to assign pencolor and fillcolor at the same time.

用于笔控的函数:

FunctionDescription
penup() or pu()This function is used to lift pen from the current position.
goto()This function is used to move the pen to a new location after lifting.
pendown() or pd()This function is used again put down the pen to a new location.
pensize(int)This function is used to specify the width of the line drawn by pen.

注意:要了解更多关于海龟的信息,请单击此处。
例子:

python3
# Python program to
# demonstrate basics of turtle
 
# Importing and making a turtle object
# with background color as white
import turtle
 
frame = turtle.Screen().bgcolor("White")
draw = turtle.Turtle()
draw.left(90)
draw.forward(100)
draw.right(90)
draw.forward(100)


python3
# Python program to
# demonstrate printing HELLO
# using turtle
 
# Here frame is initialized with
# background colour as "White"
import turtle
frame = turtle.Screen().bgcolor("White")
draw = turtle.Turtle()
 
# The colour, width and speed of the pen is initialized
draw.color("Green")
draw.width(3)
draw.speed(10)
 
# Now lets get started with actual code
# printing letter H
draw.left(90)
draw.forward(70)
draw.penup()
draw.goto(0, 35)
draw.pendown()
draw.right(90)
draw.forward(30)
draw.penup()
draw.goto(30, 70)
draw.pendown()
draw.right(90)
draw.forward(70)
 
# printing letter E
draw.penup()
draw.goto(40, 0)
draw.pendown()
draw.right(180)
draw.forward(70)
draw.right(90)
draw.forward(35)
draw.penup()
draw.goto(40, 35)
draw.pendown()
draw.forward(35)
draw.penup()
draw.goto(40, 0)
draw.pendown()
draw.forward(35)
 
# printing letter L
draw.penup()
draw.goto(90, 70)
draw.pendown()
draw.right(90)
draw.forward(70)
draw.left(90)
draw.forward(35)
 
# printing letter L
draw.penup()
draw.goto(135, 70)
draw.pendown()
draw.right(90)
draw.forward(70)
draw.left(90)
draw.forward(35)
 
# printing letter O
draw.penup()
draw.goto(210, 70)
draw.pendown()
for i in range(25):
    draw.right(15)
    draw.forward(10)


输出:

龟的基本例子

在了解了turtle的基础知识之后,让我们编写打印'HELLO'的代码。下面是实现。

蟒蛇3

# Python program to
# demonstrate printing HELLO
# using turtle
 
# Here frame is initialized with
# background colour as "White"
import turtle
frame = turtle.Screen().bgcolor("White")
draw = turtle.Turtle()
 
# The colour, width and speed of the pen is initialized
draw.color("Green")
draw.width(3)
draw.speed(10)
 
# Now lets get started with actual code
# printing letter H
draw.left(90)
draw.forward(70)
draw.penup()
draw.goto(0, 35)
draw.pendown()
draw.right(90)
draw.forward(30)
draw.penup()
draw.goto(30, 70)
draw.pendown()
draw.right(90)
draw.forward(70)
 
# printing letter E
draw.penup()
draw.goto(40, 0)
draw.pendown()
draw.right(180)
draw.forward(70)
draw.right(90)
draw.forward(35)
draw.penup()
draw.goto(40, 35)
draw.pendown()
draw.forward(35)
draw.penup()
draw.goto(40, 0)
draw.pendown()
draw.forward(35)
 
# printing letter L
draw.penup()
draw.goto(90, 70)
draw.pendown()
draw.right(90)
draw.forward(70)
draw.left(90)
draw.forward(35)
 
# printing letter L
draw.penup()
draw.goto(135, 70)
draw.pendown()
draw.right(90)
draw.forward(70)
draw.left(90)
draw.forward(35)
 
# printing letter O
draw.penup()
draw.goto(210, 70)
draw.pendown()
for i in range(25):
    draw.right(15)
    draw.forward(10)

输出:

使用海龟打印 'Hello'