6. Sound
Import the sounds
Section titled “Import the sounds”-
In the folder where you unzipped primeiro-jogo-recursos.zip, there are
tiro.wav,explosao.wavanddano.wav. -
Click the
soundsfolder, in the Recursos (Resources) panel, and drag the three files onto the panel: they go to the selected folder. If you prefer, right-click the folder and choose Importar som (WAV)… (import sound). -
Each sound shows up with a green ▶ next to it. Click it to listen.
Select a sound to see its details in the Inspetor (Inspector):
The Copiar código (copy code) button copies the line that plays the sound, for example Audio::play("tiro");. Then just paste it into your code.
Sound for the shot and the damage
Section titled “Sound for the shot and the damage”In Player.cpp, add two lines: one right after spawn<Bullet> and another in hurt(), after taking the life.
#include "Player.hpp"
#include "src/game.hpp"
#include <algorithm>
using namespace brasa;
void Player::onSpawn() { animSpeed = 0.2f; // the engine flame alternates between the 2 frames // Touching an Enemy costs one life (and the enemy goes away too). onCollide<Enemy>([this](Enemy& enemy) { if (hurt()) enemy.destroy(); });}
void Player::onUpdate() { // The actions are in the project settings (keyboard and gamepad). // axis returns from -1 to 1: -1 = the first action, 1 = the second. float dx = axis("esquerda", "direita"); float dy = axis("cima", "baixo"); if (dx != 0.0f && dy != 0.0f) { // on the diagonal, it moves at the same speed dx *= 0.7071f; dy *= 0.7071f; }
// clamp keeps the ship inside the screen (the ship is 16 px, so the margin is 8). const float maxX = static_cast<float>(roomWidth()) - 8.0f; const float maxY = static_cast<float>(roomHeight()) - 8.0f; x = std::clamp(x + dx * velocidade, 8.0f, maxX); y = std::clamp(y + dy * velocidade, 8.0f, maxY);
// held(action) is true while the key or the button is pressed. if (held("atirar") && canShoot_) { spawn<Bullet>(x, y - 8.0f, "Tiros"); // the shot is born in the Tiros layer, behind the ship Audio::play("tiro", {.volume = 0.4f}); canShoot_ = false; after(intervaloTiro, [this] { canShoot_ = true; }); // only shoots again a few steps from now } if (invincible_ > 0) --invincible_;}
void Player::onRender() { // Blinks while invincible: skips the drawing on some steps. if (invincible_ > 0 && (invincible_ / 4) % 2 == 0) return; drawSelf();}
bool Player::hurt() { if (invincible_ > 0 || isDestroyed()) return false; --state().lives; Audio::play("dano"); if (state().lives <= 0) { destroy(); } else { invincible_ = 90; // 1.5 s at 60 steps per second } return true;}
BRASA_ENTITY(Player);Audio::play("dano")plays the sound calleddano, which is the name of the file without the.wav.- The shot plays many times per second, so it goes quieter:
{.volume = 0.4f}plays at 40% of the volume.
Sound for the explosion
Section titled “Sound for the explosion”In Enemy.cpp, play the explosion inside kill():
#include "Enemy.hpp"
#include "src/game.hpp"
using namespace brasa;
void Enemy::onSpawn() { animSpeed = 0.1f; // random(a, b) picks a number between a and b: each enemy falls at a different speed. vspeed = random(0.8f, 1.6f); // When a Bullet touches me, the shot goes away and I die. // The other objects (Bullet, Player...) are already known here, with no #include. onCollide<Bullet>([this](Bullet& bullet) { bullet.destroy(); kill(); });}
void Enemy::onUpdate() { // roomHeight() is the height of the screen; past it (with some slack), the enemy got away. if (y > static_cast<float>(roomHeight()) + 16.0f) destroy();}
void Enemy::kill() { if (isDestroyed()) return; // avoids scoring twice on the same step state().score += pontos; Audio::play("explosao", {.volume = 0.5f}); destroy();}
BRASA_ENTITY(Enemy);Test it now
Section titled “Test it now”Press Play and play: the shot goes pew, the enemies explode and the ship complains when it takes damage.
What you learned
Section titled “What you learned”- To import sounds, just drag WAV files onto the Recursos panel, and the ▶ lets you listen right away.
Audio::play("name")plays a sound, and{.volume = ...}adjusts the volume. See Play sounds.- The Copiar código button in the Inspector already copies the finished line.
Next part: 7. Game over →
