当前位置:   article > 正文

Python中turtle库_python turtle 加背景音乐

python turtle 加背景音乐

文章目录

简介

https://images.cnblogs.com/cnblogs_com/blogs/722174/galleries/2074790/o_220114083052_Python%E4%B8%AD%E6%B5%B7%E9%BE%9F%E7%94%BB%E5%9B%BE.jpg

案例

生日蛋糕,同时添加音乐

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(
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/788519
推荐阅读
相关标签
  

闽ICP备14008679号