gpt4 book ai didi

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 (运行时错误)

转载 作者:太空宇宙 更新时间:2023-11-04 06:00:48 26 4
gpt4 key购买 nike

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

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com