Design solutions / tips for a simple game on Android

I need your advice to best implement a scrollable playing field in a simple game. The field consists of several rows, 9 cells in each row. Lines can be dynamically added and deleted during the game. Each cell has a number in it and, possibly, several superimposed images, for example, select, selector, cross out, etc. (See Fig. Below). The user should be able to click on individual cells in the row to select / deselect / cross the affected cells. At any moment of the game, there can be from 0 to 3000 cells (from 0 to 333 lines). The field should scroll up and down smoothly.

alt text

I was thinking about having a ListView, each row of which is a row in a field. That way, I could add / remove lines dynamically during the game. However, how exactly should I implement the row: to have one bitmap representing the row, and then, when the user touches it, get the coordinates of the touch area and find out which cell was affected, and then act on this. Is it possible to get the contact coordinates of a string in a ListView? If not, should 9 fictitious image measurements be placed on each line, and then act on the user regarding these? How about performance?

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

Any other solutions?

I prefer it to be a β€œregular” game like an application, rather than a loop game, as I don't think I really need to redraw 30 times per second.

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

+8
android
source share
2 answers

You can easily make this setup quickly launched with a combination of "game loop" / SurfaceView. This means that there is no ListView, only custom processing of drawings and events. Fortunately, event handling is not that difficult, and you will win later because you will have much more control over the interface than if you went with a bunch of customized views and layouts.

I would avoid this tutorial www.droidnova.com, it unnecessarily uses HashMaps, which will cost you only in performance.

Here is how I would do it:

  • Create an object to store cell data. In this example, I would use an object with an identifier (for printing) and x and y to draw on the screen.
  • Determine the size of the board that will fit on your screen without scrolling, say, 10x10. You will add scrolling later.
  • Create a two-dimensional array of cell objects with a length of boardSize x boardSize. Fill objects with identifiers and x and y on the screen.
  • In your custom onDraw iteration through each "row" and "column" of your only array and draw an object by its stored x and y values.

So now you have a grid of objects displayed on your screen. Now you want to limit the number of rows currently displayed and add some functions to change which rows are visible. This is done as follows:

  • During initialization, configure some global ints like mCurrentRow = 0 and mNumVisibleRows = 3. They define your "viewport".
  • Modify the drawing code to draw lines starting with mCurrentRow and ending with mCurrentRow + mNumVisibleRows. The result of this should be that you only ever see mNumVisibleRows strings, and which group of strings you see depends on what you set mCurrentRow to.
  • Add triangles to the right of the grid drawing and touch touch events in these areas to increase / decrease mCurrentRow. Obviously, you should not allow this value to go beyond your line counters.
  • If you want to be cool, draw a line between your triangles for the scroll newCurrentRow = (touch.y / canvas.height()) * boardSize; and map the events to a connection with something like newCurrentRow = (touch.y / canvas.height()) * boardSize; It needs some tweaking, but you get this idea.

This approach has the disadvantage of showing only the full set of rows at a time, so scrolling will not be smooth. However, you have full control over your canvas and what you draw, so with a little more math, you can implement a smooth scroll mechanism that will be compensated for by the fractional row height in the y-direction instead of whole lines.

+4
source share

I do not know if you can create a game as you described with good performance. I would look at the basic programming of slot machines.

But, avoiding the standard components of the presentation, you have to write all the logic yourself, it requires quite some work. Things like handling click events on different lines should be calculated based on the position of the tile relative to the game camera. Thus, theres a lot of new things in learning how to develop a game at a lower level.

You also need to transfer the rendering of your game into your own hands by developing a game loop that constantly updates and draws your snippets from the background stream to reflect the scrolling / status of your game. You can get more information about the basics of the game cycle:

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

If you want to know more, you should see the following notes from android.

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

This gives you a very good idea of ​​developing games for andriod at a low level, where you can fine-tune performance.

+1
source share

Source: https://habr.com/ru/post/650334/


All Articles