More Sounds - noooway/love2d_arkanoid_tutorial GitHub Wiki
In this part, more sound effects are added.
So far, apart from music, sounds have been played only on ball-brick collisions. Besides, there has been only a single sound for each brick type. To add some diversity, sound effects for other collision types can be implemented and instead of a single sound, several effects for each collision event can be provided to choose from.
I'll continue to use love.audio.Source
s directly.
However, several modules, such as SLAM and TEsound are available to simplify sound management. Check out another possibilities at the libraries list on the LÖVE wiki.
New sounds for ball-brick collisions are grouped in tables according to brick type:
local simple_break_sound = {
love.audio.newSource(
"sounds/simple_break/recordered_glass_norm.ogg",
"static"),
love.audio.newSource(
"sounds/simple_break/edgardedition_glass_hit_norm.ogg",
"static") }
local armored_hit_sound = {
love.audio.newSource(
"sounds/armored_hit/qubodupImpactMetal_short_norm.ogg",
"static"),
love.audio.newSource(
"sounds/armored_hit/cast_iron_clangs_14_short_norm.ogg",
"static"),
love.audio.newSource(
"sounds/armored_hit/cast_iron_clangs_22_short_norm.ogg",
"static") }
local armored_break_sound = {
love.audio.newSource(
"sounds/armored_break/armored_glass_break_short_norm.ogg",
"static"),
love.audio.newSource(
"sounds/armored_break/ngruber__breaking-glass_6_short_norm.ogg",
"static") }
local ball_heavyarmored_sound = {
love.audio.newSource(
"sounds/heavyarmored_hit/cast_iron_clangs_11_short_norm.ogg",
"static"),
love.audio.newSource(
"sounds/heavyarmored_hit/cast_iron_clangs_18_short_norm.ogg",
"static") }
Sounds when bonus is picked:
local bonus_collected_sound = {
love.audio.newSource("sounds/bonus/bonus1.wav", "static"),
love.audio.newSource("sounds/bonus/bonus2.wav", "static"),
love.audio.newSource("sounds/bonus/bonus3.wav", "static")
}
Ball-wall collision sound:
local ball_wall_sound = love.audio.newSource(
"sounds/ball_wall/pumpkin_break_01_short_norm.ogg",
"static")
After the sounds are loaded, it is necessary to select and play one of them. Selection is random, and several random number generators are created specially for this purpose.
For ball-brick collisions:
local snd_rng = love.math.newRandomGenerator( os.time() ) --(*1)
function bricks.brick_hit_by_ball( i, brick, shift_ball, bonuses, score_display )
if bricks.is_simple( brick ) then
.....
table.remove( bricks.current_level_bricks, i )
local snd = simple_break_sound[ snd_rng:random( #simple_break_sound ) ] --(*2)
snd:play()
elseif bricks.is_armored( brick ) then
bricks.armored_to_scrathed( brick )
local snd = armored_hit_sound[ snd_rng:random( #armored_hit_sound ) ]
snd:play()
elseif bricks.is_scratched( brick ) then
bricks.scrathed_to_cracked( brick )
local snd = armored_hit_sound[ snd_rng:random( #armored_hit_sound ) ]
snd:play()
elseif bricks.is_cracked( brick ) then
.....
table.remove( bricks.current_level_bricks, i )
local snd = armored_break_sound[ snd_rng:random( #armored_break_sound ) ]
snd:play()
elseif bricks.is_heavyarmored( brick ) then
local snd =
ball_heavyarmored_sound[ snd_rng:random( #ball_heavyarmored_sound ) ]
snd:play()
end
end
(*1): New random number generator
(*2): snd_rng:random( #simple_break_sound )
generates a random number between 1 and
the length of the simple_break_sound
. This number is passed as an index into the
simple_break_sound[ ..... ]
, which effectively selects a random element from the simple_break_sound
Same for the bonuses:
local snd_rng = love.math.newRandomGenerator( os.time() )
function bonuses.bonus_collected( i, bonus,
balls, platform,
walls, lives_display )
.....
table.remove( bonuses.current_level_bonuses, i )
local snd = bonus_collected_sound[ snd_rng:random( #bonus_collected_sound ) ]
snd:play()
end
Since there is only a single sound for ball-wall collisions, there is no need for random number generator.
function balls.wall_rebound( single_ball, shift_ball )
.....
balls.increase_speed_after_collision( single_ball )
ball_wall_sound:play()
end