猫史档案馆


【Python作品分享】去太空,趣编程

用户:嘤嘤怪哟嘤嘤怪哟查看:0 回复:3 评论:0 创建时间:2021-05-05T22:18:22


【作品展示】

center_image

 

【作品介绍】

这个作品主要使用了turtle库和random库,实现在宇宙中星河相伴的场景,错过了余晖,还会有漫天星辰,实现思路主要是通过while和for循环实现颜色的变化,这也是我想了很久的,最开始时本来是打算只接用while循环但是颜色会过于单一,才有了这个作品。最后祝祖国航天事业越来越nice

 

【作品源代码】

from turtle import *
from random import random,randint
screen = Screen()
width, height = 800, 600
screen.setup(width, height)  
screen.title("星空")  
screen.bgcolor("black")  
screen.mode("logo")  
screen.delay(0) 

printer = Turtle()
printer.hideturtle()
printer.penup()
printer.color('white')
printer.goto(-100, -150)

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(300):
    star = t.clone()
    s = random()/3
    if s > 0.01 and s < 0.03:
        star.pencolor("black")
        star.fillcolor("black")
    elif s > 0.03 and s < 0.04:
        star.pencolor("lightcoral")
        star.fillcolor("lightcoral")
    elif s > 0.05 and s < 0.1:
        star.pencolor("green")
        star.fillcolor("green")
    elif s > 0.15 and s < 0.16:
        star.pencolor("yellow")
        star.fillcolor("yellow")
    elif s > 0.19 and s < 0.2:
        star.pencolor("red")
        star.fillcolor("red")
    elif s > 0.21 and s < 0.22:
        star.pencolor("purple")
        star.fillcolor("purple")
    elif s > 0.29 and s < 0.3:
        star.pencolor("darkorange")
        star.fillcolor("darkorange")
    elif s > 0.31 and s < 0.32:
        star.pencolor("red")
        star.fillcolor("yellow")
    elif s > 0.32 and s < 0.33:
        star.pencolor("yellow")
        star.fillcolor("white")
    star.shapesize(s, s)
    star.speed(int(s*30))  # 设置画笔移动速度,画笔绘制的速度范围[0,10]整数,数字越大越快
    star.setx(width/2 + randint(1, width))  # 将当前x轴移动到指定位置
    star.sety(randint(-height/2, height/2))  # 将当前y轴移动到指定位置
    #star.showturtle()       #显示画笔的turtle形状
    stars.append(star)

i = 0
while True:
    i += 0
    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()
    if i >= 100:
        break

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
......6666....无法显示......6666....无法显示

利害emotion_编程猫_点赞

点赞0


评论


无题_无题_

厉害厉害~

点赞0


评论


嘤嘤怪哟嘤嘤怪哟

11

点赞0


评论