2D Platform Games: Tiles & Scrolling
Platform Games Character jumps from platform to platform World terrain is made of tiles Add player, bad guys, power ups
Creating the game map Make the world (game map) for one level. Don’t use one huge image! Use grid – each slot points to a tile image or null
Issues with Drawing the World Tile world is bigger than screen Need to move the screen (instead of the player, often) Often want background image that moves slower than the rest of the world
Calculate New Position Keep the player in the middle of the screen… Percentage of left and right of screen that makes the scroll happen if (Player.Position.X < marginLeft) cameraMovement = Player.Position.X - marginLeft
Offset Don’t scroll off ends: float maxCameraPosition = Tile.Width * Width – viewport.Width; That is, (the width of one tile * # tiles in a row) – (width of screen) Screen
Drawing the Tiles Efficiency: only draw visible tiles: int left = (int)Math.Floor(cameraPosition/Tile.Width); int right = left + <viewport width>/Tile.Width; right = Math.Min(right, Width-1) // where Width is the width of the 2D table.
Drawing the Sprites Not many – just draw all (brute force) Many – only draw visible sprites Keep sprites in a list ordered by position Partition map into screen – sized portions, keep sprites from each portion
Drawing Backgrounds Choices: Keep background static Scroll background image at same rate as tiles Scroll background image slower to mimic distance Last method is known as Parallax Scrolling
Scrolling Often will have segments of backgrounds. As scrolling happens, rotate backgrounds. Usually no more than 2 backgrounds segments drawn at any given time.