in

Platform starter kit: Adding vertical scrolling

This post is one of a series that shows how to take the basic Platform Starter Kit from the XNA Games Studio and turn it into a more full featured game by taking advantage of the many tutorials and game examples published on the XNA Creators Club.

Having already added power-ups, lives, highscore and game state management the one piece left is to make each level more challenging by adding the possibility of height with vertical scrolling. The code to do this is published in the forums by Microsoft but it isn’t readily obvious.

Open the Level.cs file and add the following, after the existing cameraPosition variable declaration:

public float cameraPositionYAxis;

Modify the Draw method to use a Y-axis offset in addition to the previously calculated X-axis offset. Modify the following line:


//Matrix cameraTransform = Matrix.CreateTranslation(-cameraPosition, 0.0f, 0.0f); 

to match the following:

Matrix cameraTransform = Matrix.CreateTranslation(-cameraPositionXAxis, -cameraPositionYAxis, 0.0f); 

Modify the ScrollCamera method by adding the following, after the existing code block that calculates the scrolling borders of the X-axis:


// Calculate the scrolling borders for the Y-axis. 

const float TopMargin = 0.3f; 

const float BottomMargin = 0.1f; 

float marginTop = cameraPositionYAxis + viewport.Height * TopMargin; 

float marginBottom = cameraPositionYAxis + viewport.Height - viewport.Height * BottomMargin; 


in the same method, add the following, after the existing code block that calculates how far to scroll along the X-axis: 

// Calculate how far to vertically scroll when the player is near the top or bottom of the screen. 

float cameraMovementY = 0.0f; 

if (Player.Position.Y < marginTop) //above the top margin 

cameraMovementY = Player.Position.Y - marginTop; 

else if (Player.Position.Y > marginBottom) //below the bottom margin 

cameraMovementY = Player.Position.Y - marginBottom; 

In the same method, add a new variable (called maxCameraPositionYOffset) that tracks the highest scrolling point for the camera:


float maxCameraPositionYOffset = Tile.Height * Height - viewport.Height; 

Finally, add the following, after the calculation for the camera position along the X-axis:


cameraPositionYAxis = MathHelper.Clamp(cameraPositionYAxis + cameraMovementY, 0.0f, maxCameraPositionYOffset); 

Now all you have to do is add some level designs (the 0.txt, 1.txt etc etc) that are larger both in height and width for the viewport and you’ll have a horizontal and vertical scrolling. This adds an extra dimension to the level design possibilities.

I’ll be publishing my code (not my graphic assets) so if you’ll not up for coding you can still do level design and graphics to make your own version of this game.

Look out for other posts on the Platform Starter Kit.

Shout out

Subscriptions

Tags

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.