Skip to content Skip to sidebar Skip to footer

Pygame Playlist Continuous In The Background

I'm trying to get background music for my game, but I can't seem to figure it out perfectly. I've used pygame in the past, but it was only for one song during the credits of my gam

Solution 1:

You can use a thread to play music in the background.

importthreadingmusicThread= threading.Thread(target=music)
musicThread.start()

If you ever want to stop the music without closing your game, you should kill the thread.

Solution 2:

You can set a pygame.mixer.music.set_endevent() which get posted in the event queue when the music finishes. Then you just choose another song. Something along these lines:

import os
import pygame
pygame.init()
pygame.mixer.init()

SIZE = WIDTH, HEIGHT = 720, 460
screen = pygame.display.set_mode(SIZE)

MUSIC_ENDED = pygame.USEREVENT
pygame.mixer.music.set_endevent(MUSIC_ENDED)


BACKGROUND = pygame.Color('black')


classPlayer:

    def__init__(self, position):
        self.position = pygame.math.Vector2(position)
        self.velocity = pygame.math.Vector2()

        self.image = pygame.Surface((32, 32))
        self.rect =  self.image.get_rect(topleft=self.position)

        self.image.fill(pygame.Color('red'))

    defupdate(self, dt):
        self.position += self.velocity * dt
        self.rect.topleft = self.position


defload_music(path):
    songs = []
    for filename in os.listdir(path):
        if filename.endswith('.wav'):
            songs.append(os.path.join(path, filename))
    return songs


defrun():
    songs = load_music(path='/Users/Me/Music/AwesomeTracks')

    song_index = 0# The current song to load
    pygame.mixer.music.load(songs[song_index])
    pygame.mixer.music.play()
    song_index += 1

    clock = pygame.time.Clock()
    player = Player(position=(WIDTH / 2, HEIGHT / 2))

    whileTrue:
        dt = clock.tick(30) / 1000for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a:
                    player.velocity.x = -200elif event.key == pygame.K_d:
                    player.velocity.x = 200elif event.key == pygame.K_w:
                    player.velocity.y = -200elif event.key == pygame.K_s:
                    player.velocity.y = 200elif event.type == pygame.KEYUP:
                if event.key == pygame.K_a or event.key == pygame.K_d:
                    player.velocity.x = 0elif event.key == pygame.K_w or event.key == pygame.K_s:
                    player.velocity.y = 0elif event.type == MUSIC_ENDED:
                song_index = (song_index + 1) % len(songs)  # Go to the next song (or first if at last).
                pygame.mixer.music.load(songs[song_index])
                pygame.mixer.music.play()

        screen.fill(BACKGROUND)

        player.update(dt)
        screen.blit(player.image, player.rect)

        pygame.display.update()

run()

So the actual solution is just 3 parts

  1. Create an event MUSIC_ENDED = pygame.USEREVENT.
  2. Tell pygame to post the event when a song finishes pygame.mixer.music.set_endevent(MUSIC_ENDED)
  3. Check for the event in the event queue for event in pygame.event.get(): if event.type == MUSIC_ENDED:

And then you're free to do whatever you desire.

Post a Comment for "Pygame Playlist Continuous In The Background"