Python - Pygame Adding Sound To The Game

In this tutorial we will do the Adding Sound to the Game. Pygame is a Free and Open Source python programming language framework for making game applications. It is highly portable and runs on nearly every platform and operating system. It is highly recommended for beginner to learn Pygame when making basic games. So let's now do the coding...

Getting Started

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/. After Python IDLE's is installed, open the command prompt then type "python -m pip install pygame", and hit enter. tut1

Importing Modules

After setting up the installation, run the IDLE and click file and then new file. After that a new window will appear containing a black file this will be the text editor for the python. Then copy code that I provided below and paste it inside the IDLE text editor
  1. import pygame, os
  2. from pygame.locals import *

Writing The Variables

We will assign the a certain variables that we will need to declare later in the main loop. To do that just kindly write this code inside your text editor.
  1. # Game Initialization
  2. pygame.init()
  3.  
  4. # Initialize Sound Files
  5. pygame.mixer.music.load('Sounds/sound.wav')
  6.  
  7.  
  8. # Center the Game Application
  9. os.environ['SDL_VIDEO_CENTERED'] = '1'
  10.  
  11. # Game Resolution
  12. screen_width=800
  13. screen_height=600
  14. screen=pygame.display.set_mode((screen_width, screen_height))
  15.  
  16. # Color
  17. white=(255, 255, 255)
  18. black=(0, 0, 0)
  19. yellow=(255, 255, 0)
  20.  
  21. #Text Renderer
  22. def text_format(text, textFont, textSize, textColor):
  23.     font=pygame.font.Font(textFont, textSize)
  24.     newText=font.render(text, 0, textColor)
  25.     return newText
  26.  
  27. # Framerate
  28. clock=pygame.time.Clock()
  29. fps=30
  30.  
  31. # Fonts
  32. font='fonts/Retrotown.ttf'

Main Function

This is the Main Code for the Pygame application. The code contains a several input function that will allow to act accordingly and render to the application. To do that just write this code inside the IDLE text editor.
  1. def main_menu():
  2.     menu=True
  3.     selected=""
  4.     while menu:
  5.         for event in pygame.event.get():
  6.             if event.type==pygame.QUIT:
  7.                 pygame.quit()
  8.                 quit()
  9.             if event.type==pygame.KEYDOWN:
  10.                 if event.key==pygame.K_ESCAPE:
  11.                     pygame.quit()
  12.                     quit()
  13.                 if event.key==pygame.K_UP:
  14.                     selected="play"
  15.                 elif event.key==pygame.K_DOWN:
  16.                     selected="stop"
  17.                 if event.key==pygame.K_SPACE:
  18.                     if selected=="play":
  19.                         # Play The Sound
  20.                         pygame.mixer.music.play(-1, 0.0)
  21.                     elif selected=="stop":
  22.                         # Stop The Sound
  23.                         pygame.mixer.music.stop()
  24.  
  25.         screen.fill(black)
  26.         title=text_format("Pygame - Adding Sound To The Game", font, 40, white)
  27.         if selected=="play":
  28.             text_play = text_format("PLAY", font, 50, yellow)
  29.         else:
  30.             text_play = text_format("PLAY", font, 50, white)
  31.  
  32.         if selected=="stop":
  33.             text_stop=text_format("STOP", font, 50, yellow)
  34.         else:
  35.             text_stop = text_format("STOP", font, 50, white)
  36.  
  37.         title_rect=title.get_rect()
  38.         play_rect=text_play.get_rect()
  39.         stop_rect=text_stop.get_rect()
  40.  
  41.         screen.blit(title, (screen_width/2 - (title_rect[2]/2), 50))
  42.         screen.blit(text_play, (screen_width/2 - (play_rect[2]/2), 250))
  43.         screen.blit(text_stop, (screen_width/2 - (stop_rect[2]/2), 400))
  44.         pygame.display.update()
  45.         clock.tick(fps)
  46.         pygame.display.set_caption("Pygame - Adding Sound To The Game")

Initializing The Game

This is the Initialization code, this will run the entire code when the application start. It will render the Main Loop to display the specific methods. To do that just write this code inside the IDLE text editor.
  1. # Initialize The Game
  2. main_menu()
  3. pygame.quit()
  4. quit()
There you have it we successfully Add the Sound To The Game. I hope that this simple tutorial help you to what you are looking for and enhance your programming capabilities. For more updates and tutorials just kindly visit this site. Enjoy Coding!!
Tags

Add new comment