STREAMLINING SOUND
In this tutorial we cover how to create procedures to insert the sounds of weapons during combat. While this might make for a more immersive experience, it’s not very efficient because it requires you to create a specific procedure for every single weapon. If you want to streamline your sound process, add a new sound_path attribute to the Weapon class:
class Weapon(Item): def __init__(self, name, itemdesc, updroomdesc, portable, revealsitem, usedin, usedesc, removesroomitem, addsroomitem, useroomdesc, disposable, damage, sound_path):
super().__init__(name, itemdesc, updroomdesc, portable, revealsitem, usedin, usedesc, removesroomitem, addsroomitem, useroomdesc, disposable) self.damage = damage self.sound_path = sound_path
You can now add the path to the sound file for each weapon:
sword = Weapon(“Sword”, “A sharp and sturdy sword.”, None, True, None, 0, “You swing the sword around with great skill.”, None, None, None, False, 2, “/home/nate/mu_code/sword.wav”)
You can now replace individual sound procedures, such as play_sword_clang, with a more generic one: def play_player_weapon_sound(): sound_file_path = player.weapon.sound_path playerweapon_sound = pygame.mixer.Sound(sound_file_
path) playerweapon_sound.play() return
Finally, you need to modify the fight procedure to play the correct sound for the weapon the player is currently wielding each time they use it:
while enemy.alive and player.hp > 0: player_damage = player.weapon.damage enemy.hp -= player_damage play_player_weapon_sound()
For convenience, you can download an example of this more efficient code from: https://github.com/azuregate/lxfpythontextadventure/blob/ main/nextroomsoundscript2.py
bar at other times as well, such as during the
fight procedure.
enemy_damage = enemy.weapon.damage player.hp -= enemy_damage typewriter_effect(f”\033[31mThe {enemy.name} hits you with its {enemy.weapon.name}. It causes {enemy_ damage} damage.\033[0m”) showhpbar()
As things stand, the HP bar is always displayed in green. If you want to colour-code it depending on how many hit points you have remaining, add the following:
if player.hp >4D
Finally, change the last print command in the
showhpbar procedure to:
print(hp_color + f”HP: [{bar}] {player.hp}/10 HP”)
You can download an example of this script that shows the HP power bar in the player’s inventory and during combat from: https://github.com/azuregate/ lxfpythontextadventure/blob/main/ testroomprogressbar1.py
Sounds and sorcery
Although text adventures aren’t especially rich in media, some, like Sherlock Holmes: The Riddle of the Crown Jewels, put sound effects to good use, such as by playing a cacophony of discordant notes if Doctor Watson tries to play Holmes’s Stradivarius violin.
Sound is one of the many supported features in Pygame – a collection of modules that, believe it or not, are specifically designed to help Python game creators. To get started, make sure you have Pygame installed by opening the terminal and running:
$ sudo apt install python3-pygame
Next, open your script and add the following line at the start:
import pygame
Initialise Pygame by adding:
pygame.init()
If you take the time to download the example script from https://github.com/azuregate/lxfpython textadventure/blob/main/nextroomsoundscript1.py,
you’ll see that there is a dedicated procedure for playing the sound of a sword each time the player uses it in combat:
def play_sword_clang(): sound_file_path = “/home/nate/mu_code/sword.wav” sword_sound = pygame.mixer.Sound(sound_file_path) sword_sound.play() return
As you can see here, the path to the sound file
sword.wav (which you can also retrieve from Nate’s GitHub repo) needs to be modified to make it work with your game. Make sure the media file is in the same folder as your Python script.
You can now modify the fight procedure to play the sound: while enemy.alive and player.hp > 0: player_damage = player.weapon.damage enemy.hp -= player_damage play_player_weapon_sound() typewriter_effect(f”\033[32mYou hit the {enemy. name} with your {player.weapon.name}. It causes {player_damage} damage.\033[0m”)
The example script also supports playback of a file
mace.wav for whenever the enemy hits your player during combat.
These effects work well for the example script but aren’t the most efficient way to code, given you have to create and invoke a new procedure to play each sound effect. See the Streamlining Sound box (opposite page) for a more efficient implementation of this.
Further reading
By now, you’ll be well on your way to a visually stunning text adventure. In this tutorial, we’ve only made a few small tweaks to sample scripts in order to encourage you to be creative.
For instance, sound effects only currently play during combat, but you may want them to do so when players use stat items, which increase or decrease their health. Consider also colour-coding the various exits in the game to assign shades to each direction, because this may make it easier to navigate.
You also may want to experiment more with text effects, changing sizes and the way in which they’re displayed. Although text adventures usually shy away from full-blown images (see Introducing Images box, page 91), you could consider using some ASCII art.
Once you’ve finished your game creation, please don’t hesitate to contact us at Linux Format to show off your sprawling Python adventure.