import pygame
from pygame import locals
import os

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')  # 下一曲按钮
num = -1 #索引变量      
volume = 0.2 #音量变量
pygame.mixer.music.set_volume(volume)  # 初始播放音量
click = 0 #次数
play_button = stop_img #按钮样式变量

#音乐
music_list = [] #歌曲列表
path = "C:\\Users\\dundun\\Desktop\\Python专区\\玛酷\\课程\\pygame\\课题\\音乐播放器\\配置文件" #路径变量,所有\都要改成\\
filelist = os.listdir(path) #取出path中所有文件名
for i in filelist:
    if i[-4:] == ".ogg" or i[-4:] == ".wav": #切片(右不包括),如果i的倒数第4-1个字符等于.ogg/.wav
        music_list.append(i) #将该项加入列表

pygame.mixer.music.load("歌曲1.wav")#开场音乐
pygame.mixer.music.play()

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 #音量加0.1
                if volume > 1: #如果音量大于1
                    volume = 1 #音量等于1
                pygame.mixer.music.set_volume(volume) #将音量设为volume
            if event.key == locals.K_DOWN: #当按下下键
                volume -= 0.1 #音量减0.1
                if volume < 0: #如果音量小于1
                    volume = 0 #音量等于0
                pygame.mixer.music.set_volume(volume) #将音量设为volume

        # 按下鼠标
        if event.type == locals.MOUSEBUTTONDOWN:
            if event.button == 1: #当按下左键
                x,y = event.pos #x,y设为鼠标坐标
                if x > 270 and x < 370 and y > 350 and y < 450: #当坐标符合要求
                    # 次数增加
                    click += 1
                    if click % 2 == 0: #当判断为第2次点击(暂停)
                        play_button = stop_img #设按钮样式为暂停
                        pygame.mixer.music.unpause() #暂停音乐
                    else:
                        play_button = play_img #设按钮样式为播放
                        pygame.mixer.music.pause() #播放音乐

                #上一曲
                if x > 120 and x < 220 and y > 350 and y < 400: #如果坐标符合
                    num -= 1 #索引(num)减一
                    if num < 0: #如果num小于0
                        num = len(music_list)-1 #num切换为最后一个歌曲的索引
                    pygame.mixer.music.load(path+"\\"+music_list[num])  # 载入列表中num代表的音乐
                    pygame.mixer.music.play() #播放音乐
                    click = 0 #点击次设为零
                    play_button = stop_img #设按钮样式为暂停
                
                #下一曲
                if x > 420 and x < 520 and y > 350 and y < 400: #如果坐标符合
                    num += 1 #索引(num)加一
                    if num > len(music_list)-1: #如果num大于最后一个歌曲的索引
                        num = 0 #num切换为第一个歌曲的索引
                    pygame.mixer.music.load(path+"\\"+music_list[num])  # 载入列表中num代表的音乐
                    pygame.mixer.music.play() #播放音乐
                    click = 0 #点击次设为零
                    play_button = stop_img #设按钮样式为暂停
                
    # 绘制画面
    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()