Second Android Game “Chem-Quest”

Finally I have got a finished my second game. This one is created without AndEngine.

Most of UI is created in xml layout definition. Graphics is handled by classes that extends android.widget.Button.

In those I override onDraw method to draw graphics elements for all click-able sprites.

About the game

Game is another brain training game. You must collect specific amount of water in two tubes using simple steps : fill tube, pout into another tube and pour out.

It is avaible on GetJar:
http://www.getjar.com/mobile/165999/chem-quest

Happy gaming!

Advertisements

New game and app is comming …

Since last days I was working hard on my next game and first application.

Game
It is called “Chem-Quest”. I started working on this game 2 month ago, it was my first look on Android. There is no framework in this game. I combine XML layout and drawing on View’s Canvas.
I want to say that it wasn’t simple to handle graphics as using framework, but I want to try this way. Most problems is how to scale on different displays. Still game is not best on smallest devices (less than 180×160).

In this game graphics is more complex, almost beautiful I’d say 🙂 It was not created by me, but my friend.

Still I need to spend some time to test all paths. I hope it will be published tomorrow or day after tomorrow.

Application
First application, but it is almost like game. Actually it is based on other game called “Remember Me: Balls” – this game needs days to finish.

Application is called “Lotto-Blender”. It is a lottery randomiser or lottery number mixer. It is based on AndEngine and it was created in 3 days till now – yes only 3 days.

Graphics is simple but I think quite nice, all created in Gimp using some filters.

This is important application for me because I used AndEngine physics plugin for the first time. This plugin is a fast and free Android physics engine. It is based on JNI calls to Box2D engine.

The great about this engine is simple way to attach this engine to your AndEngine code.

Application is almost ready, but still I get some errors. I hope it will be published till Monday.

Future plans
In free time I was looking for good free engine for Android 3D games and I could not found any.
There is two or tree that simplified some work, but no full game engine or engine that is in final release.
Most advanced is JMonkeyEngine, but in alpha stage.
Most stable is libgdx and jpc – but without 3D physics and all configuration is on developer side.

So I decided to give up 3D games for now.

My topic for next week is more advanced physics in 2D game, with joins, maybe skeleton physics, and some more advanced graphics. And again I want to use AndEngine.

Did You heard that AndEngine creator is working now for some company called “Zynga”? It is looks that AndEngine become Zynga property, but it will stay an open source.

Happy codding!

New Graphics for Remember Me

Lately I was working on graphics for Remember Me. Even if there is only couple of downloads of this Android game I was requested to change graphics to something more light.

This is what I was created using GIMP and Pinta – yes both Linux. I use Linux for Android development now, it works faster than Vista.

As you can see backgroud is changed. Also I added animated colourful clouds as a background of level buttons. There is only 3 levels now EASY, MEDIUM and HARD I decided that other levels did not make any difference.
Finally final compare screen is changed, it is more readable now.

Happy gaming!

Simple Radar for Space Android game

In space-shooter-like game it is nice to have Radar on the Hud.

Tere is simple way to draw radar as a Rectangle shape with dots on it. To do it I will use special Scene to manage dots movement and Rectangle as a Radar to show position of the objects.

Becouse this is only how to to create radar I will use simple graphics – rectangle with colored dots.

1)
First thing we need to define our World size fields. I will handle differend size of world and camera view. World should be bigger than camera size lets set it to 2 times bigger.

In BaseGameActivity class “MrRadar”

    public static final int CAMERA_HEIGHT = 320;
    public static final int CAMERA_WIDTH = 480;

    public static final int SPACE_HEIGHT = CAMERA_HEIGHT * 2;
    public static final int SPACE_WIDTH = CAMERA_WIDTH * 2;

2)
To manage Entities on Radar I will define interface “IRadarObject”. I will extends IEntity becouse of simple get size and position of the object.

	public interface IRadarObject extends IEntity {
		public ObjectConfig getConfig();
	}

Also I define class to hold all graphic information about dot on the radar, like color, etc.

	public class ObjectConfig {
		public int red;
		public int green;
		public int blue;
		public ObjectConfig(int red, int green, int blue) {
			this.red = red;
			this.green = green;
			this.blue = blue;
		}
	}

3)
Now lets define Radar class.

It will be Rectangle – that is simplest way.
To handle all object I should create a Map – it will be managed from our Scene (defined in p.4).

Map radarObjectsMap = new HashMap();

Dots will be draw on rectangle but first I must find a way to transform space points to radar points.
To calculate x position of the object I get it is X position in the space and devide it by space width – this is my percentage of the space where object is paced, so this is also percentage of position on radar.
It is very simple and can be done like this:

	
private Pos convertPosition(IEntity ent) {
		Pos pos = new Pos();
		pos.x = (ent.getX() / MrChain.SPACE_WIDTH) * getWidth();
		pos.y = (ent.getY() / MrChain.SPACE_HEIGHT) * getHeight();
		pos.width = this.getWidth() * SCALER;
		pos.height = this.getHeight() * SCALER;
			
		return pos;
	}

Pos is special class that contains only fields: x,y, width and height.

I must add methods to handle adding, removing and moving objects on the radar.
I will just use Map. In adding I will define size of the object, first position and color. to define position I will use my method from above. Also I will check if Map already contains this object:

	
public void addObject(IRadarObject obj) {
		if (!radarObjectsMap.containsKey(obj)) {
			Pos pos = convertPosition(obj);
			Rectangle rec = new Rectangle(pos.x, pos.y, pos.width, pos.height);
			rec.setColor(obj.getConfig().red, obj.getConfig().green, obj.getConfig().blue);
			attachChild(rec);
			radarObjectsMap.put(obj, rec);
		}
	}

In similar way I remove objects and update its size, color and position.

Radar class looks like this:

public class Radar extends Rectangle {
	private static final float SCALER = 0.05f;
	
	Map radarObjectsMap = new HashMap();

	public Radar(float pX, float pY, float pWidth, float pHeight) {
		super(pX, pY, pWidth, pHeight);
		setAlpha(0.3f);
	}

	public void addObject(IRadarObject obj) {
		if (!radarObjectsMap.containsKey(obj)) {
			Pos pos = convertPosition(obj);
			Rectangle rec = new Rectangle(pos.x, pos.y, pos.width, pos.height);
			rec.setColor(obj.getConfig().red, obj.getConfig().green, obj.getConfig().blue);
			attachChild(rec);
			radarObjectsMap.put(obj, rec);
		}
	}
	
	public void removeObject(IRadarObject obj) {
		if (radarObjectsMap.containsKey(obj)) {
			Rectangle rec = radarObjectsMap.remove(obj);
			detachChild(rec);
		}
	}
	
	public void removeAllObjects() {
		radarObjectsMap.clear();
		this.detachChildren();
	}
	
	public void updateObject(IRadarObject obj) {
		Rectangle rec = radarObjectsMap.get(obj);
		if (rec != null) {
			Pos pos = convertPosition(obj);
			rec.setPosition(pos.x, pos.y);
			rec.setHeight(pos.height);
			rec.setWidth(pos.width);
		}
	}
	
	private Pos convertPosition(IEntity ent) {
		Pos pos = new Pos();
		pos.x = (ent.getX() / MrChain.SPACE_WIDTH) * getWidth();
		pos.y = (ent.getY() / MrChain.SPACE_HEIGHT) * getHeight();
		pos.width = this.getWidth() * SCALER;
		pos.height = this.getHeight() * SCALER;
			
		return pos;
	}
	private class Pos {
		float x;
		float y;
		float width;
		float height;
	}
}

4)
To define scene I will extends Scene from AndEngie API and in each attachChild and removeChild I will call Radar methods to add and remove objects. Finally I define UpdateHandler to manage object movement.

public class SceneWithRadar extends Scene {
	Radar radar;
	IUpdateHandler radarHandler = new IUpdateHandler() {
		@Override
		public void onUpdate(float pSecondsElapsed) {
			for (IEntity child : SceneWithRadar.this.mChildren) {
				if (child instanceof IRadarObject) {
					radar.updateObject((IRadarObject) child);
				}
			}
		}
		@Override
		public void reset() {
		}
	};
	public SceneWithRadar(float radarPosX, float radarPosY, float radarWidth, float radarHeight) {
		radar = new Radar(radarPosX, radarPosY, radarWidth, radarHeight);
		registerUpdateHandler(radarHandler);
	}
	@Override
	public void attachChild(IEntity pEntity) throws IllegalStateException {
		if (pEntity instanceof IRadarObject) {
			radar.addObject((IRadarObject) pEntity);
		}
		super.attachChild(pEntity);
	}
	
	@Override
	public boolean detachChild(IEntity pEntity) {
		if (pEntity instanceof IRadarObject) {
			radar.removeObject((IRadarObject) pEntity);
		}
		return super.detachChild(pEntity);
	}
	
	@Override
	public void detachChildren() {
		radar.removeAllObjects();
		super.detachChildren();
	}
}

How to handle states of sprites

Most of sprites in games has states that will change after fires some event.

Simple example:
We have one sprite “Dot”. This sprite has 3 states. Sprite can be clicked, when it will change state. Every state has different texture.

SHOW_EMPTY – empty Dot, sprite will not change it state
SHOW_SELECTED – Dot will be filled, sprite will not change it state
DRAW_EMPTY – Empty dot to change, sprite will change state to DRAW_SELECTED
DRAW_SELECTED- Filled Dot to change, sprite will change state to DRAW_EMPTY

BAD SOLUTION
We will use “if … else” statement and some methods and fields to handle states.

It will look like this.

boolean selected;
boolean clickable;

funtion void setSelected(boolean selected) {
  this.seleced = selected;
}
function boolean getSelected() {
   return selected;
}
function void setCkickable(boolean clickable) {
   this.clickable = clickable;
}
function boolean getClickable() {
   return clickable;
}

and for checking state and draw texture from touch listener:

function void manageTexture() {
if (clickable && selected) {
   // do something with texture
} else if (!clickable && selected) {
   // do something else with texture
}

… and so on.

For simple sprites it might look OK, but after some time new state comes, eg. HINT that will be same like SHOW_SELECTED but with minor changes. Then we must add new “if … else” statement.

After adding 3 or more sates code is hard to maintenance.

Pros:
Simple and fast to start implement
For 1-3 states small lines of code

Cons:
For more than 3 states huge number of lines that is hard to maintenance.

BETTER SOLUTION
I will use enums and interfaces
My enums will be:

enum DotState {
SHOW_EMPTY
SHOW_SELECTED
DRAW_SELECTED
DRAW_HINT
}

I will define our textures, lets simplified this to use TiledTexture from Andengine (integers define index of texture in image file.

enum DotState {
SHOW_EMPTY(0),
SHOW_SELECTED(1),
DRAW_SELECTED(2),
DRAW_HINT(3)
}

Then I will define what will happen after click. Click will be managed in touch listener that call our function but this function will be inside enum as a object that implements interface.

My interface will be:

private interface IManageMyState {
manage();
}

I will create objects for all states inside Sprite class like this:

private static final IManageMyState manageDrawSelected = new IManageMyState() {
public void manage(Dot dot) {
dot.state = DotState.DRAW_EMPTY;
}
};

Because enum is static my ImanageMyState objects must be static also. Dot object to handle will be passed as a parameter “manage(Dot dot)”.

And at the end I will pass my manager objects to enum:

enum DotState {
SHOW_EMPTY(0, manageShowEmpty),
SHOW_SELECTED(1, manageshowSelected),
DRAW_ENPTY(2, manageDrawEmpty),
DRAW_SELECTED(3, manageDrawSelected)
} 

We can manage textures simple to use our state.

@Override
function void onTouch(…) {
this.state.manager.manage(this);
}

Note that I passed “this” to manage method, this my dot to manage.

And that’s it!

WHY IT IS BETTER SOLUTION?

We can simply add new functions to IManageMyState, all states is simple to maintenance because all thins is in one place.

If you know better solution to handle states please write your comment 🙂

Pros:
Simple to maintenece for more than 3 states

Cons:
Lot of empty methods for states that do nothing

First Android game “Remember Me : Characters”

Finally after 2 weeks of learning Android and AndEngine my game is posted to GetJar.com.

Memory like game with minimal graphics and simple game play.

I’m Java Developer specialized in Web Application and for me AndEngine was easy to learn. It is best for simple 2D games because most of work is done – manage sprites, scaling, base Activity. Of course most of you already know that it is free Engine.

About the game

“Remember Me: Characters” is “clone” of Memory game. Player must remember shape of letter and draw it on blank canvas.

Game in version 1.0 contains Greek, Cyrillic, Chinese, Armenian, Hebrew, Arabic, Syriac, Thaana, Thai characters so there is no chance to get bored.

For trainings you can choose 90% of character to be shown, after some training next levels are ready to play for most experienced players there is 0% of hints level.

So if you are interested check it out on GetJar

http://www.getjar.com/remember-me-characters/