TV Out - Background Collsision (Game)

Hello there.

I would like to know how to organize a collision.

How can you determine where Mario is now and if there are objects on the background, then some action follows.

Part code:

// player coordinates
int marioX, marioY;

// player direction
int marioDX = 1;
int marioDY = 1;

int sensorValue = analogRead(A0);
  
// LEFT BUTTON
if(sensorValue >= 120 && sensorValue < 135){
    marioX -= marioDX;
}
// RIGHT BUTTON
if(sensorValue >= 140 && sensorValue < 155){
    marioX += marioDX; 
}

TV.bitmap(marioX, marioY, mario_sprite);

I have an array of background:

PROGMEM const unsigned char background[] = {
128,18,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xF8,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0xFC,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xFE,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xFE,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xFE,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xFE,0x00,0x00,0x00,
0x00,0x00,0x1E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0xFE,0x00,0x00,0x00,
0x00,0x00,0x3F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xFF,0xFE,0x00,0x00,0x00,
0x00,0x00,0x3F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xFF,0xFE,0x00,0x00,0x00,
0x00,0x00,0x3F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xFF,0xFE,0x00,0x00,0x00,
0x00,0x00,0x3F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xFF,0xFE,0x00,0x00,0x00,
0x00,0x00,0x3F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xFF,0xFE,0x00,0x00,0x00,
0xFF,0xC0,0x3F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xE0,0x3F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xF0,0x3F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xF0,0x3F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xF0,0x3F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xF0,0x3F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
};

and mario sprite:

PROGMEM const unsigned char mario_sprite[] = {
16,16,
0x00,0x00,
0x03,0xE0,
0x06,0x7C,
0x07,0xD0,
0x0F,0xDC,
0x0F,0xEE,
0x0F,0xC4,
0x03,0xF8,
0x07,0xA8,
0x0F,0xFC,
0x1F,0xF8,
0x1F,0xF0,
0x1F,0xF0,
0x3E,0xE0,
0x21,0xC0,
0x01,0xE0 
};

After incrementing the marioX and Y, check those values to see if he is in the position you are interested in.

if(marioX >= background[marioX] ? && marioY >= background[marioY]) {
  marioDX = 0;
}

You can demonstrate the code so that I understand how it is done, i do not understand.

How can mario collide with the background? What value of the background indicates a solid object?

I need to somehow determine the coordinates of Mario and if it is in the area before 1 then stop moving
How can this be correctly calculated considering the screen resolution of 128x96.

unsigned char background_collision[] = {
0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,
0,0,1,0,0,0,0,0,0,0,1,1,1,0,0,0,
0,0,1,0,0,0,0,0,0,0,1,1,1,0,0,0,
0,0,1,0,0,0,0,0,0,0,1,1,1,0,0,0,
0,0,1,0,0,0,0,0,0,0,1,1,1,0,0,0,
0,0,1,0,0,0,0,0,0,0,1,1,1,0,0,0,
0,0,1,0,0,0,0,0,0,0,1,1,1,0,0,0,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
};

if(background_collision[marioX] == 1) {
  marioX -= marioDX; 
}

You have a single-dimension array. But you have drawn it in rows of 16 so it looks like 16x18. Is that correct?

Then maybe something like this...

if(background_collision[marioX + marioY*16] == 1) {
  //collided

I will continue to adapt the collision later, at first I optimize the generation of images.

I took 8 pixels from the top vertically and 16 pixels horizontally in my cycle for is 1 line from the top to get points and lives.

Thus the size of the sprite is 8x8 pixels 12 lines by 16.

I have an unpleasant flicker in the lower right corner as you can see in the picture that I need to change in the code so that the flicker disappears my cycle worked.

int room[12][16] = {
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
};
void loop() 
{
  // Score
  TV.select_font(font6x8);
  TV.set_cursor(0,0);
  TV.print("000000");
  
  TV.set_cursor(86,0);
  TV.print("x03");
  
  // Hearth
  TV.bitmap(108,0, full_hearth);
  TV.bitmap(115,0, full_hearth);
  TV.bitmap(122,0, full_hearth);

  // Drawing 16*8 pixels horizontally
  for(int x=0; x<16; x++) 
  {
    // Drawing 12*8 pixels vertically
    for(int y=11; y>=1; y--) 
    {
      if(room[y][x]==1) {
        TV.bitmap(x*8, y*8, top_side);
      }
      if(room[y][x]==2) {
        TV.bitmap(x*8, y*8, bottom_side);
      }
    }
  }

  TV.bitmap(marioX,marioY, right_normal_mario);
  
  // Eliminate screen flicker
  TV.delay_frame(1);
  TV.clear_screen();
}