Creating the package

Create another __init__.py file, this time in your casino_sounds folder. Add the following code:

import os

import pygame
from casino import assets_folder

class SoundBoard:
def __init__(self):
pygame.init()
self.sound_folder = os.path.join(assets_folder, 'sounds')
self.place_sound = self.load_sound('cardPlace1.wav')
self.shuffle_sound = self.load_sound('cardShuffle.wav')
self.chip_sound = self.load_sound('chipsStack6.wav')

Our SoundBoard class will make use of the inbuilt os module for some file path validation, as well as make use of our newly-installed pygame. We also import the assets_folder from our casino package to allow us to store sound files inside it.

We begin our SoundBoard class's __init__ method by initializing pygame. Without this, our sound will not work.

The next thing we need is access to the folder that holds our audio files. I have placed these in the assets folder, under a new folder named sounds. We access this path by combining the string "sounds" with the assets_folder variable that we imported from our casino module. We use os.path.join to achieve platform-independent file paths.

Each sound we wish to play will become its own attribute of our SoundBoard class. We assign each attribute by passing the name of the audio file to a method called load_sound. Let's check this out:

def load_sound(self, sound):
file_location = os.path.join(self.sound_folder, sound)
if os.path.isfile(file_location):
return pygame.mixer.Sound(file_location)
else:
raise Exception('file ' + file_location + ' could not be found')

We combine the passed-in audio filename with the sounds folder, which we have saved as our sound_folder attribute. This creates the full path to the supplied audio file.

The audio files I have used in this chapter were obtained from the Open Game Art website, and are located at https://opengameart.org/content/54-casino-sound-effects-cards-dice-chips. Due to flexible licensing, they are also included in the assets folder for this chapter in the GitHub repository, which accompanies this book.

Before trying to load this with pygame, we need to check that it is indeed an existing file path. We achieve this using os.path.isfile. This will check that there is a file at the supplied path. If there is not, we will raise an Exception, letting the user know that they require this file in order to run the game.

If the isfile check passes, we can use pygame.mixer.Sound to create a playable Sound object, passing it along the full path to our specified audio file. This Sound object is what will be assigned to each class attribute.

Now that this is done, all we need to do to play one of these sounds is call .play() on our SoundBoard instance's relevant attribute.

That's all there is to adding sound effects to our blackjack game. We now have a reusable class that will play any sound effect we decide to put into our assets folder and assign as an attribute.

With the casino_sounds package finished, we have all of the necessary tools to begin refactoring our game.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset