I'm new to Gosu. I'm trying to write a tile-based engine, and what's probably a fairly obvious problem has arisen for me. I can't write a separate variable for each of my 192 tiles! Is there a way to simply stamp an image onto the screen?
What do you mean by stamping? :?
I mean, can I take an image, place it somewhere (say, 0, 0), and then make a copy of it there, so that I can move my object away with a duplicate of it still at 0, 0.
You may want to look at the
CptnRuby.rb example. It has a tile map, and you could stamp tiles by just putting them in the tile array at the right place. Level editors usually work this way.
Well, to make "variables" for tiles, you could simply put them into array.
But for stamping, as you called, you can use:
@tiles=Window.record(map_width, map_height) do
## draw tiles here
And then draw
@tiles as image.
I am using an array for the tiles. My problem is finding a way to get all of my 100+ tiles that will be shown on the screen without having to assign each tile image its own variable.
Edit: Well actually, I suppose that I can just use ranges. Yeah, that should work.
you can draw an image multiples times, just call image.draw with different position
EDIT: Wait, perhaps I just confused myself... it's rather late over here. I'll just keep this post here just in case it's useful to someone :P
I believe what you're asking is the ability to access 100 unique types of tiles without having separate variables for each.
In that case you have two options:
1) Assuming the tiles are all the same size, put them into a spritesheet. Load the sheet using Gosu::Image#load_tiles(), and then reference the tiles by indexing into the returned array.
2) If they are not the same size, or you prefer to reference them by name, you can resort to a more complex solution. Store your tiles in a hash, where the keys are the names of the tiles, and the values are Gosu::Image instances. I think this is probably overkill, but if that's what you want, then go right ahead.
Thanks for the reply, but it's been solved on another forum. This was (mostly) my post:
Images can be drawn multiple times to the screen; any number of times or not at all. Take this for example:
# Lets say that my_image is a 100x100 size image, and we want to stamp it 10x10 times
for x in 1..10
for y in 1..10
w, h = my_image.width, my_image.height
# Multiply the sizes so the "stamps" don't overlap
my_image.draw(x * w, y * h, 0)
EDIT: Oh also, all the copies will stay until the screen is wiped (after each draw call)
The last line was his main issue.
Check on Coders' shed; I answered it there. ;)
Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill