Python之七彩花朵代码实现

Python之七彩花朵代码实现


下面是一个简单的使用 Python 的 七彩花朵。这个示例只是一个简单的版本,没有很多高级功能,但它可以作为一个起点,你可以在此基础上添加更多功能。

import turtle as tu
import random as ra
import math
tu.setup(1.0, 1.0)
t = tu.Pen()
t.ht()
colors = ['red','skyblue','orange','yellow','lime','pink','violet']
class Flower():    #每个花朵(花朵类)
    def __init__(self):
        self.r = ra.randint(8,12)        #花朵的半径
        self.x = ra.randint(-1000,1000)   #花朵的横坐标
        self.y = ra.randint(-500,500)     #花朵的纵坐标
        self.f = ra.uniform(-3.14,3.14)   #花朵左右移动呈正弦函数
        self.speed = ra.randint(5,10)     #花朵移动速度
        self.color = ra.choice(colors)    #花朵的颜色
        self.outline = 1                  #花朵的外框大小(可不要)
    def move(self):                    #花朵移动函数
        if self.y >= -500:            #当花朵还在画布中时
            self.y -= self.speed     #设置上下移动速度
            self.x += self.speed * math.sin(self.f)    #设置左右移动速度
            self.f += 0.1            #可以理解成标志,改变左右移动的方向
        else:                        #当花朵漂出了画布时,重新生成一个花朵
            self.r = ra.randint(8,12)
            self.x = ra.randint(-1000,1000)
            self.y = 500
            self.f = ra.uniform(-3.14,3.14)
            self.speed = ra.randint(5,10)
            self.color = ra.choice(colors)
            self.outline = 1
    def draw(self):       #画花朵函数,就是用turtle画花朵
        t.penup()
        t.goto(self.x,self.y)
        t.setheading(self.x)
        t.pendown()
        t.left(36)
        t.color(self.color)
        t.begin_fill()
        t.fillcolor(self.color)
        for i in range(5):
            t.left(-72)
            t.circle(self.r,extent=144)
        t.end_fill()
        #t.right(36)
        #t.begin_fill()
        #t.fillcolor("red")
        #t.color("white")
        #t.circle(12)
        #t.end_fill()
Flowers = []            #用列表保存所有花朵
for i in range(100):
    Flowers.append(Flower())
tu.bgcolor('black')
while True:           #开始漂浮
    tu.tracer(0)
    t.clear()
    for i in range(50):    #在画布中设置50个漂浮的花朵
        Flowers[i].move()
        Flowers[i].draw()
    tu.update()
tu.mainloop()

《AUTOSAR谱系分解(ETAS工具链)》之总目录

猜你喜欢

转载自blog.csdn.net/PlutoZuo/article/details/134675552