Python流星雨代码

Python流星雨代码

前言 

用Python画场流星雨看看,源码见文末公众号哈。 

流星类 

    def __init__(self):
        self.r = ra.randint(50,100)  
        self.t = ra.randint(1,3)
        self.x = ra.randint(-2000,1000)   #流星的横坐标
        self.y = ra.randint(0,500)     #流星的纵坐标
        self.speed = ra.randint(5,10)     #流星移动速度
        self.color = ra.choice(colors)    #流星的颜色
        self.outline = 1                 #流星的大小 

画流星 

    def star(self):                #画流星函数  
        t.pensize(self.outline)    #流星的大小
        t.penup()                  #提笔
        t.goto(self.x,self.y)      #随机位置
        t.pendown()                #落笔
        t.color(self.color)        
        t.begin_fill()
        t.fillcolor(self.color)
        t.setheading(-30)
        t.right(self.t)
        t.forward(self.r)
        t.left(self.t)
        t.circle(self.r*math.sin(math.radians(self.t)),180)
        t.left(self.t)
        t.forward(self.r)
        t.end_fill()    

移动函数 

    def move(self):                    #流星移动函数
        if self.y >= -500:            #当流星还在画布中时
            self.y -= self.speed     #设置上下移动速度
            self.x += 2*self.speed   #设置左右移动速度
        else:                        
            self.r = ra.randint(50,100) 
            self.t = ra.randint(1,3)
            self.x = ra.randint(-2000,1000)
            self.y = 500
            self.speed = ra.randint(5,10)
            self.color = ra.choice(colors)
            self.outline = 1  

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
扎眼的阳光的头像扎眼的阳光普通用户
上一篇 2023年3月12日 上午12:08
下一篇 2023年3月12日 下午3:10

相关推荐