Thread: Whats the best way of making a game like minecraft, terraria, etc (2D)

  1. #1
    Registered User
    Join Date
    Mar 2012

    Question Whats the best way of making a game like minecraft, terraria, etc (2D)

    Basically I want to make a game that has no storyline that is basically a do what ever you want game. What would be the best library's to use?
    and how would I go about making it tile based? Thanks in advanced for any help.

    In C++
    Last edited by Aaron Hance; 05-09-2012 at 10:34 AM.

  2. #2
    Registered User
    Join Date
    Dec 2011
    Are you planning for puzzle kind of game ?

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    The best way is to learn C++. I should mention Terraria was programmed in C# using XNA and Minecraft I believe was programmed in Java. Programming the engine for Terraria would not be all that hard sans the water and how it behaves. The hardest part would be acquiring the graphical assets, music and sound to create that type of game. It is relatively simple to program a brute force 2D tile engine but making it blazing fast so you can update the game world, animations and the like is not so simple. However with processors as they are today and the speed of graphics cards you might actually be able to get away with pure brute force rendering.

    A tile engine is a simple paint by number scheme. You need a grid on the screen that is exactly 1 row and column larger than the screen on all sides respectively. Paint the grid from top left to bottom right either going top to bottom left to right or left to right top to bottom. Use the texture that has the same ID as the tile currently being rendered. For scrolling simply translate the entire grid left/right and when the left/right scroll value modulus the tile width or height is zero then you decrement /increment the starting row / column of the render and snap the 'grid' or the starting point of the tile render back to where it was before the scrolling occurred.

    The rest of the calculations become a simple computer science exercise of how to cache in data from a larger array to a smaller array and/or how to link the arrays into zones for the entire world. The parallax scrolling that Terraria implements is dirt simple. Use two tile layers and scroll each layer by (speed / z) or (speed / layer). The back layers will scroll slower than the front layers and thus...parallax scrolling.

    The rest of Terraria is simple mouse in grid cell tests or mouse in sprite bounding box tests. A very simple mouse position to cell position is:

    POINT mousePos;
    // Clamp to grid
    mousePos.x -= mousePos.x % grid.cellSizeX;
    mousePos.y -= mousePos.y % grid.cellSizeY;
    // Convert to row / column
    int col = mousePos.x / grid.cellSizeX;
    int row = mousePos.y / grid.cellSizeY;
    // Adjust for starting scroll position
    // Adjust starting scroll cell
    // This will not compile if the operands on either side of the modulus operator are floats
    int startScrollWorldPosY = grid.scrollX - (grid.scrollX % grid.cellSizeX); 
    int startScrollWorldPosX = grid.scrollY - (grid.scrollY % grid.cellSizeX);
    int startScrollCol = startWorldScrollPosX / grid.cellSizeX;
    int startScrollRow = startWorldScrollPosY / grid.cellSizeY;
    // Translate local row, col mouse click to world row, col
    col += startScrollCol;
    row += startScrollRow;
    int cellvalue = map.GetCellValueAtRowCol(row,col)
    switch (cellValue)
        ...Handle different cell values here and react according to mouse input and state of game
    Note that this code does not account for the fact that grid.scrollX and grid.scrollY will be floats and you cannot do an integer modulus (%) on a float. You will need to use a floating point modulus in order to compile and to get a valid result.

    After you get the map working the rest is pure old school sprite animation and hit testing code and whatever else you want to implement.

    This will likely result in more questions but it should get you started. I recommend purchasing some books on C++ as well as 2D and 3D graphics.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Start by reading this, it will give you recommendations on what libraries and books to look into.

    2D wise, you are going to be looking at SFML, SDL or a similar 2D library, assuming you already have programming knowledge, start there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-30-2011, 07:18 PM
  2. Lottery game Issue in Code...whats wrong?
    By thebridge in forum C Programming
    Replies: 6
    Last Post: 10-07-2010, 08:16 PM
  3. Should I use a game engine in making a RPG game?
    By m3rk in forum Game Programming
    Replies: 6
    Last Post: 01-26-2009, 04:58 AM
  4. Help on game making
    By rtyn in forum Game Programming
    Replies: 4
    Last Post: 07-31-2008, 07:25 PM
  5. Whats a very simple but good game i can try to make?
    By bluehead in forum C++ Programming
    Replies: 2
    Last Post: 11-06-2001, 09:24 PM