跳轉到內容

維基少年:樹莓派/樹莓派海龜星星

來自華夏公益教科書

海龜是一種程式語言,用於使用海龜和一系列命令繪製圖形。我們使用 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")

盡情創造各種大小和顏色的星星吧!

海龜 API

[編輯 | 編輯原始碼]

這是海龜 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()

華夏公益教科書