其他
Python源代码分享:07画满天繁星
Python,速成心法
敲代码,查资料,问度娘
练习,探索,总结,优化
使用turtle库实现了满天繁星的效果,使用了随机数生成星星的位置和大小,同时使用随机颜色填充星星,最终显示在一个黑色的画布上。
import turtle
import random
# 创建turtle对象
t = turtle.Turtle()
t.hideturtle()
t.speed(0)
# 设置画布背景颜色和画笔颜色
turtle.bgcolor('black')
colors = ['red', 'yellow', 'blue', 'green', 'orange', 'purple', 'white', 'gray']
# 画满天繁星
for i in range(500):
x = random.randint(-turtle.window_width()//2, turtle.window_width()//2)
y = random.randint(-turtle.window_height()//2, turtle.window_height()//2)
size = random.randint(1, 20)
t.penup()
t.goto(x, y)
t.pendown()
t.dot(size, random.choice(colors))
# 显示画布
turtle.done()
完毕!!感谢您的收看
------------往期更多精彩内容------------
-------------------------------------------------
---------------Python源码集合---------------
Python源代码分享:05使用turtle模块绘制一个彩色螺旋图案