Use pygame to implement a music player (1)

table of Contents

One, the installation of pygame

1 Graphical interface installation

2 Command line installation

3 The simplest structure of pygame

Second, the realization of the music player

1 Picture display

2 Play music


Pygame is a set of Python program modules used to develop game software, developed on the basis of the SDL library. Allows you to create feature-rich games and multimedia programs in Python programs. Pygame is a highly portable module that can support multiple operating systems.

Development environment: python3.7 + pycharm community edition

One, the installation of pygame

1 Graphical interface installation

Select File---settings in pycharm

2 Command line installation

Execute in terminal:

pip install pygame -i https://pypi.douban.com

3 The simplest structure of pygame

Create a new py file and type the code as shown below:

The basic structure of pygame consists of three parts:

  1. Initialize pygame
  2. Create game window
  3. Create a game loop

Second, the realization of the music player

The implemented player is as follows:

The structure of the project:

To implement the player, the following problems must be solved:

  1. How to realistic pictures
  2. How to play music
  3. How to judge whether the mouse has clicked the button
  4. How to display text
  5. How to implement a progress bar

1 Picture display

Images are very important resources in the game. Images may be some backgrounds, characters, etc. pygame supports the following image formats: jpeg, png, bmp, gif, etc. Usually to display a picture, there are three steps:

 # 显示背景
# 1.加载图片
background = pygame.image.load("images/player.png")  

# 2 把图片贴到窗口上
# 参数:要显示的图片对象,在目标窗口的坐标
screen.blit(background,(0,0))  

# 3 刷新屏幕
pygame.display.flip()  

Complete code:

# 导入pygame
import pygame

#rect描述对象位置,参数包括left(x坐标),top(y坐标),width,height
WINDOW = pygame.Rect(0,0,480,380)

def main():
    global index
    index = 0  # 默认是第一首歌曲的下标
    # 1. 初始化pygame的各个模块
    pygame.init()

    # 2 建立一个游戏窗口
    screen = pygame.display.set_mode(WINDOW.size)
    # 设置窗口标题
    pygame.display.set_caption("音乐播放器")

    # 显示背景
    background = pygame.image.load("images/player.png")  # 加载图片
    screen.blit(background,(0,0))  # 把background贴到窗口上
    pygame.display.flip()  # 刷新屏幕

    # 3 建立一个游戏循环
    running = True
    while running:
        # 处理用户的事件
        for event in pygame.event.get():
            # type事件类型
            if event.type == pygame.QUIT:
                running = False


if __name__ == '__main__':
    main()

You can see the interface after running:

2 Play music

To play music, use the mixer module, among which the common methods are:

pygame.mixer.music.load(file) 使用文件名作为参数载入音乐 ,音乐可以是mp3等格式
pygame.mixer.music.play()     播放载入的音乐pygame.mixer.music.stop() 停止播放,
pygame.mixer.music.pause()     暂停播放。pygame.mixer.music.unpause() 取消暂停。
pygame.mixer.music.set_volume(value) 来设置音量,音量value的范围为0.0到1.0。
pygame.mixer.music.get_volume() 获取音量大小。
pygame.mixer.music.get_pos()    获取播放的时长,单位毫秒

Because the song to be played is not a song, you can define a global variable to save the song list. Define a function to complete the loading of the song list

song_list = []  # 歌曲列表
# 加载歌曲列表
def load_songs():
    songs = os.listdir("music")
    songs = ["music/"+song for song in songs]
    song_list.extend(songs)
    
# 播放指定的歌曲
def play(index):  # index当前歌曲的索引
    pygame.mixer.music.stop()
    # 播放音乐
    music = pygame.mixer.music.load(song_list[index])
    pygame.mixer.music.play()

Main function:

def main():
    index = 0  # 默认是第一首歌曲的下标
    # 1. 初始化pygame的各个模块
    pygame.init()

    # 2 建立一个游戏窗口
    screen = pygame.display.set_mode(WINDOW.size)
    # 设置窗口标题
    pygame.display.set_caption("音乐播放器")

    # 显示背景
    background = pygame.image.load("./images/player.png")  # 加载图片
    screen.blit(background,(0,0))  # 把background贴到窗口上
    pygame.display.flip()  # 刷新屏幕

    # 加载歌曲列表并播放第一首歌曲
    load_songs()
    play(index)

    # 3 建立一个游戏循环
    running = True
    while running:
        # 处理用户的事件
        for event in pygame.event.get():
            # type事件类型
            if event.type == pygame.QUIT:
                running = False

Up to now, we can already play music

Guess you like

Origin blog.csdn.net/chengshaolei2012/article/details/114144064