Saturday, October 11, 2014

Using Android Accelerometer with Pygame

My son recently started to use the accelerometer functions in his Android phone to develop games with Python. He's building a variation of Swarm, a 2D tile map game that he wrote in middle school.

The accelerometer is easy to get working with pgs4a.

To pull the x,y,z axis, use this:


You will get a floating point number for each of the three axises.

Here's a simple way to set four directions, up, down, left, right:


In the example above, I'm holding the phone sideways, in landscape mode.  The left-right movement is controlled by the 2nd value in the list and the up-down movement is controlled by the 1st item in the list.

It would easy to set up 8 direction movement or even a greater range of movement.  Unlike Swarm, which used only 8 directions for bullets, we're using sin and cos to give the bullets a greater range of movement.

In order to test the application on my desktop, I've also created a virtual controller to simulate the accelerometer.

Here's the application running on my desktop with the virtual controller.
video


After you install the application on your Android phone, it is a bit more difficult to debug and test the accelerometer.   You can print out the value of the accelerometer and then tune your game so that the player moves with the sensitivity that works for your game. If you print out the accel_reading, you will see a three number tuple, with the numbers all in floating point.


I/python  (21058): Initialize Python for Android
I/python  (21058): ['/data/data/org.pychildren.accel/files/lib/python2.7/site-packages', '/data/data/org.pychildren.accel/files/lib/site-python']
I/python  (21058): Opening APK '/data/app/org.pychildren.accel-1.apk'
I/python  (21058): (-3.8019924163818359, -0.31603461503982544, 8.7819318771362305)
I/python  (21058): (-3.8019924163818359, -0.31603461503982544, 8.7819318771362305)
I/python  (21058): (-3.8019924163818359, -0.31603461503982544, 8.7819318771362305)

If you don't know how to see the output from your app when it is running on your phone, read my   post explaining how you can see the output of your print statements with adb.

Here's a demo of the character running on an old Samsung phone without the flock.   The controller in the lower right is for 360 degree bullet firing.

video

6 comments:

  1. Good blog as usual, The best thing about these blogs, its simplicity in term of learning. In the beginning phase of android course learning these are most beneficial.

    ReplyDelete

  2. Thanks for sharing information.
    online recharge
    Existing customers can avail the tariff benefit of ALL LOCAL CALLS at 25P/min for 3 months on Special Tariff voucher (STV)priced at Rs 38 only in Haryana.

    ReplyDelete
  3. Hey this is the best android game called food jump just realeased in Google Play store,I just played this game food jump this is awesome and addictive get the download link here
    food jump

    ReplyDelete
  4. I always read blog but this is one of the best for the viewers thanks for the real and helpful blog.

    mobile game developers

    ReplyDelete
  5. The first one is such an incredible picture. I love how it is just black, white and red, it leaves such an amazing effect. visit: Programming Games For Kids

    ReplyDelete
  6. Thank you for sharing this article, it is great info provide me. visit best leading
    mobile game developers

    ReplyDelete