Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design decisions / advice for a simple game on Android

Tags:

android

I need your advice on the best way to implement a scrollable playing field in a simple game. The field consists of multiple rows, with 9 cells in each row. The rows can be dynamically added and removed during the course of the game. Each cell has a digit in it, and optionally several overlayed images, like selection, selector over, crossed out, etc. (see the pic below). The user should be able to click on individual cells of a row to select/deselect/cross the touched cells. At any point in the game there can be from 0 to 3000 cells (0 to about 333 rows). The field should scroll up and down smoothly.

alt text

I was thinking about having a ListView with its each row being a row on the field. That way i could add/remove rows dynamically during the game. However, how exactly should i implement a row: have one bitmap representing a row, and then when the user touches it -- get the coordinates of the touch area, and find out what cell was affected, and then act upon that. Is it possible to get the touch coordinates of a row in ListView? If not, should I place 9 dummy image placeholders in each row, and then act on user touching those? What about performance?

Or should I have one huge bitmap / canvas representing the entire field, place it inside a ScrollView and then calculate all the coordinates and update it as the user interacts with it? Is it going to be faster or slower than the ListView?

Any other solutions?

I prefer it to be a "regular" app type game, not a loop-based game, as I don't think I really need to redraw 30 times a second.

I am pretty new to Android. Thank you for your suggestions.

like image 701
Levon Avatar asked Dec 21 '10 08:12

Levon


2 Answers

You can easily make that kind of setup run quickly with a "game loop" / SurfaceView combination. That means no ListView, only custom drawing and event handling. Luckily the event handling isn't that difficult, and you'll win later on because you'll have much greater control over the interface than if you had gone with a bunch of customized views and layouts.

I'd avoid that www.droidnova.com tutorial, it uses HashMaps unnecessarily, which will only cost you in performance.

Here's how I'd go about it:

  1. Create an object to hold your cell data. For this example, I'd use an object with an id (to print), and an x and y for where to draw on the screen.
  2. Decide on a board size which will fit on your screen without scrolling, say 10x10. You'll add the scrolling later.
  3. Create a 2-dimensional array of cell objects with lengths boardSize x boardSize. Fill the objects with id and x and y position on the screen.
  4. In your custom onDraw, iterate through each "row" and "column" of your single array and draw the object at its stored x and y value.

So now you've got a grid of objects displaying on your screen. Now you want to restrict the number of rows currently displayed and add some functionality to change which rows are visible. This is done as follows:

  1. During initialization, set up some global ints as mCurrentRow = 0 and mNumVisibleRows = 3. These define your "view window".
  2. Modify your drawing code to only draw rows starting at mCurrentRow and ending at mCurrentRow + mNumVisibleRows. The result of this should be that you only ever see mNumVisibleRows rows, and which group of rows you see depends on what you set mCurrentRow to.
  3. Add some triangles to the right of your grid drawing and have tap touch events in those areas map to increments/decrements of mCurrentRow. Obviously, you should not allow that value to go outside your row count bounds.
  4. If you want to get classy, draw a line between your triangles for a scroll area, and map touch events there to something like newCurrentRow = (touch.y / canvas.height()) * boardSize; That needs some tweaking, but you get the idea.

This approach has the downside of only showing a full set of rows at a time, so scrolling wouldn't be smooth. However, you have complete control over your canvas and what you draw, so with a little more math you could implement a smooth scrolling mechanism which would offset by a fractional row height in the y-direction instead of whole rows.

like image 141
Josh Avatar answered Nov 03 '22 12:11

Josh


I dont know if you can create a game like you described with good performance. I would look into basic tile game programming.

But by avoiding the standard view components are have to write all the logic yourself requires quite some work. Things like handling "click" events on different rows needs to be calculated by the tile position relative to the game camera. So theres alot of new stuff in learning to develop game at a lower level.

You also have to take the rendering of your game into your own hands by developing a game loop that constantly updates and draw's your tiles from a background thread to reflect the scroll / state of your game. You can get more infomation on the basics of a game loop at:

http://www.rbgrn.net/content/54-getting-started-android-game-development

If you want to learn more you should see the following keynotes from android.

http://developer.android.com/videos/index.html#v=U4Bk5rmIpic http://developer.android.com/videos/index.html#v=7-62tRHLcHk

Those give you a very good insight in developing games for andriod at a low level where you can fine tune for performance.

like image 28
Mads Lee Jensen Avatar answered Nov 03 '22 12:11

Mads Lee Jensen