You need to sign in or sign up before continuing.
Commit 2aadbf28 by BellCodeEditor

save project

parent 19f22638
Showing with 43 additions and 10 deletions
import pygame import pygame
import os
from pygame import locals from pygame import locals
pygame.init() # 初始化 pygame.init() # 初始化
...@@ -12,14 +13,23 @@ last_img = pygame.image.load('last.png') # 上一曲按钮 ...@@ -12,14 +13,23 @@ last_img = pygame.image.load('last.png') # 上一曲按钮
next_img = pygame.image.load('next.png') # 下一曲按钮 next_img = pygame.image.load('next.png') # 下一曲按钮
logo_img = pygame.image.load('logo.png') # 下一曲按钮 logo_img = pygame.image.load('logo.png') # 下一曲按钮
# 将文件内的音乐添加到列表
music_list=[]
path="C:\\Users\\LTT\\Documents\\pygame_lesson5_diy1\\"
file=os.listdir(path)
for i in file:
if i[-4:]==".wav" or i[-4:]==".ogg":
music_list.append(i)
# 载入音乐 # 载入音乐
pygame.mixer.music.load('歌曲4.ogg') # 载入音乐 #pygame.mixer.music.load('歌曲4.ogg') # 载入音乐
volume = 0.2 volume = 0.2
pygame.mixer.music.set_volume(volume) # 初始播放音量 pygame.mixer.music.set_volume(volume) # 初始播放音量
click = 0 click = 0
click_xy=(0,0)
play_button = stop_img play_button = stop_img
num=0
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == locals.QUIT: if event.type == locals.QUIT:
...@@ -37,17 +47,40 @@ while True: ...@@ -37,17 +47,40 @@ while True:
volume = 0 volume = 0
pygame.mixer.music.set_volume(volume) pygame.mixer.music.set_volume(volume)
# 按下鼠标
if event.type == locals.MOUSEBUTTONDOWN: if event.type == locals.MOUSEBUTTONDOWN:
click += 1 # 获取点击处的坐标
if click % 2 == 0: click_xy=event.pos
play_button = stop_img click_x=click_xy[0]
pygame.mixer.music.unpause() click_y=click_xy[1]
else: # 按下音乐暂停/开始按钮处
play_button = play_img if click_x>270 and click_x<370 and click_y>350 and click_y<450:
pygame.mixer.music.pause() # 次数增加
click += 1
if click % 2 == 0:
play_button = stop_img
pygame.mixer.music.unpause()
else:
play_button = play_img
pygame.mixer.music.pause()
# 按下上一曲
if click_x>120 and click_x<220 and click_y>350 and click_y<400:
num-=1
if num < 0:
num=len(music_list)-1
pygame.mixer.music.load(path+"\\"+music_list[num])
pygame.mixer.music.play()
click=0
play_button=stop_img
if pygame.mixer.music.get_busy() == False: if pygame.mixer.music.get_busy() == False:
pygame.mixer.music.play() num += 1
if num > len(music_list)-1:
num=0
pygame.mixer.music.load(path+"\\"+music_list[num])
pygame.mixer.music.play()
# 绘制画面 # 绘制画面
screen.blit(bg_img, (0, 0)) # 填充背景 screen.blit(bg_img, (0, 0)) # 填充背景
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment