1、首相我们导入小海龟库,以及随机模块
import random,turtle
2、创建小海龟对象
t = turtle.Pen()
3、隐藏小海龟
t.hideturtle()
4、设置背景色
turtle.bgcolor("black")
5、设置速度0最快,1—10依次变快
t.speed(0)
6、设置颜色列表,可以使用颜色的英文颜色单词、16进制颜色、RGB颜色
a_list = ["red","orange","gold","green","cyan","blue","purple"]
7、设置画板的宽和高
width = turtle.window_width()
height = turtle.window_height()
8、定义函数
def all_stars(b):
参数:画出小星星的个数
9、利用randon模块在颜色列表中随机选择颜色
color_ = random.choice(a_list)
10、颜色填充
1、选择填充颜色:
t.fillcolor(color_)
2、开始填充:t.begin_fill()
3、写入填充的图形代码xxxxx
4、结束填充t.end_fill()
11、小星星的边长(随机)随机整数5,60之间——左闭右开[5,60)
size = random.randint(5,60) # 随机获得一个尺寸
12、同上随即坐标点,传给 goto(x,y)
x = random.randint(-width//2,width//2)
y = random.randint(-height//2,height//2)
13、小海龟海图,
1、抬笔:t.up()
2、到指定坐标:t.goto(x,y)
3、落笔:t.down()
# -*- coding: utf-8 -*-
'''
@Time :2023/8/30 16:47
@作者 :一晌小贪欢
@联系 :[email protected]
'''
import random,turtle
t = turtle.Pen()
t.hideturtle()
turtle.bgcolor("black")
t.speed(0)
a_list = ["red","orange","gold","green","cyan","blue","purple"]
width = turtle.window_width()
height = turtle.window_height()
def all_stars(b):
for i in range(b):
color_ = random.choice(a_list)
t.fillcolor(color_)
t.color(color_)
size = random.randint(5,60) # 随机获得一个尺寸
x = random.randint(-width//2,width//2)
y = random.randint(-height//2,height//2)
t.up()
t.goto(x,y)
t.down()
# 随机小行星
t.begin_fill()
for j in range(5):
t.fd(size)
t.rt(144)
t.end_fill()
all_stars(100)
turtle.done()