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()  # 结束时点击关闭窗口
相关推荐
Eiceblue13 小时前
将 Python 列表导出为 Excel 文件:一维、二维、字典列表
开发语言·python·excel·visual studio code
Swizard20 小时前
别再让你的 Python 傻等了:三分钟带你通过 asyncio 实现性能起飞
python
代码or搬砖20 小时前
String字符串
android·java·开发语言
leo__52021 小时前
基于两步成像算法的聚束模式SAR MATLAB实现
开发语言·算法·matlab
Darkershadow21 小时前
python学习之串口通信
python·学习
Macbethad1 天前
自动化测试技术报告
开发语言·lua
3824278271 天前
python:输出JSON
前端·python·json
不会画画的画师1 天前
Go开发指南:io/ioutil包应用和迁移指南
开发语言·后端·golang
2503_928411561 天前
12.22 wxml语法
开发语言·前端·javascript
5980354151 天前
【java工具类】小数、整数转中文大写
android·java·开发语言