Decorator Design Pattern – Simple Java Example

Observer Design Pattern – Simple Java Example

State Design Pattern – Simple Java Example

Strategy Design Pattern – Simple Java Example


Builder Pattern – Simple Java Example

Beginner libGDX Game Development Tutorial – player A chasing player B – adding animation

This is another part of the Beginner libGDX Game Development Tutorial. Check the previous ones to get the source code, then continue with this post.

Lets add a simple animation after one gets the other.

If you only care about animation, here is an easy example on that.

The assets for the animation:
Assets DIR
Place them in a “animation” directory in the project assets.

The freames for the animation are loaded this way:

We set the play mode:

The draw logic:

Now the whole class:

Searching for the whole project? Just download the source from the previous tutorial, repleace the class, add the assets and you are good to go.

libGDX – Simple Animation Example

The simplest way to make an animation in libGDX:

We are goin to use this asset from

The class:

You can download the example project from here.

Beginner libGDX Game Development Tutorial – player A chasing player B – code refactoring

In the previous tutorial the class has become a little messy.

To make the code cleaner I added a GameManager class.

The Game class is now a lot cleaner

The new Player class:

The InputHandler is almost the same.
Download the whole project, to have a better look at it.

Check the next tutorial for some basic animation in this example game.

Beginner libGDX Game Development Tutorial – player A chasing player B

When you are starting with game development it’s recommended to start with somethig simple. A reddit user (vethan) said it well:

“Think of a really simple game idea. Got one? It’s too complicated, make it simpler.”

I’m not gonna go step by step how to setup a libGDX project, because many smart people did it already:

In this tutorial we are creating a desktop version of somethig similar to the Tag Game.

One player controls a needle with the keyboard arrows, the other player tries to escape using the WASD keys. When the needle hits the balloon the game resets.
You can download the project and import it to eclipse (the download link in in the end of this post).

The awesome player graphics done by my girlfriend:

player1.png and player2.png

The sound file:

The source code:

Download the project as a .zip.
Download the runnable game jar.

The jar will not start? Try to type in the console:

Go to the next part of this tutorial (refactoring of current code).

Java Defensive Copy with a Copy Constructor

“You must program defensively, with the assumption that clients of your class will do their best to destroy its invariants.”
– Effective Java, item 39

If you create classes that contain muttable objects it’s a good practice to make this classes use Defensive Copy. It’s a way to make sure that your code works how you expect.

Let’s look at this problem:

The output:

The state of the Building object changed but we don’t even touched it! The problem is a reference leak that allows the client to change the state of the Building object by changing only the FrontDoor object.

To avoid that we can use the Defensive Copy (creating new instance of the FrontDoor object in the constructor setter and getter):

Now the output of the starter will be correct:

But there is a similar but more elegant solution: using a Copy Constructor (in this case in the FrontDoor class).

The final verions of the Building class: