音乐播放器
已结束
乐多
开始于: 2025-12-27 14:30
12
小时
主持人:
14
10:30
文件
import os
path = "文件路径"
f_list = os.listdir(path)
m_list = []
for f in f_list:
if(f[-4:] == ".wav" or f[-4:] == ".wav"):
m_list.append(f)
import pygame
from pygame import locals
pygame.init() # 初始化
# 创建窗口
screen = pygame.display.set_mode((640, 480))
# 载入图片、资源
bg_img = pygame.image.load('background.png') # 背景图
play_img = pygame.image.load('play.png') # 播放按钮
stop_img = pygame.image.load('stop.png') # 暂停按钮
last_img = pygame.image.load('last.png') # 上一曲按钮
next_img = pygame.image.load('next.png') # 下一曲按钮
logo_img = pygame.image.load('logo.png') # 下一曲按钮
# 音乐
filelist = ['歌曲1.wav', '歌曲2.wav', '歌曲3.wav', '歌曲4.ogg']
num = 0 # 从第一首歌开始
# 初始加载并播放第一首音乐
if filelist:
pygame.mixer.music.load(filelist[num])
pygame.mixer.music.play()
volume = 0.2
pygame.mixer.music.set_volume(volume) # 初始播放音量
click = 1 # 修改为1,使初始按钮显示为暂停状态
play_button = stop_img # 初始显示暂停按钮
is_playing = True # 初始标记为播放状态
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
exit()
# 按键,控制声音大小
if event.type == locals.KEYDOWN:
if event.key == locals.K_UP:
volume += 0.1
if volume > 1:
volume = 1
pygame.mixer.music.set_volume(volume)
if event.key == locals.K_DOWN:
volume -= 0.1
if volume < 0:
volume = 0
pygame.mixer.music.set_volume(volume)
if event.type == locals.MOUSEBUTTONDOWN:
if event.button == 1: # 左击
x, y = event.pos
# 修正碰撞检测区域与绘制位置匹配(270,330)
if x > 270 and x < 370 and y > 330 and y < 430:
click += 1
if click % 2 == 0:
play_button = stop_img
pygame.mixer.music.unpause()
is_playing = True
else:
play_button = play_img
pygame.mixer.music.pause()
is_playing = False
# 只有在播放状态且音乐未播放时才切换歌曲
if pygame.mixer.music.get_busy() == False and is_playing:
num += 1
if num > len(filelist)-1:
num = 0
pygame.mixer.music.load(filelist[num])
pygame.mixer.music.play()
is_playing = True # 标记为播放状态
# 绘制画面
screen.blit(bg_img, (0, 0)) # 填充背景
screen.blit(play_button, (270, 330)) # 暂停按钮
screen.blit(logo_img, (170, 60)) # 中间logo图
screen.blit(last_img, (120, 350)) # 上一曲
screen.blit(next_img, (420, 350)) # 下一曲
# 刷新画面
pygame.display.update()
- 状态
- 已结束
- 规则
- 乐多
- 题目
- 3
- 开始于
- 2025-12-27 14:30
- 结束于
- 2025-12-28 2:30
- 持续时间
- 12 小时
- 主持人
- 参赛人数
- 14