Showing posts with label gaming. Show all posts
Showing posts with label gaming. Show all posts

Friday, May 15, 2009

Noughts & Crosses

When I started learning J2ME in 2005, the first game I wrote was a Noughts & Crosses game. Since then I've been working on a platform game, but as it's taking so long to complete I've decided to polish up the old Noughts & Crosses game, and release it:

Noughts & Crosses

Features:
  • Play against a human opponent or against the phone.
  • Three difficulty levels.
  • Ability to enter player names.
  • Ability to choose starting player.
  • Touch screen support.
System Requirements: Java (MIDP 2.0, CLDC 1.0)
Genre: Puzzle | Strategy

Watch Gameplay Movie

Play Demo Online

Download Demo: xno.jad xno.jar

You can view the demo game play movie on YouTube, or you can play the demo online. I would really appreciate it, though, if you could download the game to your phone and test it and let me know how it works. Any feed back is very welcome.

Saturday, November 15, 2008

A J2ME Mobile Game (Contd)

A small update to my previous post:



Some new effects and a new monster. Also, added music created using ModPlug Tracker.

Sunday, August 17, 2008

A J2ME Mobile Game

Here's a sneak peek at a J2ME mobile game I've been working on in my spare time:





It's the first proper game I've ever tried to write and I hope I'm able to complete it. I drew all the images in Gimp and laid out the level in Mappy. The screenshot and video is from the first level running in a little tester I wrote that lets me test out the animations, tile maps and levels, using my very own 2D tile engine.

Monday, October 01, 2007

Ten Steps To Becoming A J2ME Game Developer


In line with JavaLobby's Java Gaming celebration month, I present Ten Steps To Becoming A J2ME Game Developer:
  1. Buy and read J2ME Game Programming by Martin J. Wells. If you're short of time, read chapters 7 through 16. This book will help you wrap your head around the different concepts in game programming.
  2. Download and install the JDK, the WTK, your favourite IDE (Eclipse or Netbeans) and the J2ME plugins for your IDE (EclipseME or Netbeans Mobility Pack).
  3. Download and install Ant and Antenna (J2ME tasks for Ant). The only way to deal with device fragmentation is to create a flexible build script that allows you to use device configuration files and a preprocessor to create device specific jar files.
  4. Download and install Proguard. You will need to obfuscate your code to fit into the operator limited jar size.
  5. Download and install the latest SDKs and Emulators from Nokia, Motorola and Sony Ericsson. You will need to test on these emulators as well as on some actual devices.
  6. Download and install GIMP for graphics, Audacity for sound effects, and AnvilStudio or RoseGarden for MIDI music.
  7. Download and install Mappy to create your 2D tilemaps. You don't even have to write your own level editor these days.
  8. Create your 2D tile based game. Besides the book mentioned above, the forums at J2MEForums has all you need to know to write your first game.
  9. Submit your game demo to the GetJar's beta testing program. This will allow you to identify any remaining device compatibility issues.
  10. Publish your game with mobile game portals like ClickGamer, and advertisement wrapping portals like GameJump and Hovr.
  11. Profit ?!?!
For a hobbyist, J2ME is the best platform to begin writing games: free and excellent tools, easy to program, easy to deploy, and a friendly community too. Anyone and their dog can write a game in 3 months that's as good if not better than some of the games being churned out by the big game studios. So follow my Ten-Step program and start writing J2ME games today!

Thursday, September 13, 2007

Right Shift Considered Harmful (Again)

CLDC 1.0 has no support for floats. So J2ME programmers generally use fixed point maths to represent decimals. The basic idea is that you multiply the integer by the precision factor you need, and then divide by that precision factor to get back your integer. By choosing a precision factor that's a power of 2, we can use left bitshift to covert to fixed point and right bitshift to get back the integer. This is a common optimization since integers are stored in binary format and left bitshift is equivalent to multiplying by 2 and right bitshift is equivalent to dividing by 2. So, for example a typical fixed point math class would look like this:
public final static int PRECISION = 16;
public final static int FACTOR = 65536;

public final static int toFp(int a){ return (int)((long)a << PRECISION); }
public final static int toInt(int a_fp){ return (a_fp >> PRECISION); }
public final static int addFp(int a_fp, int b_fp) { return a_fp + b_fp; }
public final static int subFp(int a_fp, int b_fp) { return a_fp - b_fp; }
public final static int mulFp(int a_fp, int b_fp) { return (int)((((long)a_fp * (long)b_fp)) >> PRECISION); }
public final static int divFp(int a_fp, int b_fp) { return (int)(((long)a_fp << PRECISION) / b_fp); }

But then while writing my first J2ME game using fixed point math, I had the weirdest problem: motion in directions where sin/cos was negative was always greater than in directions where sin/cos was positive. Debugging, I found that the problem was with right shifting negative integers: right-shifting a negative integer is not equivalent to dividing by 2 because negative integers are stored in 2's complement form!

To understand the problem, lets try a simple example:
int a = 3/2;           // = 1
int b = -3/2; // = -1
int c = 3 >> 1; // = 1
int d = -3 >> 1; // = -2!!!
int e = -3 >>> 1; // = 2147483646!!!

System.out.println("a="+a+" b="+b+" c="+c+" d="+d+" e="+e);

I had to understand what was going on here! So here's what I found out:
Since integer is a 32 bit binary in Java:
3 is represented as: 00000000 00000000 00000000 00000011

3 >> 1
= 00000000 00000000 00000000 00000001 (1) (Right most bit 1 is shifted off)
= 2^0 = 1

-3 is represented as: 00000000 00000000 00000000 00000011
then 1's complement = 11111111 11111111 11111111 11111100
then add 1 (2's complement) = 11111111 11111111 11111111 11111101 (Left most bit is sign bit)

-3 >> 1
= 11111111 11111111 11111111 11111101 >> 1
= 11111111 11111111 11111111 11111110 (1) (Right most bit 1 is shifted off and sign bit is retained)
= -(2^0 + 1) = -2 (Converting from 2's complement)

-3 >>> 1
= 11111111 11111111 11111111 11111101 >>> 1
= 01111111 11111111 11111111 11111110 (1) (Right most bit 1 is shifted off but sign bit is also shifted)
= (2^(32-1) + 2^0 + 1) = 2147483646 (Converting from 2's complement)

Apparently this problem is a very old one. I found this interesting paper by Guy Steele in 1976 that explains it in detail: Arithmetic Shifting Considered Harmful. At the time, right shifting instead of dividing by 2 was a common compiler optimization and compiler writers either did not grasp or did not document the difference. What amazes me is that 30 years later, I faced the exact same problem because this difference is still not properly documented. Ironically, even the Java Language Specification which was co-authored by Guy Steele has only a very cryptic reference to this issue. Of course the situation in Java is still better than C/C++ where the result of right shifting a negative integer is compiler/platform dependent.