要用Python中的turtle模塊畫星空,可以按照以下步驟進行:
import turtle
turtle.setup(width=800, height=600)
turtle.bgcolor('black')
turtle.color('white')
turtle.speed(0)
def draw_star():
for _ in range(5):
turtle.forward(100)
turtle.right(144)
turtle.right(72)
for _ in range(36):
draw_star()
turtle.done()
完整示例代碼如下所示:
import turtle
def draw_star():
for _ in range(5):
turtle.forward(100)
turtle.right(144)
turtle.right(72)
turtle.setup(width=800, height=600)
turtle.bgcolor('black')
turtle.color('white')
turtle.speed(0)
for _ in range(36):
draw_star()
turtle.done()
運行以上代碼,即可在turtle繪圖窗口中看到畫出的星空效果。