How To Play Background Music On Pyhame
Hey in that location! Today in this tutorial can we wait at adding groundwork music in the window using pygame. So let'south begin!
Pace ane: Download the music nosotros want to play
The first thing that nosotros need to do earlier we move on to the coding section, is downloading some music. Yous can choose whatsoever music you want. I went with some uncomplicated catchy groundwork music for now.
I have added the background music I am going to apply. You can hear it if you want to.
Just the trouble is using mp3
format for background music in pygame can get quite buggy and can outcome in weird squeaky and popping audio instead of the music.
So to prevent that we convert out mp3 format file to OOG
format which makes it a amend fit for pygame windows. You can observe the file format converters online where yous just have to upload your music file and the residuum is washed for free!
The oog file sounds just the same. I have added my wav
music file below for you to verify the same.
Step 2: Importing Modules
The first step in every program is to import the necessary modules. The code for the same is shown below.
import pygame from pygame.locals import * from pygame import mixer
We volition be importing pygame
and pygame.locals
module for smoothen functioning of the window. And we will exist importing the mixer
module to play the music.
Stride 3: Create the bones screen design and functionalities
To create the screen nosotros first initialize the pygame using init
part. We volition be loading the groundwork image using the image.load
part.
Then we will have a running loop that will put the background image and update the screen design. Nosotros will also add the QUIT window functionality with the help of effect handling.
In the end, nosotros will exist using the quit
part to destroy the pygame initiated earlier. The code for the same is shown below.
pygame.init() width = 1000 height = 500 window = pygame.display.set_mode((width,height)) bg_img = pygame.paradigm.load('Images/bg.png') bg_img = pygame.transform.scale(bg_img,(width,height)) runing = True while runing: window.blit(bg_img,(0,0)) for issue in pygame.issue.get(): if event.type == QUIT: runing = False pygame.display.update() pygame.quit()
The output screen of the lawmaking to a higher place is displayed below.
Step 4: Initialize Mixer in the program
The adjacent step is to initialize the mixer
in the plan and load the music using music.load
where the path of the music file is passed as a parameter.
After this, nosotros add the part named music.pla
y. Merely this won't offset the background music. For that, we need to add some more elements in the lawmaking which are covered in the later sections.
The code for the same is shown below.
mixer.init() mixer.music.load('Music File/bensound-summer_wav_music.wav') mixer.music.play()
Calculation Groundwork Music to a Pygame Window – Full Code
The final code is shown beneath:
import pygame from pygame.locals import * from pygame import mixer pygame.init() width = k peak = 500 window = pygame.display.set_mode((width,peak)) bg_img = pygame.image.load('Images/bg.png') bg_img = pygame.transform.scale(bg_img,(width,height)) mixer.init() mixer.music.load('Music File/bensound-summer_wav_music.wav') mixer.music.play() runing = Truthful while runing: window.blit(bg_img,(0,0)) for issue in pygame.consequence.get(): if event.type == QUIT: runing = Fake pygame.display.update() pygame.quit()
The Last Output
The output of the code is displayed in the video below. I have not put a whole over iii minute video.
Y'all can hear the music playing perfectly!
Decision
Congratulations! You successfully learned how to add background music to your screen! You tin effort out different music sounds and add it to your games build if you have any!
Thank y'all for reading!
How To Play Background Music On Pyhame,
Source: https://www.askpython.com/python-modules/pygame-adding-background-music
Posted by: matterfinge1992.blogspot.com
0 Response to "How To Play Background Music On Pyhame"
Post a Comment