Android Tutorial [B4X] [BitmapCreator] Cross platform Falling Sand game


The main challenge here is the performance. All particles are updated if needed and redrawn every cycle.

The Particles array stores the state of all "game pixels". A value of 0 means that the pixel is empty. Other values mean that there is a particle on that pixel.
Sand particles move down or diagonally if they can.

The RowsState array tracks the rows that are considered dirty. Non-dirty rows are skipped.

Single dimension arrays of bytes can be accessed in an optimized way in B4i with Bit.FastArrayGet / Set methods ( I used these methods in some of the cases.

All the important code is implemented in the Game class, which is of course shared between the three projects. No sprites are used in this game. The drawings are done in the MoveParticles sub.
As in the Tetris game the layout is managed with the designer script.

You need to change the GameUtils module that is included in the zip files with the one attached separately.


- Recent version of XUI.
- BitmapCreator v3.6 (B4J, B4i)

The three projects are attached. I've also uploaded an executable jar. Download and double click.


    25.6 KB · Views: 742
    177.6 KB · Views: 548
    24.6 KB · Views: 602
  • GameUtils.bas
    6.5 KB · Views: 622
Last edited:

Roger Taylor

Licensed User
Longtime User
I have another question concerning the main game loop. It calls mGame.Tick(gs) after gs.GameTime is incremented. What does .GameTime actually control? I need another loop that runs as fast as the app will allow with no concerns about the FPS. This is where I need to do my CPU stepping. So far I've only been able to get a 60 Hz CPU rate because the MainLoop seems to be locked to this and that's where I temporarily placed my test CPU stepping call.

Also, does the GameStep class do all of the FPS maintenance? I see it locking to 30, 60, 90, 120 FPS... the nearest it can achieve for the device the app is running on.


B4X founder
Staff member
Licensed User
Longtime User
What does .GameTime actually control?
It allows the consumer to have a time measurement that is separate from the actual FPS.
I wouldn't spend too much time with this code as a much more powerful implementation is available in X2 framework.
In X2 it tries to maintain 60 FPS and reduces it to 30 FPS if needed. It tries to avoid fluctuations which are more noticeable than a lower frame rate.