Scrolling X&Y

Does anyone know of an article or simple example which demonstrates scrolling on both X&Y axis?

For example say you have a map that’s 256x256. Your character is drawn at 0,0. Screen needs to scroll when character is 32 pixels from the edge of either top, bottom, left, or right side of the screen.

My (incomplete) LodeRunner has this. Where possible the player is centered in the screen - until you move to an edge in which case the player moves to the side.

To do this, I have a player structure and a level structure.

struct Player {

  uint8_t x;
  uint8_t y;
  int8_t xDelta;
  int8_t yDelta;

};

and

struct Level {

  uint8_t xOffset;
  uint8_t yOffset;
  int8_t xOffsetDelta;
  int8_t yOffsetDelta;

};

Actually in my game this is a class not a struct but functionally equivalent. To move a player left:

void movePlayerLeft() {

  // If the player is in the right hand side of the screen, then move them back towards the centre ..

  if (player.x > 60) {  
    
    player.xDelta = -2;
    level.setXOffsetDelta(0);

  }
  else {

    // If the screen can scroll to the left, then scroll the screen ..

    if (level.getXOffset() < 0) {

      player.xDelta = 0;
      level.setXOffsetDelta(2);

    }
    else {

      // Otherwise move the player to the left ..

      if (player.x > 0) {

        player.xDelta = -2;
        level.setXOffsetDelta(0);

      }

    }

  }

}

Then in my loop, I update the player positions …

void loop() {

    ...

    // Move player ..

    player.x = player.x + player.xDelta;
    player.y = player.y + player.yDelta;
    level.setXOffset(level.getXOffset() + level.getXOffsetDelta());
    level.setYOffset(level.getYOffset() + level.getYOffsetDelta());

}

You can see it working here >

3 Likes

Doesn’t mystic balloon also have a scrolling feature? that would work for something like a map