简体   繁体   English

音乐和图像初始化失败

[英]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.我试图用 Gosu 制作一个多选音乐播放器,但是尽管程序运行,但我想要的图片和音乐无法初始化,它显示黑屏。 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).但是添加多项选择元素是行不通的(注意:read_integer_in_range 在输入 function 中定义,名称本身是不言自明的)。 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..所有 Png/Jpg 和 mp3 文件都可以正常工作。

I tried separating the draw_beth to call it in case but it did not work.我尝试将draw_beth分开以防case ,但它没有用。 I hope some passing by could help me with this one我希望路过的人可以帮助我解决这个问题

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;如我所见,您正在使用 GUI 创建音乐播放器,如果这样做,则不应使用 gets function,而应跟踪光标的 position 并返回测试值; 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那么您可以在“按钮按下 ID”function 中使用案例条件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM