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()  # 结束时点击关闭窗口
相关推荐
两万五千个小时1 天前
落地实现 Anthropic Multi-Agent Research System
人工智能·python·架构
哈里谢顿1 天前
Python 高并发服务限流终极方案:从原理到生产落地(2026 实战指南)
python
用户8356290780512 天前
无需 Office:Python 批量转换 PPT 为图片
后端·python
markfeng82 天前
Python+Django+H5+MySQL项目搭建
python·django
GinoWi2 天前
Chapter 2 - Python中的变量和简单的数据类型
python
JordanHaidee2 天前
Python 中 `if x:` 到底在判断什么?
后端·python
ServBay2 天前
10分钟彻底终结冗长代码,Python f-string 让你重获编程自由
后端·python
闲云一鹤2 天前
Python 入门(二)- 使用 FastAPI 快速生成后端 API 接口
python·fastapi
Rockbean2 天前
用40行代码搭建自己的无服务器OCR
服务器·python·deepseek
曲幽2 天前
FastAPI + Ollama 实战:搭一个能查天气的AI助手
python·ai·lora·torch·fastapi·web·model·ollama·weatherapi