使用Python的`turtle`库可以绘制星星。下面是一个简单的示例代码,展示了如何使用`turtle`库来绘制星星:
import turtle
from random import randint, random
设置画布
screen = turtle.Screen()
width, height = 800, 600
screen.setup(width, height)
screen.title("模拟3D 星空")
screen.bgcolor("black")
screen.mode("logo")
screen.delay(0) 设置为0以避免卡顿
创建画笔
t = turtle.Turtle(visible=False, shape="circle")
t.pencolor("white")
t.fillcolor("white")
t.penup()
t.setheading(-90)
t.goto(width / 2, randint(-height / 2, height / 2))
绘制星星
stars = []
for i in range(200):
star = t.clone()
size = random() / 3
star.shapesize(size, size)
star.speed(int(size * 10))
star.setx(width / 2 + randint(1, width))
star.sety(randint(-height / 2, height / 2))
stars.append(star)
结束绘制
turtle.done()
这段代码首先创建了一个`turtle`对象,并设置了画笔的颜色和形状。然后,它使用一个循环来创建200个星星,每个星星的大小和位置都是随机的。最后,调用`turtle.done()`来结束绘制。
如果你想要绘制不同形状的星星,可以修改`t.begin_fill()`和`turtle.fd(D)`之间的循环,改变星星的边数或者边长。
希望这能帮助你开始用Python画星星!