Commit 0e04a5b8 by BellCodeEditor

save project

parent a11d6bb6
Showing with 79 additions and 64 deletions
import pygame import pygame
from pygame import locals from pygame import locals
pygame.init() # 初始化 pygame.init() # 初始化
# 创建窗口 # 创建窗口
screen = pygame.display.set_mode((640, 480)) screen = pygame.display.set_mode((640, 480))
# 载入图片、资源 # 载入图片、资源
bg_img = pygame.image.load('background.png') # 背景图 bg_img = pygame.image.load('background.png') # 背景图
play_img = pygame.image.load('play.png') # 播放按钮 play_img = pygame.image.load('play.png') # 播放按钮
stop_img = pygame.image.load('stop.png') # 暂停按钮 stop_img = pygame.image.load('stop.png') # 暂停按钮
last_img = pygame.image.load('last.png') # 上一曲按钮 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') # 下一曲按钮
# 载入音乐 # 载入音乐
pygame.mixer.music.load('歌曲1.wav') # 载入音乐 music_list=[]
path="C:\\Users\\11111\\Documents\\pygame_lesson5_diy1"
volume = 0.2 filelist = os.listdir(path)
pygame.mixer.music.set_volume(volume) # 初始播放音量 for i in filelist:
click = 0 if i [-4:] == ".ogg" or i[-4:] == ".wav":
play_button = stop_img music_list.append(i)
num = 0
while True:
for event in pygame.event.get(): volume = 0.2
if event.type == locals.QUIT: pygame.mixer.music.set_volume(volume) # 初始播放音量
exit() click = 0
# 按键,控制声音大小 play_button = stop_img
if event.type == locals.KEYDOWN: is_playing = True
if event.key == locals.K_UP:
volume += 0.1 # 初始状态设置为播放中
if volume > 1: if music_list:
volume = 1 pygame.mixer.music.load(os.path.join(path,music_list[num]))
pygame.mixer.music.set_volume(volume) pygame.mixer.music.play()
if event.key == locals.K_DOWN:
volume -= 0.1
if volume < 0: while True:
volume = 0 for event in pygame.event.get():
pygame.mixer.music.set_volume(volume) if event.type == locals.QUIT:
exit()
# 按下鼠标 # 按键,控制声音大小
if event.type == locals.MOUSEBUTTONDOWN: if event.type == locals.KEYDOWN:
if event.button == 1: if event.key == locals.K_UP:
x,y = event.pos volume += 0.1
if x > 270 and x <370 and y > 350 and y < 450: if volume > 1:
# 次数增加 volume = 1
click += 1 pygame.mixer.music.set_volume(volume)
if click % 2 == 0: if event.key == locals.K_DOWN:
play_button = stop_img volume -= 0.1
pygame.mixer.music.unpause() if volume < 0:
else: volume = 0
play_button = play_img pygame.mixer.music.set_volume(volume)
pygame.mixer.music.pause()
if event.type == locals.MOUSEBUTTONDOWN:
if pygame.mixer.music.get_busy() == False: if event.button == 1: # 左击
pygame.mixer.music.play() x, y = event.pos
if x > 270 and x < 370 and y > 350 and y < 450:
# 绘制画面 click += 1
screen.blit(bg_img, (0, 0)) # 填充背景 if click % 2 == 0:
screen.blit(play_button, (270, 330)) # 暂停按钮 play_button = stop_img
screen.blit(logo_img, (170, 60)) # 中间logo图 pygame.mixer.music.unpause()
screen.blit(last_img, (120, 350)) # 上一曲 else:
screen.blit(next_img, (420, 350)) # 下一曲 play_button = play_img
# 刷新画面 pygame.mixer.music.pause()
pygame.display.update() # 只在播放按钮状态为停止且音乐未播放时才自动播放
if pygame.mixer.music.get_busy() == False:
num += 1
if num > len(music_list)-1:
num = 0
pygame.mixer.music.load(music_list[num])
pygame.mixer.music.play()
# 绘制画面
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()
\ No newline at end of file
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