Linux Format

STREAMLINI­NG 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, updroomdes­c, portable, revealsite­m, usedin, usedesc, removesroo­mitem, addsroomit­em, useroomdes­c, disposable, damage, sound_path):

super().__init__(name, itemdesc, updroomdes­c, portable, revealsite­m, usedin, usedesc, removesroo­mitem, addsroomit­em, useroomdes­c, 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 playerweap­on_sound = pygame.mixer.Sound(sound_file_

path) playerweap­on_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 convenienc­e, you can download an example of this more efficient code from: https://github.com/azuregate/lxfpythont­extadventu­re/blob/ main/nextroomso­undscript2.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/ lxfpythont­extadventu­re/blob/main/ testroompr­ogressbar1.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 Stradivari­us violin.

Sound is one of the many supported features in Pygame – a collection of modules that, believe it or not, are specifical­ly 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 textadvent­ure/blob/main/nextroomso­undscript1.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 Streamlini­ng Sound box (opposite page) for a more efficient implementa­tion 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 Introducin­g 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.

 ?? ?? Although we’re against images in text adventures in general, there’s no reason you can’t include a helpful in-game map.
Although we’re against images in text adventures in general, there’s no reason you can’t include a helpful in-game map.
 ?? ?? The Pygame library supports sound playback. Here the sword and mace sounds can be called via separate procedures. You can find a more efficient implementa­tion of sound in the box, below-left.
The Pygame library supports sound playback. Here the sword and mace sounds can be called via separate procedures. You can find a more efficient implementa­tion of sound in the box, below-left.

Newspapers in English

Newspapers from Australia