python无源蜂鸣器音乐代码
时间: 2023-05-19 11:01:47 浏览: 303
以下是一个简单的 Python 无源蜂鸣器音乐代码示例:
```python
import winsound
import time
# 定义音符频率
C4 = 261
D4 = 294
E4 = 329
F4 = 349
G4 = 392
A4 = 440
B4 = 493
# 定义音符时长
quarter_note = 500
half_note = 1000
whole_note = 2000
# 定义音乐
music = [(E4, quarter_note), (E4, quarter_note), (F4, quarter_note), (G4, half_note), (G4, quarter_note), (F4, quarter_note), (E4, quarter_note), (D4, half_note), (C4, quarter_note), (C4, quarter_note), (D4, quarter_note), (E4, half_note), (E4, quarter_note), (D4, quarter_note), (D4, quarter_note), (E4, quarter_note), (E4, quarter_note), (F4, quarter_note), (G4, half_note), (G4, quarter_note), (F4, quarter_note), (E4, quarter_note), (D4, half_note), (C4, quarter_note), (C4, quarter_note), (D4, quarter_note), (E4, half_note), (D4, quarter_note), (C4, quarter_note), (C4, whole_note)]
# 播放音乐
for note in music:
winsound.Beep(note[0], note[1])
time.sleep(note[1] / 1000)
```
这段代码可以播放一首简单的小星星的曲子。
阅读全文
相关推荐















