ruby - gosu 中的/usr/local/lib/ruby/gems/2.2.0/gems/gosu-0.10.4/lib/gosu/patches.rb :37:in `initialize' : Cannot open file ./media/image.jpg (运行时错误)

标签 ruby linux libgosu

我的代码没有给出所需的输出并显示上述错误。

require  'gosu'

 def media_path(file)
   File.join(File.dirname(File.dirname(
    __FILE__)),'media', file)
 end


#def media_path(file); File.expand_path "media/#{file}", File.dirname(__FILE__)
# end


 class Explosion
   FRAME_DELAY = 10 # ms
   SPRITE = media_path('space.png')

   def self.load_animation(window)
     Gosu::Image.load_tiles(
       window, SPRITE, 128, 128, false)
   end

   def initialize(animation, x, y)
     @animation = animation
     @x, @y = x, y
     @current_frame = 0
   end

   def update
     @current_frame += 1 if frame_expired?
   end

   def draw
     return if done?
     image = current_frame
     image.draw(
       @x - image.width / 2.0,
       @y - image.height / 2.0,
       0)
   end
 def done?
     @done ||= @current_frame == @animation.size
   end

   private

   def current_frame
     @animation[@current_frame % @animation.size]
   end

   def frame_expired?
     now = Gosu.milliseconds
     @last_frame ||= now
     if (now - @last_frame) > FRAME_DELAY
       @last_frame = now
     end
   end
 end

 class GameWindow < Gosu::Window
   BACKGROUND = media_path('image.jpg')

   def initialize(width=800, height=600, fullscreen=false)
     super
     self.caption = 'Hello Animation'
     @background = Gosu::Image.new(
       self, BACKGROUND, false)
     @animation = Explosion.load_animation(self)
     @explosions = []
   end

   def update
@explosions.reject!(&:done?)
     @explosions.map(&:update)
   end

   def button_down(id)
     close if id == Gosu::KbEscape
     if id == Gosu::MsLeft
       @explosions.push(
         Explosion.new(
           @animation, mouse_x, mouse_y))
     end
   end

   def needs_cursor?
     true
   end

   def needs_redraw?
     !@scene_ready || @explosions.any?
  end

   def draw
     @scene_ready ||= true
     @background.draw(0, 0, 0)
     @explosions.map(&:draw)
  end
 end

 window = GameWindow.new
 window.show

最佳答案

找不到./media/image.jpg,您确定它在那里,并且可以打开,并且是当前目录“.”你认为它是什么?

关于ruby - gosu 中的/usr/local/lib/ruby/gems/2.2.0/gems/gosu-0.10.4/lib/gosu/patches.rb :37:in `initialize' : Cannot open file ./media/image.jpg (运行时错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34639903/

相关文章:

ruby-on-rails - 如何将旧的 lambda 语法转换为新的 lambda 文字语法

linux kernel crypto API,在AES-GCM算法中如何设置aad的长度为零位?

ruby - 无法在 Ruby 中使用 Gosu 打印文本

ruby - ruby/libgosu 中的中心文本

ruby-on-rails - Rails AR 日期范围查询 - 夏令时 - 重叠

html - Nokogiri XML 到节点

ruby - 不存在关系时的postgres内部连接总和

linux - 如何在 Linux 上将 F# 与单声道一起使用?

c++ - C++ 中 mt19937 的平台依赖状态?

ruby - 在 FreeBSD 上安装 gosu