2
\$\begingroup\$

In my game you collect coins (surprise!). At the moment I generate them like this:

  • Find a random position given a rectangle (eg. the screen size) and generate a coin
  • Possible positions are left and right of this coin for our next coin
    • If both are available, choose at random
    • If a coin exists in one position, use the other
    • If neither are available skip this step
  • Do the same for up and down positions of the new coin
  • Repeat this sequence for all available coins

enter image description here

This works fine but I would like to create custom shapes with my coins like arrows, stars etc. This got me thinking into how I could achieve this. One way I thought of was to use a small texture where each coloured pixel represented the position of a coin. So that a picture like this:

enter image description here

Can be used to generate an array of coin position coordinates in any framework supporting textures. I'm pretty sure this can be done, but was wondering if anyone has tried this or something totally different for generating coins or any other objects in a game. Ideally the game would involve several different textures and choose them at random, and combine this with the random scattering textures from simple algorithms such as the one I have above.

\$\endgroup\$
3
  • 6
    \$\begingroup\$ This is effectively just applying the concept of a tilemap to the layout of collectible items in your game. Yes, it's a good technique--whether your store the data as an image or CSV or whatever makes little difference. Is there something specific you wanted to ask? \$\endgroup\$ Commented Feb 25, 2012 at 15:55
  • \$\begingroup\$ How would I go about storing something like this in a csv file, and would this be faster than loading a texture from a png? \$\endgroup\$ Commented Feb 26, 2012 at 1:43
  • \$\begingroup\$ @AramKocharyan - Storing it in a binary file would probably be the fastest. You basically just store a 2D array in the file. Since you're just using two values ("coin" or "no coin") you could store it down to the bit level. So, say your file has one byte at the beginning to indicate how many bits each row is, then you can loop through the file reading 1 byte at a time and converting those bits to a 2D array of bools that you could use to draw the coins. Of course, you'd need a custom editor to make the file but it might be worth it. \$\endgroup\$ Commented Feb 26, 2012 at 16:26

1 Answer 1

2
\$\begingroup\$

In one of my projects, as I was starting out, I wanted to make something right away and not get bogged down with technicalities. I was using Java and ImageIcons, so when it came to loading level data, I simply drew the level in Paint at one pixel per tile and parsed that into my array.

It was a hacky solution with zero thought involved, but in the end worked out quite well. Bitmaps are very easy to edit, and the different channels can work as layers easily. Most languages tend to have relatively simple methods for reading pixels as well. For complex maps with very varied tiles, it wouldn't work very well, but I don't think that is the case for you.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks! I've gone with that kind of option. I put several "bitmaps" into a PNG and for now when alpha != 0 I know there's a coin there. I then load different sections of the PNG like a sprite sheet, but I had to write my own code to handle that. \$\endgroup\$ Commented Mar 1, 2012 at 9:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .