用python的turtle画模仿3d星空、运动的恒星小宇宙

1.导入模块

from turtle import *
from random import random,randint

2.步初始化定义
定义屏幕,窗口大小,标题,背景颜色

screen = Screen()
#---大一点效果好一点---
width ,height = 1600,1200
screen.setup(width,height)
screen.title('浪漫星空')
screen.bgcolor("black")
#设置或返回以毫秒为单位的绘制延迟,延迟越大,绘图越慢
screen.delay(0)

3.定义3种不同颜色的星球,大小、速度、位置、形状不同

#shape():设置乌龟的图形形状,取值:“arrow”, “turtle”, “circle”, “square”, “triangle”, “classic”
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
#---星球---白色星星---
t = Turtle(visible = False,shape='circle')
t.pencolor("white")
#海龟的颜色,也就是飞动的星球的颜色
t.fillcolor("blue")
t.penup()
#旋转角度
t.setheading(-10)
#坐标是随机的
t.goto(width/2,randint(-height/2,height/2))

#---星球2---绿色远处小星星---
t2 = Turtle(visible = False,shape='turtle')
#海龟的颜色,也就是飞动的星球的颜色
t2.fillcolor("green")
t2.penup()
t2.setheading(-50)
#坐标是随机的
t2.goto(width,randint(-height,height))

#---星球3---近的红色恒星---
t3 = Turtle(visible = False,shape='circle')
#海龟的颜色,也就是飞动的星球的颜色
t3.fillcolor("red")
t3.penup()
t3.setheading(-90)
#坐标是随机的
t3.goto(width*2,randint(-height*2,height*2))

4.定义星球列表—用于存放

stars = []
stars2 = []
stars3 = []

5.定义3种星球的大小、速度、位置并存放各自列表中

注意200为画200个各自星球就退出,注意太多了要卡死的

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
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)

for i in range(200):
    star2 = t2.clone()
    #决定星球的大小
    s2= random()/2
    star2.shapesize(s2,s2)
    star2.speed(int(s*10))
    star2.setx(width/2 + randint(1,width))
    star2.sety(randint(-height/2,height/2))
    star2.showturtle()
    stars2.append(star2)

for i in range(200):
    star3 = t3.clone()
    #决定星球的大小
    s3= random()*5
    star3.shapesize(10*s3,10*s3)
    star3.speed(int(s3*10))
    star3.setx(width*2 + randint(1,width))
    star3.sety(randint(-height*2,height*2))
    star3.showturtle()
    stars3.append(star3)

6.游戏循环—各自星球的启动

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
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()

    for star2 in stars2:
        star2.setx(star2.xcor() - 3 * star2.speed())
        if star2.xcor()<-width/2:
            star2.hideturtle()
            star2.setx(width/2 + randint(1,width))
            star2.sety( randint(-height/2,height*2))
            star2.showturtle()

    for star3 in stars3:
        star3.setx(star3.xcor() - 3 * star3.speed())
        if star3.xcor()<-width*2:
            star3.hideturtle()
            star3.setx(width*2 + randint(1,width))
            star3.sety( randint(-height*2,height*2))
            star3.showturtle()

实现效果:
在这里插入图片描述

  • 7
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
python开发的真实星空显示软件 含真实恒星位置数据3144颗 代码讲解见: https://blog.csdn.net/xiaorang/article/details/106598307 数据格式例: {'long': 0.023278328898474372, 'lat': -0.09961466705757636, 'light': 46, 'const': 66}, {'long': 0.024870941840919196, 'lat': 0.2338062439126301, 'light': 55, 'const': 62}, {'long': 0.028107061526797, 'lat': 1.1204335039257496, 'light': 56, 'const': 18}, {'long': 0.03660100303760025, 'lat': 0.5077259659824991, 'light': 21, 'const': 1}, {'long': 0.04004802831028905, 'lat': 1.0323574005393255, 'light': 23, 'const': 18}, {'long': 0.03944444109507185, 'lat': 0.3178583859888262, 'light': 55, 'const': 62}, {'long': 0.040797071265367454, 'lat': -0.488478858963941, 'light': 54, 'const': 74}, {'long': 0.0410661312228549, 'lat': -0.798444499556106, 'light': 39, 'const': 64}, {'long': 0.043800486202076855, 'lat': 0.1945266317121166, 'light': 55, 'const': 66}, {'long': 0.045036755271142, 'lat': 0.804111967609767, 'light': 50, 'const': 1}, {'long': 0.043785947609407745, 'lat': -1.4350775693910554, 'light': 53, 'const': 58}, {'long': 0.04915283505929031, 'lat': -0.2699684886295715, 'light': 49, 'const': 21}, {'long': 0.050498187206605094, 'lat': -0.4851966800391031, 'light': 54, 'const': 74}, {'long': 0.05119631890740283, 'lat': -0.6131874860342564, 'light': 52, 'const': 74}, {'long': 0.05775584219505068, 'lat': 0.26500400429202875, 'light': 28, 'const': 62}, {'long': 0.05896303407877759, 'lat': 0.7162006931179011, 'light': 57, 'const': 1}, {'long': 0.06371905629046214, 'lat': 0.3526728525507925, 'light': 48, 'const': 62}, {'long': 0.06387905062299246, 'lat': -0.33043929519585447, 'light': 44, 'const': 21}, 代码解说详细的教程见: https://blog.csdn.net/xiaorang/article/details/106598307
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值