Skip to content

Commit

Permalink
Add sounds #4
Browse files Browse the repository at this point in the history
  • Loading branch information
felipemfp committed Aug 9, 2016
1 parent 6f5fa84 commit 8ba026d
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 33 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions src/car.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/data/data.json
Original file line number Diff line number Diff line change
@@ -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}}
{"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}}
22 changes: 18 additions & 4 deletions src/gameState.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/garageState.rb
Original file line number Diff line number Diff line change
@@ -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 = [
Expand All @@ -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

Expand All @@ -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

Expand All @@ -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)
Expand Down
24 changes: 12 additions & 12 deletions src/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down
Binary file added src/media/sounds/ambulance.wav
Binary file not shown.
Binary file added src/media/sounds/police.wav
Binary file not shown.
2 changes: 1 addition & 1 deletion src/menuState.rb
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down
10 changes: 6 additions & 4 deletions src/player.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/racing.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require_relative 'main'

module Racing
def Racing.open
def self.open
window = MainWindow.new
window.show
end
Expand Down
4 changes: 2 additions & 2 deletions src/state.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class State
def initialize(options={})
def initialize(options = {})
@main = options[:main]
end

Expand All @@ -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

0 comments on commit 8ba026d

Please sign in to comment.