绘制天空里随机产生的100个彩色的星星,效果如图所示
import random
import turtle
def draw(t, pos, length, color=(0, 255, 0), fillc=(0, 255, 0)):
t.up()
t.setpos(pos[0], pos[1])
t.color(color, fillc)
t.pd()
t.begin_fill()
for i in range(5):
t.fd(length)
t.lt(144)
t.end_fill()
t.up
turtle.setup(800, 600)
turtle.bgcolor('black')
turtle.colormode(255)
T = turtle.Turtle()
turtle.tracer(0)
pos =(random.randint(-400, 400), random.randint(-300, 300))
for i in range(100):
length =random.randint(5, 15)
pos =(random.randint(-400, 400), random.randint(-300, 300))
color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
T.lt(random.randint(1, 360))
draw(T, pos, length, color, color)
turtle.done()