维基少年:树莓派/树莓派海龟星星
海龟模块内置于 Python 的默认安装中。因此,本教程不需要树莓派。 |
海龟是一种编程语言,用于使用海龟和一系列命令绘制图形。我们使用 Python 模块,它允许您使用海龟命令绘制图片。
首先,我们将编写一个程序来绘制一颗星星
# Import all functions and variables from the turtle library
from turtle import *
# Set the pen color to WhiteSmoke
color("WhiteSmoke")
# Set the background color to MidnightBlue
bgcolor("MidnightBlue")
# Put the pen down so the turtle can start drawing
pendown()
# Start filling the shape with the current pen color
begin_fill()
# Draw the star shape
for side in range(5):
# Turn left 144 degrees
left(144)
# Move the turtle forward 50 units
forward(50)
# Finish filling the shape
end_fill()
# Pick the pen up so the turtle can move without drawing
penup()
# Move the turtle forward 100 units
forward(100)
# Enter the turtle graphics event loop and wait for the user to close the window
done()
现在修改程序,使其使用函数
from turtle import *
# A function for drawing a star #'def' means 'define'
def drawStar():
pendown()
begin_fill()
for side in range(5):
left(144)
forward(50)
end_fill()
penup()
# Now use the function to draw a light grey star on a dark blue
background
color("WhiteSmoke")
bgcolor("MidnightBlue")
drawStar()
forward(100)
drawStar()
left(120)
forward(150)
drawStar()
hideturtle()
done()
现在修改函数,使其绘制不同大小的星星
def drawStar(starSize)
…
forward(starSize)
…
drawStar(100)
…
drawStar(50)
现在,修改函数,以便我们可以绘制不同大小和颜色的星星
def drawStar(starSize, starColour)
color(starColour)
…
forward(starSize)
…
drawStar(100, "Red")
…
drawStar(50, "White")
尽情创造各种大小和颜色的星星吧!
这是海龟 Python 模块提供的函数的完整列表: https://docs.pythonlang.cn/3/library/turtle.html
forward()
| fd()
backward()
| bk()
| back()
right()
| rt()
left()
| lt()
goto()
| setpos()
| setposition()
setx()
sety()
setheading()
| seth()
home()
circle()
dot()
stamp()
clearstamp()
clearstamps()
undo()
speed()
position()
| pos()
towards()
xcor()
ycor()
heading()
distance()
degrees()
radians()
pendown()
| pd()
| down()
penup()
| pu()
| up()
pensize()
| width()
pen()
isdown()
color()
pencolor()
fillcolor()
fill()
begin_fill()
end_fill()
reset()
clear()
write()
showturtle()
| st()
hideturtle()
| ht()
isvisible()
v
shape()
resizemode()
shapesize()
| turtlesize()
settiltangle()
tiltangle()
tilt()
onclick()
onrelease()
ondrag()
mainloop()
| done()
begin_poly()
end_poly()
get_poly()
clone()
getturtle()
| getpen()
getscreen()
setundobuffer()
undobufferentries()
tracer()
window_width()
window_height()
bgcolor()
bgpic()
clear()
| clearscreen()
reset()
| resetscreen()
screensize()
setworldcoordinates()
delay()
tracer()
update()
listen()
onkey()
onclick()
| onscreenclick()
ontimer()
mode()
colormode()
getcanvas()
getshapes()
register_shape()
| addshape()
turtles()
window_height()
window_width()
bye()
exitonclick()
setup()
title()