Jason’s babblings.

More awesome than a ten pound bag of flapjacks.

Archive for March 30th, 2010

Day 4: Let’s clean up the code.

Posted by Plaidman on 30th March 2010

Up to this point I’ve been hard-coding a lot of things in the game and not leaving a very much room for expansion. This is quick and good enough for testing things, but ultimately as the project gets bigger, it can get messy. Today we’ll do some code redesign so we can expand to add new map tiles and and character types – that means enemies! For today’s note, if you’re unfamiliar with Object Oriented programming and design, you might find yourself going a bit glossy-eyed. I’ll forgive you.

Our game uses a lot of X/Y coordinate pairs. It’s not a terrible inefficiency to have two variables here, but it looks a bit cluttery when declaring the two variables, and if we have to set both variables at the same time. A Coord object would do nicely to store both X and Y coordinates of anything – pixel coordinates, map tile locations, player locations, etc – so we can create and update both X and Y variables at the same time. Not necessarily more efficient, just cleans up the code a bit. We can also add some helper classes to this Coord object to check if it’s within a specified rectangle, or to check the distance between two of these types of coordinates easily. This will be handy later when we have characters interacting.

Next we’ll create a BitmapHelper object that initializes, processes, and stores all of the graphics files used by the game. Think of it like a librarian that knows exactly where all books are at all times. We can ask the librarian for any tile, creature image, or series of animation images and it’ll give them to us, ready to be drawn. Moving the graphics stuff into its own object is also nice if we want to use it in another app, we can just grab the object file instead of copying and pasting pieces of code from the Game object. We’ll also see benefit from this by not having to wade through game code to add another graphic, everything is neatly packed away in the graphics object.

Current demo: Aside the background changes, I added a walking animation so Red walks to adjacent tiles instead of jumps.

Posted in Android | No Comments »