diff --git a/README.md b/README.md index 1b4375f..fa81d58 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,8 @@ ruby run.rb * [car-brake.wav](src/media/sounds/car-brake.wav) and [car-speed.wav](src/media/sounds/car-speed.wav) by [FreeSoundEffects.com](http://www.freesoundeffects.com/free-sounds/cars-10069/) * [menu.wav](src/media/sounds/menu.wav) by [Azure](http://www.flashkit.com/loops/Pop-Rock/Pop/Whatcha-Azure-10593/index.php) * [menu-option.wav](src/media/sounds/menu-option.wav) by [Independent.nu](http://opengameart.org/content/16-button-clicks) +* [ambulance.wav](src/media/sounds/ambulance.wav) by [Partners In Rhyme](http://www.freesoundeffects.com/free-track/amb-426608/) +* [police.wav](src/media/sounds/police.wav) by [Partners In Rhyme](http://www.freesoundeffects.com/free-track/police-siren-426661/) * [Need for Font.ttf](src/media/fonts/NeedforFont.ttf) by [Magique Fonts](http://www.1001fonts.com/need-for-font-font.html) ## Contributing diff --git a/src/car.rb b/src/car.rb index 7b497b1..ccb7e45 100644 --- a/src/car.rb +++ b/src/car.rb @@ -1,10 +1,12 @@ class Car - attr_reader :x, :y + attr_reader :x, :y, :song + attr_accessor :sample - def initialize(animation) + def initialize(animation_file, song_file) pos = [180.0, 255.0, 330.0] - @animation = animation - @x = pos[rand(pos.size)] + @animation = Gosu::Image.load_tiles(animation_file, 140, 140) + @song = Gosu::Sample.new(song_file) if song_file + @x = pos.sample @y = rand(100.0..170.0) * -1 @angle = 0.0 @vel = 2.5 diff --git a/src/data/data.json b/src/data/data.json index 475a4a1..feee546 100644 --- a/src/data/data.json +++ b/src/data/data.json @@ -1 +1 @@ -{"high_scores":[0,0,0,0,0],"game_over":["GAME OVER","SUCH A FOOL","SUCH A LOSER","You'll pay\nfor this!","Hey!\nDon't do that!","Pay attention!","Speed it\nup already,\njackass!","Wake the\nf*ck up, idiot!","Stay in\nyour lane!","Pick a speed\nand stick\nwith it!","You're\nan idiot!","Learn how\nto drive!","Thanks for\ncutting me\noff!","Blinker?"],"current_car":0,"config":{"sound":false}} \ No newline at end of file +{"high_scores":[39,39,0,0,0],"game_over":["GAME OVER","SUCH A FOOL","SUCH A LOSER","You'll pay\nfor this!","Hey!\nDon't do that!","Pay attention!","Speed it\nup already,\njackass!","Wake the\nf*ck up, idiot!","Stay in\nyour lane!","Pick a speed\nand stick\nwith it!","You're\nan idiot!","Learn how\nto drive!","Thanks for\ncutting me\noff!","Blinker?"],"current_car":2,"config":{"sound":true}} \ No newline at end of file diff --git a/src/gameState.rb b/src/gameState.rb index cf38464..b817203 100644 --- a/src/gameState.rb +++ b/src/gameState.rb @@ -16,7 +16,7 @@ def initialize(options = {}) @road = Road.new - @player = Player.new(Gosu::Image::load_tiles(CARS[@main.data['current_car']], 140, 140)) + @player = Player.new(CARS[@main.data['current_car']][0], CARS[@main.data['current_car']][1]) @player.warp(WIDTH / 2, HEIGHT - 90) @cars = [] @@ -27,6 +27,9 @@ def initialize(options = {}) @car_speed = Gosu::Song.new('src/media/sounds/car-speed.wav') @main.play_sound(@car_speed, true) + if @player.song + @player.sample = @main.play_sound(@player.song, true, 0.5) + end end def millis @@ -36,7 +39,12 @@ def millis def update if @alive if millis - @last_millis > @cars_interval - @cars << Car.new(Gosu::Image::load_tiles(CARS.sample, 140, 140)) + next_car = CARS.sample + car = Car.new(next_car[0], next_car[1]) + if car.song + car.sample = @main.play_sound(car.song, true, 0.3) + end + @cars << car @last_millis = millis end if millis / 1000 > @interval @@ -56,7 +64,10 @@ def update @road.move @cars.each(&:move) @cars.each do |car| - car = nil if car.y >= 512 + 140 + if car.y >= 512 + 140 + car.sample.stop if car.sample + car = nil + end end @cars = @cars.compact if @player.collision?(@cars) @@ -67,6 +78,7 @@ def update @main.data['high_scores'] << @player.score @main.data['high_scores'] = @main.data['high_scores'].sort.reverse.take(5) end + @player.sample.stop if @player.sample end @player.set_score(millis / 226) end @@ -84,11 +96,13 @@ def draw def button_down(id) if id == Gosu::KbEscape || id == Gosu::GpButton1 || !@alive @car_speed.stop - @cars.each do |_car| + @cars.each do |car| + car.sample.stop if car.sample car = nil end @cars = @cars.compact @player.set_score(0) + @player.sample.stop if @player.sample @main.state = 0 end end diff --git a/src/garageState.rb b/src/garageState.rb index 446aa25..95a053d 100644 --- a/src/garageState.rb +++ b/src/garageState.rb @@ -1,5 +1,5 @@ class GarageState < State - def initialize(options={}) + def initialize(options = {}) super options @option_font = Gosu::Font.new(15, name: 'src/media/fonts/NeedforFont.ttf') @options = [ @@ -18,7 +18,7 @@ def initialize(options={}) @option_sample = Gosu::Sample.new('src/media/sounds/menu-option.wav') @song = Gosu::Song.new('src/media/sounds/menu.wav') @main.play_sound(@song, true) - @car = Gosu::Image::load_tiles(CARS[@main.data['current_car']], 140, 140) + @car = Gosu::Image.load_tiles(CARS[@main.data['current_car']][0], 140, 140) @car_font = Gosu::Font.new(20, name: 'src/media/fonts/NeedforFont.ttf') end @@ -36,7 +36,7 @@ def draw @car_font.draw(caption, option[1], option[2], ZOrder::UI) end end - image = @car[Gosu::milliseconds / 100 % @car.size] + image = @car[Gosu.milliseconds / 100 % @car.size] image.draw_rot(355, 155, ZOrder::Cars, 0.0) end @@ -47,7 +47,7 @@ def button_down(id) @main.state = 0 else @main.data['current_car'] = @current_option - @car = Gosu::Image::load_tiles(CARS[@main.data['current_car']], 140, 140) + @car = Gosu::Image.load_tiles(CARS[@main.data['current_car']][0], 140, 140) end elsif id == Gosu::KbDown || id == Gosu::GpDown @main.play_sound(@option_sample) diff --git a/src/main.rb b/src/main.rb index 621900f..d13aae0 100755 --- a/src/main.rb +++ b/src/main.rb @@ -13,14 +13,14 @@ WIDTH = 512 HEIGHT = 512 CARS = [ - 'src/media/images/car.png', - 'src/media/images/ambulance.png', - 'src/media/images/audi.png', - 'src/media/images/black_viper.png', - 'src/media/images/mini_truck.png', - 'src/media/images/mini_van.png', - 'src/media/images/police.png', - 'src/media/images/taxi.png' + ['src/media/images/car.png', nil], + ['src/media/images/ambulance.png', 'src/media/sounds/ambulance.wav'], + ['src/media/images/audi.png', nil], + ['src/media/images/black_viper.png', nil], + ['src/media/images/mini_truck.png', nil], + ['src/media/images/mini_van.png', nil], + ['src/media/images/police.png', 'src/media/sounds/police.wav'], + ['src/media/images/taxi.png', nil] ].freeze module ZOrder @@ -46,12 +46,12 @@ def initialize @data = JSON.parse(File.read('src/data/data.json')) @is_sound_enable = @data['config']['sound'] - @current_state = @states[@state].new(main:self) + @current_state = @states[@state].new(main: self) end def update if @state != @last_state - @current_state = @states[@state].new(main:self) + @current_state = @states[@state].new(main: self) @last_state = @state end @current_state.update @@ -65,11 +65,11 @@ def button_down(id) @current_state.button_down(id) end - def play_sound(_song, _loop = false) + def play_sound(_song, _loop = false, _volume = 1, _speed = 1) if _song.is_a? Gosu::Song _song.play(_loop) if @is_sound_enable else - _song.play if @is_sound_enable + _song.play(_volume, _speed, _loop) if @is_sound_enable end end diff --git a/src/media/sounds/ambulance.wav b/src/media/sounds/ambulance.wav new file mode 100644 index 0000000..09231f5 Binary files /dev/null and b/src/media/sounds/ambulance.wav differ diff --git a/src/media/sounds/police.wav b/src/media/sounds/police.wav new file mode 100644 index 0000000..6f799d3 Binary files /dev/null and b/src/media/sounds/police.wav differ diff --git a/src/menuState.rb b/src/menuState.rb index 85e570b..95e4bd2 100644 --- a/src/menuState.rb +++ b/src/menuState.rb @@ -1,5 +1,5 @@ class MenuState < State - def initialize(options={}) + def initialize(options = {}) super options @font = Gosu::Font.new(25, name: 'src/media/fonts/NeedforFont.ttf') @options = [ diff --git a/src/player.rb b/src/player.rb index a725992..919499a 100644 --- a/src/player.rb +++ b/src/player.rb @@ -1,8 +1,10 @@ class Player - attr_reader :score + attr_reader :score, :song + attr_accessor :sample - def initialize(animation) - @animation = animation + def initialize(animation_file, song_file) + @animation = Gosu::Image.load_tiles(animation_file, 140, 140) + @song = Gosu::Sample.new(song_file) if song_file @x, @y, @angle = 0.0 @score = 0 @vel = 1.0 @@ -57,7 +59,7 @@ def collision?(cars) end def draw - image = @animation[Gosu::milliseconds / 100 % @animation.size] + image = @animation[Gosu.milliseconds / 100 % @animation.size] image.draw_rot(@x, @y, ZOrder::Player, @angle) end end diff --git a/src/racing.rb b/src/racing.rb index 13fcec6..215ee95 100644 --- a/src/racing.rb +++ b/src/racing.rb @@ -1,7 +1,7 @@ require_relative 'main' module Racing - def Racing.open + def self.open window = MainWindow.new window.show end diff --git a/src/state.rb b/src/state.rb index f8d275d..04f069d 100644 --- a/src/state.rb +++ b/src/state.rb @@ -1,5 +1,5 @@ class State - def initialize(options={}) + def initialize(options = {}) @main = options[:main] end @@ -11,7 +11,7 @@ def draw raise NotImplementedError, 'This is not implemented!' end - def button_down(id) + def button_down(_id) raise NotImplementedError, 'This is not implemented!' end end