維基少年:樹莓派/樹莓派海龜星星
| 海龜模組內置於 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.python.club.tw/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()