Learn LIBGDX with Real Code Examples
Updated Nov 24, 2025
Code Sample Descriptions
1
LibGDX Simple Counter Game
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class CounterGame extends ApplicationAdapter {
private SpriteBatch batch;
private BitmapFont font;
private int count;
@Override
public void create() {
batch = new SpriteBatch();
font = new BitmapFont();
count = 0;
}
@Override
public void render() {
if (Gdx.input.isKeyJustPressed(Input.Keys.UP)) count++;
if (Gdx.input.isKeyJustPressed(Input.Keys.DOWN)) count--;
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
font.draw(batch, "Count: " + count, 100, 150);
batch.end();
}
@Override
public void dispose() {
batch.dispose();
font.dispose();
}
}
A basic LibGDX example that displays a counter and increments/decrements it with input keys.
2
LibGDX Sprite Movement
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class MoveSprite extends ApplicationAdapter {
private SpriteBatch batch;
private Texture img;
private float x = 100;
private float y = 100;
@Override
public void create() {
batch = new SpriteBatch();
img = new Texture("player.png");
}
@Override
public void render() {
if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) x -= 2;
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) x += 2;
if (Gdx.input.isKeyPressed(Input.Keys.UP)) y += 2;
if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) y -= 2;
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(img, x, y);
batch.end();
}
@Override
public void dispose() {
batch.dispose();
img.dispose();
}
}
Moves a texture sprite using arrow keys.
3
LibGDX Clear Screen Color
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
public class ColorFade extends ApplicationAdapter {
private float r = 0;
@Override
public void render() {
r += Gdx.graphics.getDeltaTime() * 0.1f;
if (r > 1) r = 0;
Gdx.gl.glClearColor(r, 0.2f, 0.5f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
}
Changes background color gradually using delta time.
4
LibGDX Animation Example
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
public class AnimationDemo extends ApplicationAdapter {
private SpriteBatch batch;
private Animation<TextureRegion> anim;
private float state;
@Override
public void create() {
batch = new SpriteBatch();
Texture sheet = new Texture("walk.png");
TextureRegion[][] frames = TextureRegion.split(sheet, 32, 32);
anim = new Animation<>(0.1f, frames[0]);
state = 0;
}
@Override
public void render() {
state += Gdx.graphics.getDeltaTime();
TextureRegion frame = anim.getKeyFrame(state, true);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(frame, 100, 100);
batch.end();
}
}
Plays a frame-based animation.
5
LibGDX Keyboard Text Input
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class TypeDemo extends ApplicationAdapter {
private SpriteBatch batch;
private BitmapFont font;
private String text = "";
@Override
public void create() {
batch = new SpriteBatch();
font = new BitmapFont();
}
@Override
public void render() {
for (int i = 29; i < 127; i++) {
if (Gdx.input.isKeyJustPressed(i)) text += (char)i;
}
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
font.draw(batch, text, 50, 150);
batch.end();
}
}
Shows simple typed text on screen.
6
LibGDX Timer Event
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.utils.Timer;
public class TimerExample extends ApplicationAdapter {
private int counter = 0;
@Override
public void create() {
Timer.schedule(new Timer.Task() {
@Override
public void run() {
counter++;
}
}, 1, 1);
}
@Override
public void render() {
Gdx.app.log("Timer", "Counter: " + counter);
}
}
Runs an action every second using a timer.
7
LibGDX Mouse Tracker
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class MouseTracker extends ApplicationAdapter {
private SpriteBatch batch;
private BitmapFont font;
@Override
public void create() {
batch = new SpriteBatch();
font = new BitmapFont();
}
@Override
public void render() {
float x = Gdx.input.getX();
float y = Gdx.input.getY();
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
font.draw(batch, "X:" + x + " Y:" + y, 50, 150);
batch.end();
}
}
Displays mouse coordinates.
8
LibGDX Sound Playback
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.audio.Sound;
public class PlaySound extends ApplicationAdapter {
private Sound beep;
@Override
public void create() {
beep = Gdx.audio.newSound(Gdx.files.internal("beep.wav"));
}
@Override
public void render() {
if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE)) beep.play();
}
@Override
public void dispose() {
beep.dispose();
}
}
Plays a sound when SPACE is pressed.
9
LibGDX Touch Example
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
public class TouchDemo extends ApplicationAdapter {
@Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if (Gdx.input.justTouched()) {
int x = Gdx.input.getX();
int y = Gdx.input.getY();
Gdx.app.log("Touch", x + "," + y);
}
}
}
Registers touch/click input and prints coordinates.
10
LibGDX Random Walker
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import java.util.Random;
public class RandomWalker extends ApplicationAdapter {
private float x = 200;
private float y = 200;
private Random random;
@Override
public void create() {
random = new Random();
}
@Override
public void render() {
x += random.nextInt(3) - 1;
y += random.nextInt(3) - 1;
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
}
Moves a point randomly each frame.
11
LibGDX FPS Display
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class FpsDemo extends ApplicationAdapter {
private SpriteBatch batch;
private BitmapFont font;
@Override
public void create() {
batch = new SpriteBatch();
font = new BitmapFont();
}
@Override
public void render() {
int fps = Gdx.graphics.getFramesPerSecond();
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
font.draw(batch, "FPS: " + fps, 30, 150);
batch.end();
}
}
Shows frames per second on screen.