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()  # 结束时点击关闭窗口
相关推荐
黑子哥呢?32 分钟前
安装Bash completion解决tab不能补全问题
开发语言·bash
失败尽常态52335 分钟前
用Python实现Excel数据同步到飞书文档
python·excel·飞书
2501_9044477437 分钟前
OPPO发布新型折叠屏手机 起售价8999
python·智能手机·django·virtualenv·pygame
青龙小码农37 分钟前
yum报错:bash: /usr/bin/yum: /usr/bin/python: 坏的解释器:没有那个文件或目录
开发语言·python·bash·liunx
大数据追光猿43 分钟前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
Leuanghing1 小时前
【Leetcode】11. 盛最多水的容器
python·算法·leetcode
彳卸风2 小时前
Unable to parse timestamp value: “20250220135445“, expected format is
开发语言
dorabighead2 小时前
JavaScript 高级程序设计 读书笔记(第三章)
开发语言·javascript·ecmascript
xinxiyinhe2 小时前
如何设置Cursor中.cursorrules文件
人工智能·python
风与沙的较量丶2 小时前
Java中的局部变量和成员变量在内存中的位置
java·开发语言