用python代码画一幅星空
最新推荐文章于 2024-11-25 13:01:13 发布

会做饭的网络工程师 于 2023-04-07 14:16:41 发布
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
运行代码后,会在画布上画出一些大小不同、位置随机的白色星星,就像是一幅星空一样。你可以根据自己的需求修改代码,调整画布大小、星星数量和大小等参数,让画出来的星空更加逼真。
效果如图所示

import turtle
import random
turtle.setup(800, 600)
turtle.bgcolor('black')
def draw_star(x, y, size):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.color('white')
turtle.begin_fill()
for i in range(5):
turtle.forward(size)
turtle.right(144)
turtle.end_fill()
for i in range(50):
x = random.randint(-400, 400)
y = random.randint(-300, 300)
size = random.randint(5, 20)
draw_star(x, y, size)
turtle.hideturtle()
turtle.done()
import turtle
import random
turtle.setup(800, 600)
turtle.bgcolor('black')
def draw_star(x, y, size):
"""
画星星的函数
:param x: 星星的x坐标
:param y: 星星的y坐标
:param size: 星星的大小
"""
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.color('white')
turtle.begin_fill()
for i in range(5):
turtle.forward(size)
turtle.right(144)
turtle.end_fill()
for i in range(50):
x = random.randint(-400, 400)
y = random.randint(-300, 300)
size = random.randint(5, 20)
draw_star(x, y, size)
turtle.hideturtle()
turtle.done()
相关知识
python画一朵花的代码
怎么用python画一朵花
python图形绘制花朵代码,如何用python画一朵花
python运行出玫瑰花的代码
python玫瑰花代码简单
python玫瑰花代码讲解
python画皮卡丘并上色代码,用python画皮卡丘的代码
浪漫的不是代码,而是运行结果皆如你所愿:用 Python 画一个爱心表白
Python表白代码:5个步骤,用代码打造独一无二的表白
用python绘制玫瑰花的代码
原文链接: 用python代码画一幅星空 https://www.huajiangbk.com/newsview1496824.html
| 上一篇: 流星雨代码 | 下一篇: Python的turtle库实现... |





