Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Exchange / Opening all the images in a folder and drawing (newbie)
- - By Beamerr Date 2017-08-17 11:02 Edited 2017-08-17 11:08
Apologies for my ignorance but i'm relatively new to coding.

I'm trying to pull all the images from a folder (in the game directory) and draw them to the screen in a grid. I've tried putting them into an array and then passing through Gosu::Image.new and drawing but it seems to be converting them to strings.

class Board

  def initialize window

    @images = Dir.foreach "PNG-cards-1.3" do |x|
      Gosu::Image.new x
    end
  end

  def update
   
  end

  def draw
    @images.each do |y|
      y.draw 0, 0, 0
    end
  end
end

In researching this I have real problem understanding the rules about which objects/blocks will accept others and what happens when passing. Any help in this area or pointers to decent and jargon light explanations on this would be really helpful. Thanks!
Parent - - By bestguigui Date 2017-08-17 12:38
Hi !

you can use a Dir.foreach method to load the assets of the directory, like you are trying to do like this :

@images = Array.new
Dir.foreach("PNG-cards-1.3") do |x|
  @images.push Gosu::Image.new(x)
end

then, when you'll want to display the images, you'll be able to loop though your @images.
Parent - - By Beamerr Date 2017-08-17 13:14
Awesome thanks!

I tried that and it said unknown image type so I tried:

Dir.glob("PNG-cards-1.3/*.png")...

instead and it only returned the last card in the directory (obviously because of the push, in fact only showing the first). To be honest I was just relieved that something appeared on the screen! I'm sure I'll figure it out.

Thanks for your help
Parent - - By bestguigui Date 2017-08-17 13:52 Edited 2017-08-18 08:41
sorry, I always forget...

@images = Array.new
Dir.foreach("PNG-cards-1.3") do |x|
  if x.include?('.png')
    @images.push Gosu::Image.new("PNG-cards-1.3/#{x}")
  end
end

otherwise you try to load the "." and ".." that Dir returns...
Parent - - By Beamerr Date 2017-08-17 14:14
No worries.

I get a runtime error cannot open file 10_of_clubs.png. I don't really know why it can't access it at this point. I tried ("*.png") as well and it returned nil, so perhaps glob is the way to go?
Parent - - By jlnr (dev) Date 2017-08-17 19:16
In the above loop, x will be "10_of_clubs.png", but you cannot load Gosu::Image.new("10_of_clubs.png") because the subdirectory is missing. Try Gosu::Image.new("PNG-cards-1.3/" + x).

The Dir.glob approach works a little better in that regard because it will include the full path. I'm not sure why that didn't work, it should.
Parent - By bestguigui Date 2017-08-18 08:20 Edited 2017-08-18 08:41
Typing code without testing is really the best way to write silly things... also forgot the path in my sample, great ! I edited...
Up Topic Gosu / Gosu Exchange / Opening all the images in a folder and drawing (newbie)

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill