Skip to content

Commit

Permalink
Add some sound effects.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed May 15, 2024
1 parent 13e02f0 commit 1e32ffe
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions examples/flappy-bird/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,34 @@ def reset!
@score = 0
end

def play_sound(name)
path = "/_static/#{name}.mp3"
self.script("new Audio(#{JSON.dump(path)}).play()")
end

def play_music
self.script(<<~JAVASCRIPT)
if (!this.music) {
this.music = new Audio('/_static/music.mp3');
this.music.loop = true;
this.music.play();
}
JAVASCRIPT
end

def stop_music
self.script(<<~JAVASCRIPT)
if (this.music) {
this.music.pause();
this.music = null;
}
JAVASCRIPT
end

def game_over!
play_sound("death")
stop_music

Highscore.create!(ENV.fetch("PLAYER", "Anonymous"), @score)

@prompt = "Game Over! Score: #{@score}. Press Space to Restart"
Expand Down Expand Up @@ -224,6 +251,10 @@ def step(dt)
if pipe.right < @bird.x && !pipe.scored
@score += 1
pipe.scored = true

if @score == 3
play_music
end
end

if pipe.intersect?(@bird)
Expand All @@ -234,6 +265,7 @@ def step(dt)
@bonus&.step(dt)

if @bonus&.intersect?(@bird)
play_sound("clink")
@score = @score * 2
@bonus = nil
elsif @bonus and @bonus.right < 0
Expand Down
Binary file added examples/flappy-bird/public/_static/clink.mp3
Binary file not shown.
Binary file added examples/flappy-bird/public/_static/death.mp3
Binary file not shown.
Binary file added examples/flappy-bird/public/_static/music.mp3
Binary file not shown.

0 comments on commit 1e32ffe

Please sign in to comment.