Scrolling tile game help

I’m working on a scrolling tile game (think minicraft) and i have two questions:

  1. How do I make the screen move depending on where the player is?
  2. How do I know which tiles to draw depending on which tiles on currently on screen (my method of this(looping through every single tile) doesn’t work).

Any help would be appreciated, and any code examples would be greatly appreciated :smiley: .

If you store your map in a 2d array, and you know which tile the player is supposed to be on, then it’s easy to calculate which tiles to render.

Say the player is on (50, 50) and there can be a total of 40 tiles across the screen and 30 vertical. If you want to keep the player centered, then you just divide 40 by 2 and then you know you have to render x = 30…70 (40 tiles across) and same with the vertical position. 30 / 2 = 15, so y = 35…65.

The way I would do this is:

Create a Map class with a 2d array of Tiles.

if the player has free movement rather than tile movment, I would find the nearest tile.

I would then take that tile and center the camera on it. (basically translate the matrix to that position)

I would only render tiles that are within the camera position.x +/- Display.getWidth()/2 and camera position.y +/- Display.getHeight()/2

You might need to play with the render distance to make sure tiles that are half on the screen and half off are still drawn.

If you still need help tomorrow, I might be able to throw something simple together for you :slight_smile: