Python中turtle库
简介

案例
画生日蛋糕,同时添加音乐
import turtle as t import pygame import math as m import random as r def backgroundMusic(): # 音乐导入 file = r'输入音乐的路径' # 界面初始化 pygame.mixer.init() # 载入音乐 pygame.mixer.music.load(file) # 设置音量 pygame.mixer.music.set_volume(0.5) # 播放音乐,(-1)为循环播放 pygame.mixer.music.play(-1) class DrawCake: @staticmethod def drawX(a, i): angle = m.radians(i) return a * m.cos(angle) @staticmethod def drawY(b, i): angle = m.radians(i) return b * m.sin(angle) @staticmethod def drawCake(): backgroundMusic() t.speed(10) t.hideturtle() # 设置背景颜色,窗口位置以及大小 t.bgcolor('#d3dae8') t.setup(1000, 800) t.penup() t.goto(150, 0) t.pendown() # 1 t.pencolor('white') t.begin_fill() for i in range(360): x = DrawCake.drawX(150, i) y = DrawCake.drawY(60, i) t.goto(x, y) t.fillcolor('#fef5f7') t.end_fill() # 2 t.begin_fill() for i in range(180): x = DrawCake.drawX(150, -i) y = DrawCake.drawY(70, -i) t.goto(x, y) for i in range(180, 360): x = DrawCake.drawX(150, i) y = DrawCake.drawY(60, i) t.goto(x, y) t.fillcolor('#f2d7dd') t.end_fill() # 3 t.pu() t.goto(120, 0) t.pd() t.begin_fill() for i in range(360): x = DrawCake.drawX(120, i) y = DrawCake.drawY(48, i) t.goto(x, y) t.fillcolor('#cbd9f9') t.end_fill() # 4 t.begin_fill() t.pencolor('#fee48c') for i in range(540): x = DrawCake.drawX(120, i) y = DrawCake.drawY(48, i) + 70 t.goto(x, y) t.goto(-120, 0) t.fillcolor('#cbd9f9') t.end_fill() # 5 t.pu() t.goto(120, 70) t.pd() t.pencolor('#fff0f3') t.begin_fill() for i in range(360): x = DrawCake.drawX(120, i) y = DrawCake.drawY(48, i) + 70 t.goto(x, y) t.fillcolor('#fff0f3') t.end_fill() # 6 t.pu() t.goto(110, 70) t.pd() t.pencolor('#fff9fb') t.begin_fill() for i in range(360): x = DrawCake.drawX(110, i) y = DrawCake.drawY(44, i) + 70 t.goto(x, y) t.fillcolor('#fff9fb') t.end_fill() # 7 t.pu() t.goto(120, 0) t.pd() t.begin_fill() t.pencolor('#ffa79d') for i in range(180): x = DrawCake.drawX(120, -i) y = DrawCake.drawY(48, -i) + 10 t.goto(x, y) t.goto(-120, 0) for i in range(180, 360): x = DrawCake.drawX(120, i) y = DrawCake.drawY(48, i) t.goto(x, y) t.fillcolor('#ffa79d') t.end_fill() # 8 t.pu() t.goto(120, 70) t.pd() t.begin_fill() t.pensize(4) t.pencolor('#fff0f3') for i in range(1800): x = DrawCake.drawX(120, 0.1 * i) y = DrawCake.drawY(-18, i) + 10 t.goto(x, y) t.goto(-120, 70) t.pensize(1) for i in range(180, 360): x = DrawCake.drawX(120, i) y = DrawCake.drawY(48, i) + 70 t.goto(x, y) t.fillcolor('#fff0f3') t.end_fill() # 9 t.pu() t.goto(80, 70) t.pd() t.begin_fill() t.pencolor('#6f3732') t.goto(80, 120) for i in range(180): x = DrawCake.drawX(80, i) y = DrawCake.drawY(32, i) + 120 t.goto(x, y) t.goto(-80, 70) for i in range(180, 360): x = DrawCake.drawX(80, i) y = DrawCake.drawY(32, i) + 70 t.goto(x, y) t.fillcolor('#6f3732') t.end_fill() # 10 t.pu() t.goto(80, 120) t.pd() t.pencolor('#ffaaa0') t.begin_fill() for i in range(360): x = DrawCake.drawX(80, i) y = DrawCake.drawY(32, i) + 120 t.goto(x, y) t.fillcolor('#ffaaa0') t.end_fill() # 11 t.pu() t.goto(70, 120) t.pd() t.pencolor('#ffc3be') t.begin_fill() for i in range(360): x = DrawCake.drawX(70, i) y = DrawCake.drawY(28, i) + 120 t.goto(x, y) t.fillcolor('#ffc3be') t.end_fill() # 12 t.pu() t.goto(80, 120) t.pd() t.begin_fill() t.pensize(3) t.pencolor('#ffaaa0') for i in range(1800): x = DrawCake.drawX(80, 0.1 * i) y = DrawCake.drawY(-12, i) + 80 t.goto(x, y) t.goto(-80, 120) t.pensize(1) for i in range(180, 360): x = DrawCake.drawX(80, i) y = DrawCake.drawY(32, i) + 120 t.goto(x, y) t.fillcolor('#ffaaa0') t.end_fill() # 13 t.pu() t.goto(64, 120) t.pd() t.pencolor('#b1c9e9') t.begin_fill() for i in range(360): x = DrawCake.drawX(4, i) + 60 y = DrawCake.drawY(1, i) + 120 t.goto(x, y) t.goto(64, 170) for i in range(540): x = DrawCake.drawX(4, i) + 60 y = DrawCake.drawY(1, i) + 170 t.goto(x, y) t.goto(56, 120) t.fillcolor('#b1c9e9') t.end_fill() t.pencolor('white') t.pensize(2) for i in range(1, 6): t.goto(64, 120 + 10 * i) t.pu() t.goto(56, 120 + 10 * i) t.pd() t.pu() t.goto(60, 170) t.pd() t.goto(60, 180) t.pensize(1) # t.pu() t.goto(64, 190) t.pd() t.pencolor('#f1add1') t.begin_fill() for i in range(360): x = DrawCake.drawX(4, i) + 60 y = DrawCake.drawY(10, i) + 190 t.goto(x, y) t.fillcolor('#f1add1') t.end_fill() # 14 t.pu() t.goto(-56, 120) t.pd() t.pencolor('#b1c9e9') t.begin_fill() for i in range(360): x = DrawCake.drawX(4, i) - 60 y = DrawCake.drawY(1, i) + 120 t.goto(x, y) t.goto(-56, 170) for i in range(540): x = DrawCake.drawX(4, i) - 60 y = DrawCake.drawY(1, i) + 170 t.goto(x, y) t.goto(-64, 120) t.fillcolor('#b1c9e9') t.end_fill() t.pencolor('white') t.pensize(2) for i in range(1, 6): t.goto(-56, 120 + 10 * i) t.pu() t.goto(-64, 120 + 10 * i) t.pd() t.pu() t.goto(-60, 170) t.pd() t.goto(-60, 180) t.pensize(1) # t.pu() t.goto(-56, 190) t.pd() t.pencolor('#f1add1') t.begin_fill() for i in range(360): x = DrawCake.drawX(4, i) - 60 y = DrawCake.drawY(10, i) + 190 t.goto(x, y) t.fillcolor('#f1add1') t.end_fill() # 15 t.pu() t.goto(0, 130) t.pd() t.pencolor('#b1c9e9') t.begin_fill() for i in range(360): x = DrawCake.drawX(4, i) y = DrawCake.drawY(1, i) + 130 t.goto(x, y) t.goto(4, 180) for i in range(540): x = DrawCake.drawX(4, i) y = DrawCake.drawY(1, i) + 180 t.goto(x, y) t.goto(-4, 130) t.fillcolor('#b1c9e9') t.end_fill() t.pencolor('white') t.pensize(2) for i in range(1, 6): t.goto(4, 130 + 10 * i) t.pu() t.goto(-4, 130 + 10 * i) t.pd() t.pu() t.goto(0, 180) t.pd() t.goto(0, 190) t.pensize(1) # t.pu() t.goto(4, 200) t.pd() t.pencolor('#f1add1') t.begin_fill() for i in range(360): x = DrawCake.drawX(4, i) y = DrawCake.drawY(10, i) + 200 t.goto(x, y) t.fillcolor('#f1add1') t.end_fill() # 16 t.pu() t.goto(30, 110) t.pd() t.pencolor('#b1c9e9') t.begin_fill() for i in range(360): x = DrawCake.drawX(4, i) + 30 y = DrawCake.drawY(1, i) + 110 t.goto(x, y) t.goto(34, 160) for i in range(540): x = DrawCake.drawX(4, i) + 30 y = DrawCake.drawY(1, i) + 160 t.goto(x, y) t.goto(26, 110) t.fillcolor('#b1c9e9') t.end_fill() t.pencolor('white') t.pensize(2) for i in range(1, 6): t.goto(34, 110 + 10 * i) t.pu() t.goto(26, 110 + 10 * i) t.pd() t.pu() t.goto(30, 160) t.pd() t.goto(30, 170) t.pensize(1) # t.pu() t.goto(34, 180) t.pd() t.pencolor('#f1add1') t.begin_fill() for i in range(360): x = DrawCake.drawX(4, i) + 30 y = DrawCake.drawY(10, i) + 180 t.goto(x, y) t.fillcolor('#f1add1') t.end_fill() # 17 t.pu() t.goto(-30, 110) t.pd() t.pencolor('#b1c9e9') t.begin_fill() for i in range(360): x = DrawCake.drawX(4, i) - 30 y = DrawCake.drawY(1, i) + 110 t.goto(x, y) t.goto(-26, 160) for i in range(540): x = DrawCake.drawX(4, i) - 30 y = DrawCake.drawY(1, i) + 160 t.goto(x, y) t.goto(-34, 110) t.fillcolor('#b1c9e9') t.end_fill() t.pencolor('white') t.pensize(2) for i in range(1, 6): t.goto(-26, 110 + 10 * i) t.pu() t.goto(-34, 110 + 10 * i) t.pd() t.pu() t.goto(-30, 160) t.pd() t.goto(-30, 170) t.pensize(1) # t.pu() t.goto(-26, 180) t.pd() t.pencolor('#f1add1') t.begin_fill() for i in range(360): x = DrawCake.drawX(4, i) - 30 y = DrawCake.drawY(10, i) + 180 t.goto(x, y) t.fillcolor('#f1add1') t.end_fill() # 随机 color = ['#e28cb9', '#805a8c', '#eaa989', '#6e90b7', '#b8b68f', '#e174b5', '#cf737c', '#7c8782'] for i in range(160): t.pu() x = r.randint(-200, 200) y = r.randint(-25, 30) t.goto(x, y) t.pd() t.dot(r.randint(2, 5), color[r.randint(0, 7)]) for i in range(40): t.pu() x = r.randint(-90, 90) y = r.randint(-35, 10) t.goto(x, y) t.pd() t.dot(r.randint(2, 5), color[r.randint(0, 7)]) for i in range(40): t.pu() x = r.randint(-80, 80) y = r.randint(60, 90) t.goto(x, y) t.pd() t.dot(r.randint(2, 5), color[r.randint(0, 7)]) for i in range(30): t.pu() x = r.randint(-50, 50) y = r.randint(45, 70) t.goto(x, y) t.pd() t.dot(r.randint(2, 5), color[r.randint(0, 7)]) for i in range(50): t.pu() x = r.randint(-500, 500) y = r.randint(120, 300) t.goto(x, y) t.pd() t.dot(r.randint(3, 5), color[r.randint(0, 7)]) t.seth(90) t.pu() t.goto(0, 0) t.fd(210) t.left(90) t.fd(170) t.pd() t.write('Happy Birthday To You', font=('Curlz MT', 50), align='left') t.pu() t.goto(210, 300) t.pd() t.color('pink') t.write('自定义', font=('楷体', 50), align='left') t.done() if __name__ == '__main__': DrawCake.drawCake() # 运行程序
本文来自博客园,作者:Kenny_LZK,转载请注明原文链接:https://www.cnblogs.com/liuzhongkun/p/15802530.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?