动态流星python代码
时间: 2023-11-20 20:53:45 浏览: 48
以下是一个动态流星的Python代码,使用了turtle库和random库:
```python
import turtle as tu
import random as ra
import math
tu.setup(1.0, 1.0)
tu.screensize(1.0,1.0)
tu.bgcolor('black')
t = tu.Pen()
t.ht()
colors = ['red', 'yellow', 'white', 'blue', 'orange', 'green', 'purple', 'pink']
def draw_star(x, y, size):
t.penup()
t.goto(x, y)
t.pendown()
t.begin_fill()
for i in range(5):
t.forward(size)
t.right(144)
t.end_fill()
def draw_meteor():
t.penup()
x = ra.randint(-400, 400)
y = ra.randint(200, 400)
t.goto(x, y)
t.pendown()
t.color(ra.choice(colors))
size = ra.randint(5, 15)
for i in range(size):
t.forward(10)
t.right(15)
draw_star(x, y, size)
while True:
draw_meteor()
```
这个代码使用了turtle库来绘制流星和星星,使用了random库来随机生成流星的位置、颜色和大小。在代码中,我们定义了两个函数:`draw_star()`和`draw_meteor()`。`draw_star()`函数用来绘制星星,`draw_meteor()`函数用来绘制流星。在`draw_meteor()`函数中,我们使用了`ra.randint()`函数来随机生成流星的位置,使用了`ra.choice()`函数来随机生成流星的颜色,使用了`ra.randint()`函数来随机生成流星的大小。在绘制流星的过程中,我们还调用了`draw_star()`函数来在流星的落点处绘制一个星星。