rubylibgosu

Music and image failed to initialize


I was trying to make a multiple-choice music player using Gosu but the picture and music Iwanted would not initialize despite the program running, it showed a black screen. The single block of codes works:

require 'gosu'
require './input_functions'
class MW < Gosu::Window
  def initialize
    super 200, 135
    @beth = Gosu::Image.new("media/beth.jpg")
    @song = Gosu::Song.new("media/3rdmovement.mp3")
    @song.play
  end
  def draw
    @beth.draw(0, 0)
  end
end
window = MW.new
window.show

But adding the multiple choice elements would not work(note: read_integer_in_range is defined in input function, the name itself is self explanatory). Full code:

require 'gosu'
require './input_functions'
class MW < Gosu::Window
  def initialize
    super 200, 135
    @beth = Gosu::Image.new("media/beth.jpg")
    @dimitri = Gosu::Image.new("media/dimitri.png")
    @vil = Gosu::Image.new("media/vilva.png")

    @song = Gosu::Song.new("media/3rdmovement.mp3")
    @song2=Gosu::Song.new("media/2ndwaltz.mp3")
    @song3=Gosu::Song.new("media/1stseason.mp3")
    read_integer_in_range( "What song you want play
      1st Spring
      2nd Waltz
      3rd movement", 1, 3)
choice = gets.chomp.to_i()
    
 case choice
    when 1
      @song3.play
      @vil.draw(0, 0)
    when 2
      @song2.play
      @dimitri.draw(0, 0)
    when 3
      @song.play
      draw_beth()
end

  end
end

def draw_beth
  @beth.draw(0, 0)
end

window = MW.new
window.show

All of the Png/Jpg and mp3 file works just fine..

I tried separating the draw_beth to call it in case but it did not work. I hope some passing by could help me with this one


Solution

  • As I can see, you are creating a music player with GUI, and if you are doing so, you shouldn't use gets function, instead you should track for the cursor's position and return a test value; for example:

     def update
            @locs = [mouse_x, mouse_y]
            @cursor_choice_album = mouse_over_button(mouse_x, mouse_y)
        end
    
        def needs_cursor?; true; end
       
        def mouse_over_button(mouse_x, mouse_y)
            if ((mouse_x > 100 and mouse_x < 500) and (mouse_y < 500 and mouse_y > 100))
              return 1
        end
    

    then you can use the case condition in the "button down ID" function