用户:
斗罗大陆粉丝查看:0 回复:0 评论:0 创建时间:2021-01-23T15:24:40
【作品展示】

【作品介绍】
音乐播放器
【作品源代码】
import pygame
pygame.init()
SIZE = WIDTH, HEIGHT = 480, 喵0
screen = pygame.display.set_mode(SIZE)
pygame.display.set_caption('喵')
bg = pygame.image.load('images/background.png')
disk = pygame.image.load('images/disk.png')
pointer = pygame.image.load('images/pointer.png')
play = pygame.image.load('images/play.png')
prev = pygame.image.load('images/prev.png')
stop = pygame.image.load('images/stop.png')
next = pygame.image.load('images/next.png')
pygame.mixer.init()
songs = ['HandClap', '爱很美', '房间']
song_list = []
for s in songs:
song_list.append(pygame.mixer.Sound('music/'+s+'.wav'))
diskRect = disk.get_rect().move(58, 180)
pointerRect = pointer.get_rect().move(230, 30)
angle = 0
PAUSE = True
pos = 0
N = 0
a = -1
volume = -1
class Btn():
def __init__(self, x, y, img):
self.x = x
self.y = y
self.img = img
def paint(self, screen):
screen.blit(self.img, (self.x, self.y))
def click(self, state):
if state:
self.x += 2
self.y += 2
else:
self.x -= 2
self.y -= 2
btns = []
btns.append(Btn(20,580,prev))
btns.append(Btn(150, 580, play))
btns.append(Btn(280, 580, stop))
btns.append(Btn(410, 580, next))
def renderText(text, position):
my_font = pygame.font.Font('font/Regular.ttf', 40)
text = my_font.render(text, True, (255, 0, 0))
screen.blit(text, position)
while True:
screen.blit(bg, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_x,mouse_y = pygame.mouse.get_pos()
print(mouse_x,mouse_y)
if 25 < mouse_x <65 and 590 len(song_list)-1:
N =0
song_list[N].play()
elif 315 < mouse_x < 337 and 15 1:
volume = 1
song_list[N].set_volume(volume)
a = -1
elif 190 < mouse_x < 211 and 15 < mouse_y < 35:
volume -= 0.1
if volume < 0:
volume =0
song_list[N].set_volume(volume)
a = -1
else:
a= -1
if event.type == pygame.MOUSEBUTTONUP:
if a != -1:
btns[a].click(False)
if PAUSE:
pos = 25
else:
angle -= 1
pos = 10
newdisk = pygame.transform.rotate(disk,angle)
newRect = newdisk.get_rect(center = diskRect.center)
newpointer = pygame.transform.rotate(pointer,pos)
screen.blit(newdisk,newRect)
screen.blit(newpointer,pointerRect)
for b in btns:
b.paint(screen)
renderText(songs[N],(150,450))
pygame.display.update()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn