Decorator Design Pattern – Simple Java Example

Sandwich.java

Loaf.java

SandwichDecorator.java

Bacon.java

Cheese.java

Starter.java

Observer Design Pattern – Simple Java Example

Observer.java

Subject.java

TemperatureListener.java

Weather.java

Starter.java

State Design Pattern – Simple Java Example

WeaponState.java

WeaponHasAmmoState.java

WeaponHasNoAmmoState.java

Weapon.java

Starter.java

Strategy Design Pattern – Simple Java Example

Job.java


Human.Java

SuperHero.java

Engineer.java

Bob.java

John.java

Starter.java

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 GameManager.java class:

Searching for the whole project? Just download the source from the previous tutorial, repleace the GameManager.java 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 opengameart.org.

The Game.java 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 Game.java class has become a little messy.

To make the code cleaner I added a GameManager class.

GameManager.java

The Game class is now a lot cleaner
Game.java

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:
- http://libgdx.badlogicgames.com/documentation.html#gettingstarted
- http://www.gamefromscratch.com/post/2013/09/19/LibGDX-Tutorial-1-Creating-an-initial-project.aspx
- http://lmgtfy.com/?q=setting+up+libgdx+

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: http://freesound.org/people/Gniffelbaf/sounds/82116/

The source code:

Main.java

Player.java

PlayerInputHandler.java

Game.java

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

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

edit…
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: