Learning From Lunar Lander

Lunar Lander is a sample game distributed with the SDK in <SDKDIR>/platforms/android-2.1/sample/ folder. It is available since Android 1.5. This project introduces 2D drawing on Canvas, handling KeyEvent, and using Thread. I think it is a good starting point for me to learn Android game programming.

For Lunar Lander, the main components are main LunarLander (Activity), a LunarView (SurfaceView), and a LunarThread (Thread). The layout XML defines a LunarView (for displaying the game scene), and a TextView for displaying in-game message (i.e. “Press up to play”, “Press up to resume”).

The LunarView contains a innerclass, LunarThread. LunarThread is the game engine. It runs the game loop and performs all the drawings and physics (game logic). LunarView delegates all KeyEvents to LunarThread.

As I am developing a game, I have used the Lunar as a template. There are some slight modifications. Instead of inner class, I made the game engine a separate class on its own. I want to separate the View from game engine.

Posted in Tao Of Programming at March 23rd, 2010. No Comments.

Determine Earliest Business Date

Suppose a customer may sends his/her request at any day, any time. The request can only be processed during business hours (weekdays, 9am to 5pm). Given a time at which the customer makes his/her request, I wish to determine the day that the request can be processed.

Examples:

  • If request is received at 2010-03-01 13:00:00 (Monday), the request can be processed on the same day.
  • If request is received at 2010-03-01 17:00:00 (Monday), the request will be processed on the following day (2010-03-02), because the cut-off time is 5pm.
  • If request is received at 2010-03-05 17:00:00 (Fri), the request will be processed on the following Monday (2010-03-08). It has past the cut-off time, and the request will not be processed during weekends.
  • If request is received at 2010-03-07 00:00:00 (Sunday), the request will be processed on the following Monday (2010-03-08). The request will not be processed during weekends.

I have written a function that determine the earliest business date in PHP. It returns an integer result (Unix timestamp).

Enchancements:

  • Allow user to specify cut-off time
  • Check for public holidays

Posted in Tao Of Programming at March 8th, 2010. No Comments.

[PHP] Dynamically Modify URL Query String

I have to work with URL query string frequently. Often, I have to change and reconstruct the query portion based on certain rules. I wrote a function that takes in a URL string and an associative array, combines them and returns a new URL string. The resultant string can be used in <anchor> tags and also for HTTP GET request.

Usage example:

$url = 'http://www.example.com/index.php?id=123&name=orange#anchor';
$query1 = 'id=246&category=fruit shop';
$query2 = array('id'=>246, 'category'=>'fruit shop');
echo modifyQuery($url, $query1);
echo "<br />";
echo modifyQuery($url, $query2);

The $query parameter can be either a string or an associative array. In my usage example, both function calls return the same output.

NOTE: A valid URL string may contain username, password and port number. My function will not work with this information, but you can easily modify the function to retain these information. Have you noticed that the id has been changed from 123 to 246? The function’s $query parameter will appends+overwrites the query string.

Posted in Tao Of Programming at September 29th, 2009. No Comments.

First Impression Of MOTODEV Studio

I have just published another post on GTUG KL. I installed the MOTODEV Studio, and have taken some screenshots. Read more about the installation process…

Posted in Tao Of Programming at September 12th, 2009. No Comments.

[Android] API Demos

Just post another entry at http://gtugkl.blogspot.com/2009/09/android-api-demos.html.

Share a helpful tip for learning Androd programming. The SDK package comes with examples that cover a variety of topics.

Posted in Tao Of Programming at September 6th, 2009. No Comments.