How you think it's better to write code for having a matrice of sprites



6 years ago

Hello, i would have an array of 3 x 7 sprites.

I could have a sub to manage it but i would make something clean and would manage my sprites in an array.

The sprites don't move and have a x and y coordinates, a width, a height in pixel, a colon between 1 to 3, a position between 1 to 7 and of course data of the sprites to show.

For you what is the best method to manage it ?

I have to manage some sprites of this type at a same times (maybe 12 at the same times on the screen)

Thanks for your methods and advices.


NEW 6 years ago

Et surtout dites moi si mes explications de ce que je voudrai faire sont incompréhensibles ou si vous les voulez en français ;)


NEW 6 years ago

Personnellement, je n'ai pas bien compris les explications.


NEW 6 years ago

One way would be to load the sprite images the regular way as shown in the Images tutorial, then have an array of custom sprite objects that each holds info such as: what image to draw, the position in the 3x7 array, etc. The sprites array holds the maximum number of sprites you could ever have on the screen at the same time (like 12).

Here's what I mean in code:

// Load images the normal way
Image image1(image1Data);
Image image2(image2Data);

// Define a sprite structure
struct Sprite {
    Image* image; // pointer to one of the Image objects above
    int arrayX, arrayY; // position on the 3x7 grid

// At most 12 sprites on screen at the same time
#define MAX_SPRITES 12

// An array of all screen sprites, not all might be active
struct Sprite sprites[MAX_SPRITES];

// Only the first numActive from the array above are active
int numActive; // 0 <= numActive <= MAX_SPRITES

void setup() {
    // This level will use 2 sprites
    // Both sprites could point to the same Image object
    numActive = 2;

    sprites[0].image = &image1;
    sprites[0].arrayX = 0;
    sprites[0].arrayY = 2;

    sprites[1].image = &image2;
    sprites[1].arrayX = 1;
    sprites[1].arrayY = 2;

void loop() {
    // Draw active sprites
    for(int s = 0; s < numActive; s++) {
        // Dereference Image object pointer and pass it to drawImage


NEW 6 years ago

Ouch, my last comments as not been saved. I should have move befoe send it... It's time for me to thanks you for your answer alxm and sory for this late message. I didn't known that my answer hadn't be sent. I like your structured principe. I'll try it.


NEW 6 years ago

If all the sprites are the same time you could make them separate frames of one image and have the metadata in some struct array:

Image sprites(spriteData); // all the sprites
struct SpriteMetadata {
    int arrayX, arrayY;
const SpriteMetadata spriteMetadata[] = {
  {0, 0},
  {42, 1337},


NEW 6 years ago

No Sorunome it's to simulate lcd game. Each sprite as a position and can be on or off. So i have to load them and have to save the position they have to be display. The structure poposed by alxm is fine i think (i will not use the loop to display them as he could not know what i would do with but the structure is fine. Is it possible to make a loop to load the sprites array with the data (name of the image, x and y) ?