From 64b6393376f4bd885e922bd8bc73b6ddd15ee53d Mon Sep 17 00:00:00 2001 From: Bruno Coelho Date: Mon, 17 Feb 2014 21:17:29 +0000 Subject: [PATCH] adds '--mute' option to the executable --- bin/rubyhop | 7 ++++++- lib/rubyhop.rb | 28 ++++++++++++++++------------ 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/bin/rubyhop b/bin/rubyhop index 87f3352..a3267a7 100755 --- a/bin/rubyhop +++ b/bin/rubyhop @@ -1,3 +1,8 @@ #!/usr/bin/env ruby require "rubyhop" -RubyhopGame.new.show + +if ARGV[0] == "--mute" + RubyhopGame.new(800, 600, false, false).show +else + RubyhopGame.new(800, 600, false, true).show +end diff --git a/lib/rubyhop.rb b/lib/rubyhop.rb index ea5d99b..d4e9071 100644 --- a/lib/rubyhop.rb +++ b/lib/rubyhop.rb @@ -6,7 +6,7 @@ def get_my_file file class Player attr_accessor :x, :y, :alive - def initialize level + def initialize level, sounds @level = level @window = @level.window # position @@ -14,8 +14,10 @@ def initialize level @gravity = -0.25 @hop = 7.5 # sounds - @sound = Gosu::Sample.new @window, get_my_file("hop.mp3") - @gameover = Gosu::Sample.new @window, get_my_file("gameover.mp3") + if sounds + @sound = Gosu::Sample.new @window, get_my_file("hop.mp3") + @gameover = Gosu::Sample.new @window, get_my_file("gameover.mp3") + end # images @rise = Gosu::Image.new @window, get_my_file("rubyguy-rise.png") @fall = Gosu::Image.new @window, get_my_file("rubyguy-fall.png") @@ -23,7 +25,7 @@ def initialize level end def hop if @alive - @sound.play + @sound.play unless @sound.nil? @velocity += @hop end end @@ -37,7 +39,7 @@ def die! if @alive # Set velocity to one last hop @velocity = 5.0 - @gameover.play + @gameover.play unless @sound.nil? @alive = false end end @@ -96,11 +98,13 @@ def draw class HopLevel attr_accessor :window, :movement, :score - def initialize window + def initialize window, sounds @window = window - @music = Gosu::Song.new @window, get_my_file("music.mp3") - @music.play true - @player = Player.new self + if sounds + @music = Gosu::Song.new @window, get_my_file("music.mp3") + @music.play true + end + @player = Player.new self, sounds @hoops = 6.times.map { Hoop.new self } init_hoops! @font = Gosu::Font.new @window, Gosu::default_font_name, 20 @@ -312,8 +316,8 @@ def draw class RubyhopGame < Gosu::Window VERSION = "1.3.1" attr_reader :time, :sounds, :score, :high_score - def initialize width=800, height=600, fullscreen=false - super + def initialize width=800, height=600, fullscreen=false, sound=true + super(width, height, fullscreen) self.caption = "Ruby Hop - #{VERSION}" @background = Gosu::Image.new self, get_my_file("background.png") @@ -323,7 +327,7 @@ def initialize width=800, height=600, fullscreen=false # Levels @title = TitleLevel.new self - @hop = HopLevel.new self + @hop = HopLevel.new self, sound @fail = FailLevel.new self @title.on_continue { play! }