前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >用Python画一个星空

用Python画一个星空

作者头像
算法与编程之美
发布2023-08-22 14:29:44
2800
发布2023-08-22 14:29:44
举报
文章被收录于专栏:算法与编程之美

1 问题

如何用Python画一个简单的星空?

2 方法

在Python中有着各种各样的工具包,比如math、pillow、requests等等,每个包有着自己专门的功能。要用python画星空,在绘制星空的过程中一般需要运用到turtle工具,它是属于Python的标准库,当然也可以将其称之为海龟库,它能够用于描绘绘图的轨迹,并且操作也比较简单、便捷。

代码清单 1

from turtle import *from random import random,randintscreen = Screen()width ,height = 800,600screen.setup(width,height)screen.title("模拟3D星空")screen.bgcolor("black")screen.mode("logo")screen.delay(0)#这里要设为0,否则很卡t = 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() s =random() /3 star.shapesize(s,s) star.speed(int(s*10)) star.setx(width/2 + randint(1,width)) star.sety( randint(-height/2,height/2)) star.showturtle() stars.append(star)while True: for star in stars: star.setx(star.xcor() - 3 * star.speed()) if star.xcor()<-width/2: star.hideturtle() star.setx(width/2 + randint(1,width)) star.sety( randint(-height/2,height/2)) star.showturtle()

3 结语

针对用Python画一个简单星空的问题,提出使用Python中的turtle工具包的方法,通过实验,证明该方法是有效的,此方法简单易行,但仍有不足或考虑不周的地方,未来可以继续研究更高级算法。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2023-07-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 算法与编程之美 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档