Python浪漫之画星星

效果图(动态的哦!):

完整代码(上教程):

python 复制代码
import turtle
import random
import time  # 导入time模块

# 创建一个画布
screen = turtle.Screen()
screen.bgcolor("red")

# 创建一个海龟(turtle)
star = turtle.Turtle()
star.color("yellow")
star.speed(5)


# 函数:绘制星星
def draw_star(size):
    star.begin_fill()
    for _ in range(5):
        star.forward(size)
        star.right(144)  # 144度的角度
    star.end_fill()



# 函数:抖动星星
def shake_star():
    # 生成随机的抖动偏移量
    x_offset = random.randint(-10, 10)
    y_offset = random.randint(-10, 10)

    # 移动星星到新位置
    star.penup()
    star.goto(x_offset, y_offset)
    star.pendown()


# 主程序
def main():
    while True:
        shake_star()  # 抖动星星
        star.clear()  # 清除之前的星星
        draw_star(200)  # 绘制新星星
        time.sleep(1)  # 暂停1秒


# 启动程序
if __name__ == "__main__":
    main()
    turtle.done()  # 结束时点击关闭窗口
相关推荐
databook9 小时前
Manim实现闪光轨迹特效
后端·python·动效
Juchecar10 小时前
解惑:NumPy 中 ndarray.ndim 到底是什么?
python
用户83562907805111 小时前
Python 删除 Excel 工作表中的空白行列
后端·python
Json_11 小时前
使用python-fastApi框架开发一个学校宿舍管理系统-前后端分离项目
后端·python·fastapi
数据智能老司机17 小时前
精通 Python 设计模式——分布式系统模式
python·设计模式·架构
数据智能老司机18 小时前
精通 Python 设计模式——并发与异步模式
python·设计模式·编程语言
数据智能老司机18 小时前
精通 Python 设计模式——测试模式
python·设计模式·架构
数据智能老司机18 小时前
精通 Python 设计模式——性能模式
python·设计模式·架构
c8i19 小时前
drf初步梳理
python·django
每日AI新事件19 小时前
python的异步函数
python