2
votes

Just started university doing a Computer Science with Games Development degree. Looking into making a 2D platformer using tile mapping. Obviously one feature of 2D platformers is a world bigger than the screen, i.e. Super Mario Brothers, Legend of Zelda, etc.

I'm wondering how to do this. I've got a tile map set up using an array of texture, but now I'm stuck on :

  1. How do you code the fact that when the player is in the tile of y = 0 or x = screenWidth then scroll up or along a bit?
  2. How do I draw the player in all of this? (Using basic squares until I have this figured out and then I have an artist lined up to draw some stuff)

Any help would be great. Most of the answers on questions like these suggest using an engine, but I do not want to do this, so please do not suggest it.

edit : Also, yes I have had a look at the platformer starter kit. I didn't think it was really much help? I need some explanation along with code, and it's not very well commented.

3

3 Answers

2
votes

I like to take an object oriented approach to this kind of problem.

You mention you a grid of tiles (your world) to be drawn. I assume you'll also be using this for collision as well, but that's not mentioned, so I'll ignore it.

You also mention you'll need a player (a Mario, if you will). This will need to have a position within your world.

I would have two classes. One for you player, having a position in world coordinates (NOT relative coordinates). I would then have a class that describes the tiles of the world (your tile map). You could then draw the tiles based on their world coordinates, transformed by a camera matrix of some kind (a translation matrix, used to offset the view of the world). The idea here is to move the camera, not move the world. You could draw the player with that same camera transformation matrix.

Since the player has no idea he's being watched by a camera, the camera needs to know what to follow. Therefore, the camera should need to "follow" the player. You could give the camera a "Target" coordinate, and the camera can have rules on how to follow the player.

The camera could then determine if it needs to "scroll" faster or slower to keep up with the player. The camera need to be able to determine if the player is being drawn near an edge of the screen, and move tward that edge. Etc.

I hope this has been helpful :)

1
votes

You should be tracking some scrollPositionX and scrollPositionY that offset which tiles you are drawing. Using a simple method of scrolling (1 whole tile at a time), you could add scrollPositionX to the column index and scrollPositionY to the row index of your array references when drawing the map. When the player moves near an edge, increase or decrease scrollPositionX and scrollPositionY. I recommend tracking the player's absolute position on the map rather than the player's position on the screen. So if scrollPositionX == playerX, that means the player is in the first column, and you should probably decrease scrollPositionX.

1
votes
  1. What I do is keep 4 variables defining the camera boundaries. If the player is out of those boundaries relative to the camera and is moving, then scroll in the appropriate direction. And what I mean by relative to the camera is NOT the player's actual coordinates in world space, but on screen space. If that needs further clarification, please say so.

  2. Spritebatches will be your friend! Combine these with texture2D instances and you'll be set.

      SpriteBatch batch = new SpriteBatch();
      Texture2D mySprite = Content.Load<Texture2D>("mySprite");
    
      batch.begin();
      batch.draw(mySprite, x, y, width, height); //not sure about these parameters, double check them!
    
      batch.end();
    

Encapsulate this stuff into a class for easier management!