LibGDX demo platformer for 2d side scrolling game like Mario!


Since I had seen this post: on Badlogic, about adding a new maps API to libGDX, as well as a side scrolling demo into the official source, I have really wanted to see it in action, and play around with it a bit.

So, I finally got around to downloading the source for LibGDX from Github again, finding the demo and compiling it using the basic project shown here:

I have uploaded the HTML5 demo so you can play it, and have included the source code for the demo:

Demo Information

Click here to see the demo on it’s own page, so you can jump with spacebar
The demo is very awesome, for how little code is needed to create it. You can move, jump, break blocks, and the level is about the size of Mario 1. The level is created using a pre-built map editor, so you can easily change out images, change the level- everything without even needing to edit the source.

I would recommend you download the demo, compile it, and try playing with it for yourself!

Here is the source code (
(which you can also find at LibGDX’s github site:

package com.chrismweb.mygdxgame;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapTile;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;

import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Pool;

 * Super Mario Brothers like very basic platformer, using a tile map build via
 * Vicky Wenderlich

* * Shows simple platformer collision detection as well as on-the-fly map * modifications through destructable blocks! * * @author mzechner * */ public class Game implements ApplicationListener { /** * The player character, has state and state time, */ static class Koala { static float WIDTH; static float HEIGHT; static float MAX_VELOCITY = 10f; static float JUMP_VELOCITY = 40f; static float DAMPING = 0.87f; enum State { Standing, Walking, Jumping } final Vector2 position = new Vector2(); final Vector2 velocity = new Vector2(); State state = State.Walking; float stateTime = 0; boolean facesRight = true; boolean grounded = false; } private TiledMap map; private OrthogonalTiledMapRenderer renderer; private OrthographicCamera camera; private Texture koalaTexture; private Animation stand; private Animation walk; private Animation jump; private Koala koala; private Pool rectPool = new Pool() { @Override protected Rectangle newObject() { return new Rectangle(); } }; private Array tiles = new Array(); private static final float GRAVITY = -2.5f; @Override public void create() { // load the koala frames, split them, and assign them to Animations koalaTexture = new Texture("data/koalio.png"); TextureRegion[] regions = TextureRegion.split(koalaTexture, 18, 26)[0]; stand = new Animation(0, regions[0]); jump = new Animation(0, regions[1]); walk = new Animation(0.15f, regions[2], regions[3], regions[4]); walk.setPlayMode(Animation.LOOP_PINGPONG); // figure out the width and height of the koala for collision // detection and rendering by converting a koala frames pixel // size into world units (1 unit == 16 pixels) Koala.WIDTH = 1 / 16f * regions[0].getRegionWidth(); Koala.HEIGHT = 1 / 16f * regions[0].getRegionHeight(); // load the map, set the unit scale to 1/16 (1 unit == 16 pixels) map = new TmxMapLoader().load("data/level1.tmx"); renderer = new OrthogonalTiledMapRenderer(map, 1 / 16f); // create an orthographic camera, shows us 30x20 units of the world camera = new OrthographicCamera(); camera.setToOrtho(false, 30, 20); camera.update(); // create the Koala we want to move around the world koala = new Koala(); koala.position.set(20, 20); } @Override public void render() { // clear the screen, 0.7f, 1.0f, 1);; // get the delta time float deltaTime =; // update the koala (process input, collision detection, position update) updateKoala(deltaTime); // let the camera follow the koala, x-axis only camera.position.x = koala.position.x; camera.update(); // set the tile map rendere view based on what the // camera sees and render the map renderer.setView(camera); renderer.render(); // render the koala renderKoala(deltaTime); } private Vector2 tmp = new Vector2(); private void updateKoala(float deltaTime) { if (deltaTime == 0) return; koala.stateTime += deltaTime; // check input and apply to velocity & state if ((Gdx.input.isKeyPressed(Keys.SPACE) || isTouched(0.75f, 1)) && koala.grounded) { koala.velocity.y += Koala.JUMP_VELOCITY; koala.state = Koala.State.Jumping; koala.grounded = false; } if (Gdx.input.isKeyPressed(Keys.LEFT) || Gdx.input.isKeyPressed(Keys.A) || isTouched(0, 0.25f)) { koala.velocity.x = -Koala.MAX_VELOCITY; if (koala.grounded) koala.state = Koala.State.Walking; koala.facesRight = false; } if (Gdx.input.isKeyPressed(Keys.RIGHT) || Gdx.input.isKeyPressed(Keys.D) || isTouched(0.25f, 0.5f)) { koala.velocity.x = Koala.MAX_VELOCITY; if (koala.grounded) koala.state = Koala.State.Walking; koala.facesRight = true; } // apply gravity if we are falling koala.velocity.add(0, GRAVITY); // clamp the velocity to the maximum, x-axis only if (Math.abs(koala.velocity.x) > Koala.MAX_VELOCITY) { koala.velocity.x = Math.signum(koala.velocity.x) * Koala.MAX_VELOCITY; } // clamp the velocity to 0 if it's < 1, and set the state to standign if (Math.abs(koala.velocity.x) < 1) { koala.velocity.x = 0; if (koala.grounded) koala.state = Koala.State.Standing; } // multiply by delta time so we know how far we go // in this frame koala.velocity.mul(deltaTime); // perform collision detection & response, on each axis, separately // if the koala is moving right, check the tiles to the right of it's // right bounding box edge, otherwise check the ones to the left Rectangle koalaRect = rectPool.obtain(); koalaRect.set(koala.position.x, koala.position.y, Koala.WIDTH, Koala.HEIGHT); int startX, startY, endX, endY; if (koala.velocity.x > 0) { startX = endX = (int) (koala.position.x + Koala.WIDTH + koala.velocity.x); } else { startX = endX = (int) (koala.position.x + koala.velocity.x); } startY = (int) (koala.position.y); endY = (int) (koala.position.y + Koala.HEIGHT); getTiles(startX, startY, endX, endY, tiles); koalaRect.x += koala.velocity.x; for (Rectangle tile : tiles) { if (koalaRect.overlaps(tile)) { koala.velocity.x = 0; break; } } koalaRect.x = koala.position.x; // if the koala is moving upwards, check the tiles to the top of it's // top bounding box edge, otherwise check the ones to the bottom if (koala.velocity.y > 0) { startY = endY = (int) (koala.position.y + Koala.HEIGHT + koala.velocity.y); } else { startY = endY = (int) (koala.position.y + koala.velocity.y); } startX = (int) (koala.position.x); endX = (int) (koala.position.x + Koala.WIDTH); getTiles(startX, startY, endX, endY, tiles); koalaRect.y += koala.velocity.y; for (Rectangle tile : tiles) { if (koalaRect.overlaps(tile)) { // we actually reset the koala y-position here // so it is just below/above the tile we collided with // this removes bouncing :) if (koala.velocity.y > 0) { koala.position.y = tile.y - Koala.HEIGHT; // we hit a block jumping upwards, let's destroy it! TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get(1); layer.setCell((int) tile.x, (int) tile.y, null); } else { koala.position.y = tile.y + tile.height; // if we hit the ground, mark us as grounded so we can jump koala.grounded = true; } koala.velocity.y = 0; break; } }; // unscale the velocity by the inverse delta time and set // the latest position koala.position.add(koala.velocity); koala.velocity.mul(1 / deltaTime); // Apply damping to the velocity on the x-axis so we don't // walk infinitely once a key was pressed koala.velocity.x *= Koala.DAMPING; } private boolean isTouched(float startX, float endX) { // check if any finge is touch the area between startX and endX // startX/endX are given between 0 (left edge of the screen) and 1 (right edge of the screen) for (int i = 0; i < 2; i++) { float x = Gdx.input.getX() / (float); if (Gdx.input.isTouched(i) && (x >= startX && x <= endX)) { return true; } } return false; } private void getTiles(int startX, int startY, int endX, int endY, Array tiles) { TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get(1); rectPool.freeAll(tiles); tiles.clear(); for (int y = startY; y <= endY; y++) { for (int x = startX; x <= endX; x++) { Cell cell = layer.getCell(x, y); if (cell != null) { Rectangle rect = rectPool.obtain(); rect.set(x, y, 1, 1); tiles.add(rect); } } } } private void renderKoala(float deltaTime) { // based on the koala state, get the animation frame TextureRegion frame = null; switch (koala.state) { case Standing: frame = stand.getKeyFrame(koala.stateTime); break; case Walking: frame = walk.getKeyFrame(koala.stateTime); break; case Jumping: frame = jump.getKeyFrame(koala.stateTime); break; } // draw the koala, depending on the current velocity // on the x-axis, draw the koala facing either right // or left SpriteBatch batch = renderer.getSpriteBatch(); batch.begin(); if (koala.facesRight) { batch.draw(frame, koala.position.x, koala.position.y, Koala.WIDTH, Koala.HEIGHT); } else { batch.draw(frame, koala.position.x + Koala.WIDTH, koala.position.y, -Koala.WIDTH, Koala.HEIGHT); } batch.end(); } @Override public void dispose() { } @Override public void resize(int width, int height) { // TODO Auto-generated method stub } @Override public void pause() { // TODO Auto-generated method stub } @Override public void resume() { // TODO Auto-generated method stub } }


This is a great place to start for creating 2d side scroller games, with pre-built levels very quickly. If you've had dreams of re-creating levels from any Mario Brothers games, Duke Nukem, or Commander Keen, or your own unique character, this will help you get up and running very quickly.

Download the source code and assets here(without the project files).

Bookmark the permalink.


  1. Hi,

    I developed some apps already, but I’m new to the gaming scene. I really both your tutorials and wanted to give it a go. Downloaded all needed and started the desktop version: running! So far so good. Then I wanted to try to run the game on Android, which of course interests me the most. But unfortunatly I keep getting: “Couldn’t load file: data/koalio.png”. This comes from the line 88: “koalaTexture = new Texture(“data/koalio.png”);” I inserted the data-folder in all projects foldes by now. Like said the desktop version is running to. Do you have an idea whats going wrong? Thanx in advice!

    • Solved!

      The Android-Folder has an Asset Folder. The Data folder must be placed there:


    • Hey, sorry for the late reply, not sure why wordpress isn’t emailing me when comments are made!

      Yeah, for that it’s probably because it can’t find that .png/ image file in the android version of the program.

      I think they have it setup now that the desktop version points towards the assets folder of the Android version, but I end up just manually copying assets from the desktop version to android whenever I need to.

      So on your desktop version, you might have a folder called “data/koalio.png”, whereas in the android version, the file has to be under the folder “assets/data/koalio.png”.

      Hope that helps and fixes it for you!

  2. how did you get the tile map to work with html5 because every time I try it says gwt backends error, please can you tell me i would be very useful. please reply.

Leave a Reply

Your email address will not be published. Required fields are marked *