Python模拟星空
•
编程语言

文章目录
- 系列文章
- 前言
- Turtle基础
-
- 1.1 Turtle画板
- 1.2 Turtle画笔
- 1.3 Turtle画图
- 1.4 Turtle填色
- 1.5 Turtle写字
- 模拟星空
-
- 模拟星球
- 浪漫星空
- 完整代码
- 尾声
系列文章
| 序号 | 文章目录 | 直达链接 | |
| 表白系列 | |||
| 1 | 无法拒绝的表白界面 | https://want595.blog.csdn.net/article/details/134744894 | |
| 2 | 满屏飘字表白代码 | https://want595.blog.csdn.net/article/details/135037388 | |
| 3 | 无限弹窗表白代码 | https://want595.blog.csdn.net/article/details/134744711 | |
| 4 | 李峋同款可写字版跳动的爱心 | https://want595.blog.csdn.net/article/details/134744191 | |
| 5 | 流星雨 | https://want595.blog.csdn.net/article/details/134747408 | |
| 6 | 漂浮爱心 | https://want595.blog.csdn.net/article/details/134744929 | |
| 7 | 爱心光波 | https://want595.blog.csdn.net/article/details/134747365 | |
| 8 | 玫瑰花 | https://want595.blog.csdn.net/article/details/134747447 | |
| 节日系列 | |||
| 1 | 新春/跨年 | 烟花秀(2022) | https://want595.blog.csdn.net/article/details/128727394 |
| 烟花秀(2023) | https://want595.blog.csdn.net/article/details/135042880 | ||
| 粒子烟花 | https://want595.blog.csdn.net/article/details/136029420 | ||
| 2 | 圣诞节 | 圣诞礼物 | https://want595.blog.csdn.net/article/details/135336583 |
| 圣诞树(2022) | https://want595.blog.csdn.net/article/details/128428985 | ||
| 绿色圣诞树(2023) | https://want595.blog.csdn.net/article/details/135048607 | ||
| 粉色圣诞树(2023) | https://want595.blog.csdn.net/article/details/135043042 | ||
| 3 | 冬至 | 大雪纷飞 | https://want595.blog.csdn.net/article/details/128806017 |
| 4 | 生日 | 生日蛋糕 | https://want595.blog.csdn.net/article/details/128739755 |
| 5 | 儿童节 | 五彩气球 | https://want595.blog.csdn.net/article/details/128741043 |
| 6 | 国庆节 | 国庆祝福 | https://want595.blog.csdn.net/article/details/128740923 |
| 7 | 万圣节 | 万圣礼物 | https://want595.blog.csdn.net/article/details/128734395 |
| 8 | 愚人节 | 愚人代码 | https://want595.blog.csdn.net/article/details/128696990 |
| 9 | 中秋节 | 浪漫星空 | https://want595.blog.csdn.net/article/details/128737284 |
| 10 | 植树节 | 樱花树 | https://want595.blog.csdn.net/article/details/128700178 |
| 动漫系列 | |||
| 1 | 名侦探柯南系列 | 柯南 | https://want595.blog.csdn.net/article/details/134777613 |
| 2 | 喜羊羊与灰太狼系列 | 喜羊羊 | https://want595.blog.csdn.net/article/details/134778583 |
| 懒羊羊 | https://want595.blog.csdn.net/article/details/134847642 | ||
| 灰太狼 | https://want595.blog.csdn.net/article/details/135335303 | ||
| 小灰灰 | https://want595.blog.csdn.net/article/details/135335445 | ||
| 小香香 | https://want595.blog.csdn.net/article/details/135056783 | ||
| 3 | 海绵宝宝系列 | 海绵宝宝 | https://want595.blog.csdn.net/article/details/134847364 |
| 4 | 哆啦A梦系列 | 哆啦A梦 | https://want595.blog.csdn.net/article/details/135037884 |
| 5 | HelloKitty系列 | hellokitty | https://want595.blog.csdn.net/article/details/135337732 |
| 6 | Tom&Jerry系列 | Tom&Jerry | https://want595.blog.csdn.net/article/details/135337775 |
| 7 | 草莓熊系列 | 草莓熊 | https://want595.blog.csdn.net/article/details/135337832 |
| 8 | 皮卡丘系列 | 迷你皮卡丘 | https://want595.blog.csdn.net/article/details/135337911 |
| 高级皮卡丘 | https://want595.blog.csdn.net/article/details/135337937 | ||
| 豪华皮卡丘 | https://want595.blog.csdn.net/article/details/135337947 | ||
| 炫酷系列 | |||
| 1 | 一闪一闪亮星星系列 | 张万森下雪了 | https://want595.blog.csdn.net/article/details/135336915 |
| 一闪一闪亮星星 | https://want595.blog.csdn.net/article/details/135337049 | ||
| 2 | 代码雨 | https://want595.blog.csdn.net/article/details/135054341 | |
| 3 | 七彩花朵 | https://want595.blog.csdn.net/article/details/135056670 | |
| 4 | 3D星空 | https://want595.blog.csdn.net/article/details/135056516 | |
| 5 | 金榜题名 | https://want595.blog.csdn.net/article/details/135056150 | |
| 6 | 满天星 | https://want595.blog.csdn.net/article/details/135056305 | |
| …… | |||
前言
Python模拟星空,你值得拥有!uu们一周不见啦,本周博主参考网上大佬们的星空,给大家带来了属于自己的星空,快来看看吧~
Turtle基础
小海龟(Turtle)是Python中画图的一个重要的包(内置包),里面包含丰富的画图工具以及画图的各种功能,当你学会了用Turtle画图后,你可以画任何你想画的图案哦
1.1 Turtle画板
Turtle的画板大小可以用turtle.screensize()函数来设置
turtle.screensize(width,height,bg):设置画板的大小,包含长和宽,width为宽,height为高,bg为画布颜色
1.2 Turtle画笔
Turtle的画笔有几个常用的函数
①turtle.penup():抬起画笔,此时移动画笔不会在画布上留下痕迹哦 ②turtle.pendown():放下画笔,与turtle.penup相对应,放下画笔后就可以继续画画了(放下画笔后画画会在画布上留下痕迹) ③turtle.pensize():控制画笔的大小(可以根据需求自行定义画笔的大小哦) ④turtle.pencolor():控制画笔的颜色(可以自己在网上查阅所有python可以使用的颜色,python里面可以用的颜色有很多的哦) ⑤turtle.hideturtle():隐藏画笔(隐藏画笔以后画图时画笔就看不到了)
1.3 Turtle画图
在画图的过程中,我们经常要使用一些简单的移动函数
①turtle.forward(x):将画笔向前移动x个像素(x可以理解为距离) ②turtle.backward(x):将画笔向后退x个像素(x可以理解为距离) ③turtle.left(n):将画笔向左旋转n度 ④turtle.right(n):将画笔向右旋转n度 ⑤turtle.speed():设置画笔画图的速度(1~10递增,0最快)
1.4 Turtle填色
在画好图后,我们经常需要对其进行填色,这里可以用turtle.fillcolor()函数,括号里写入你想填充的颜色即可。
在使用turtle.fillcolor()函数要注意其基本格式
turtle.beginfill() #开始填充 turtle.fillcolor() #输入填充的颜色 turtle.endfill() #结束填充
1.5 Turtle写字
在完成整个画图后,我们可以使用turtle.write()函数进行写字
turtle.write(" ",move,align,font)
① 第一个位置双引号内填入要写的字
② move(可选):在默认情况下,move为false。如果move为true,则笔将移动到右下角
③ align(可选):可取值是left即左、center即中、right即右之一,是字符串格式
④ font(可选):字体三元组(fontname、fontsize、fonttype),fontname即字体名称(字符串格式,如“宋体”),fontsize即字体大小),fonttype即字体类型如:normal(普通)、bold(粗体)、italic(斜体)
模拟星空
简单了解turtle小海龟后,让我们一起来模拟星空吧!
模拟星球
我们将每一个星球看成一个质点,每一个质点用小海龟表示,通过克隆小海龟生成一个个星球
t=tu.Turtle(visible=False,shape='circle')
t.pencolor("white")
t.fillcolor("white")
t.penup()
t.goto(ra.randint(width/2,width),ra.randint(-height/2,height/2))
for i in range(99):
star=t.clone()
s=ra.uniform(0,1)/3
star.shapesize(s,s)
star.speed(ra.randint(2,5))
star.setx(ra.randint(width/2,width))
star.sety(ra.randint(-height/2,height/2))
star.showturtle()
stars.append(star)
浪漫星空
通过循环来模拟星空
当每个星球从左边消失时,我们将他从新加入右侧,继续循环
while True:
for star in stars:
star.setx(star.xcor()-star.speed())
if star.xcor()<-width/2:
star.hideturtle()
star.setx(ra.randint(width/2,width))
star.sety(ra.randint(-height/2,height/2))
star.showturtle()
完整代码
https://want595.blog.csdn.net/article/details/135056516
尾声
友友们下期见!
本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://net2asp.com/6add2d18ea.html
