Problem while opening player image

95 views
Skip to first unread message

Philippe R.

unread,
Mar 1, 2011, 4:33:59 PM3/1/11
to ruby4kids
Hello, I am trying to follow the first screencast (01 - Draw a Window
and a Player). My OS is Windows 7. I have coded the window and it was
displayed correctly, but when I tried to display the player:

<internal:lib/rubygems/custom_require>:29:in `require': no such file
to load --
player (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from my_game.rb:5:in `<main>'

So I wrote on my_game.rb at the first line: (as mentioned by the
important note)

$: << File.expand_path(File.dirname(__FILE__))

And the file player.rb was loaded correctly, but then I got the error:

C:/Users/Philippe/Documents/Ruby/player.rb:5:in `initialize': Invalid
PNG file (
RuntimeError)
from C:/Users/Philippe/Documents/Ruby/player.rb:5:in `new'
from C:/Users/Philippe/Documents/Ruby/player.rb:5:in
`initialize'
from my_game.rb:10:in `new'
from my_game.rb:10:in `initialize'
from my_game.rb:22:in `new'
from my_game.rb:22:in `<main>'

The weird things here is that the file itself is a PNG and not a JPG
as you wrote on player.rb (ln. 5):

@icon = Gosu::Image.new(@game_window, "images/player1_icon.jpg", true)

So I tried to change it to PNG instead of JPG on the code and I got
the program to run, but only the black box without the player at pos
(50,50,1).

I tried to convert the PNG to JPG and use JPG extension on the code,
instead of PNG and got:

C:/Users/Philippe/Documents/Ruby/player.rb:5:in `initialize': Invalid
PNG file (
RuntimeError)
from C:/Users/Philippe/Documents/Ruby/player.rb:5:in `new'
from C:/Users/Philippe/Documents/Ruby/player.rb:5:in
`initialize'
from my_game.rb:10:in `new'
from my_game.rb:10:in `initialize'
from my_game.rb:22:in `new'
from my_game.rb:22:in `<main>'

What am I doing wrong here? Thanks for your help.

Alberto Morales

unread,
Mar 1, 2011, 4:41:13 PM3/1/11
to ruby...@googlegroups.com
You are using Ruby 1.9.2. There is a note in the screencasts to add a line to the top of game.rb file to make this work.

Alberto.

** IMPORTANT NOTE IF YOU ARE USING RUBY 1.9.2 **

If you are getting an error like: "Can not find load path for player", then please add this line of code to the first line in the game.rb file.

$: << File.expand_path(File.dirname(__FILE__))  

Philippe R.

unread,
Mar 1, 2011, 4:42:50 PM3/1/11
to ruby...@googlegroups.com
I already did this (and mentioned it on my post). Do you mean that I should add the same line to my player.rb as well?
--
Abraços,
Philippe Rosa

Philippe R.

unread,
Mar 1, 2011, 4:44:32 PM3/1/11
to ruby4kids
Actually I just tried to add the same line to my player.rb file and it
is still crashing.

Alberto Morales

unread,
Mar 1, 2011, 4:45:37 PM3/1/11
to ruby...@googlegroups.com
Sorry, should have read the whole email.

It seems the image file might be corrupt. Can you try another .png image and drop it in the same directory.

Did you get the image file from our repository?

Alberto

Philippe R.

unread,
Mar 1, 2011, 4:48:21 PM3/1/11
to ruby...@googlegroups.com
I have tried 3 different images and none of them are working. Where can I download the exact image that you use on the screencast? Thanks again for your help.
--
Abraços,
Philippe Rosa

NeutralAngel

unread,
Mar 1, 2011, 5:05:51 PM3/1/11
to ruby4kids
Hi Philippe,

Try here:

https://github.com/ruby4kids/lessons/tree/master/my_game_folder/images

Nathan

On Mar 1, 4:48 pm, "Philippe R." <lyllo...@gmail.com> wrote:
> I have tried 3 different images and none of them are working. Where can I
> download the exact image that you use on the screencast? Thanks again for
> your help.
>
>
>
>
>
> On Tue, Mar 1, 2011 at 6:45 PM, Alberto Morales <amora...@opnet.com> wrote:
> > Sorry, should have read the whole email.
>
> > It seems the image file might be corrupt. Can you try another .png image
> > and drop it in the same directory.
>
> > Did you get the image file from our repository?
>
> > Alberto
>
> > On Mar 1, 2011, at 4:42 PM, Philippe R. wrote:
>
> > I already did this (and mentioned it on my post). Do you mean that I should
> > add the same line to my player.rb as well?
>
> Philippe Rosa- Hide quoted text -
>
> - Show quoted text -

Philippe R.

unread,
Mar 1, 2011, 5:22:14 PM3/1/11
to ruby...@googlegroups.com
Thanks for the update, but I still can't make it work. I downloaded the image file to the specific folder and got the same errors. This is the code that I am using:

player.rb:

class Player

    def initialize(game_window)
        @game_window = game_window
        @icon = Gosu::Image.new(@game_window, "images/player1_icon.png", true)
    end
   
    def draw
        @icon.draw
    end
   
end

PS: Using ...icon.png the black box is loaded. Using ...icon.jpg the program doesn't start (issuing the same error as mentioned before).

my_game.rb:

$: << File.expand_path(File.dirname(__FILE__))

require 'rubygems'
require 'gosu'
require 'player'

class MyGame < Gosu::Window
    def initialize
        super(300, 400, false)
        @player = Player.new(self)
    end

    def update
    end

    def draw
        @player.draw(50, 50, 1)
    end

end

window = MyGame.new
window.show
--
Abraços,
Philippe Rosa

Alberto Morales

unread,
Mar 1, 2011, 5:35:06 PM3/1/11
to ruby...@googlegroups.com
If think the image is called player1.png, not player1_icon.png; there is _icon in the filename.


change this:

@icon = Gosu::Image.new(@game_window, "images/player1_icon.png", true)

to:

@icon = Gosu::Image.new(@game_window, "images/player1.png", true)

and download a new image to be sure:

Philippe R.

unread,
Mar 1, 2011, 5:35:38 PM3/1/11
to ruby4kids
I just tried downgrading my Ruby installation from 1.92 to 1.87 and
the problem was still not solved. Does anyone have any other ideas?
(PS: My OS, Windows 7, is the 64-bit edition).

Philippe R.

unread,
Mar 1, 2011, 5:39:11 PM3/1/11
to ruby4kids
I had renamed the file to player1_icon.png so that it would find the file (which it did). But just to make sure I re-downloaded the file as player1.png placed it in the correct dir (/images) and changed the code as requested... I still get the black box without the player.
--
Abraços,
Philippe Rosa

Ynes Morales

unread,
Mar 1, 2011, 5:47:09 PM3/1/11
to ruby...@googlegroups.com
rewrite this part in the player.rb


class Player

    def initialize(game_window)
        @game_window = game_window
        @icon = Gosu::Image.new(@game_window, "images/player1_icon.png", true)
    end
    
    def draw
        @icon.draw(50, 50, 1)
    end
    
end


and this part in my_game.rb


require 'rubygems'
require 'gosu'
require 'player'

class MyGame < Gosu::Window
    def initialize
        super(300, 400, false)
        @player = Player.new(self)
    end

    def update
    end

    def draw
        @player.draw
        
    end

end

window = MyGame.new
window.show

Ynes

Philippe R.

unread,
Mar 1, 2011, 5:55:23 PM3/1/11
to ruby...@googlegroups.com
Thanks a lot. I realized I had the pos (50,50,1) in my_game.rb instead of player.rb. That did it! Working like a charm. Thank you all for the attention.
--
Abraços,
Philippe Rosa
Reply all
Reply to author
Forward
0 new messages