Pygame For Android

Now it's time to port game written in pygame framework to Android. If you want to know how to create games in pygame, see tutorial here Pygame tutorial 1. If you already have working game for PC and want to port it, Pygame Subset for Android is the solution.
Web Site
http://pygame.renpy.org
Download
Download suitable package and unzip into your working folder http://pygame.renpy.org/dl/

Porting guide lines for andorid
To port games written for PC to Andorid, we need to do simple modification in code to. Below are some of the guideline for porting

1. File name should be main.py
  Andorid run time need file should be named as main.py and runs main method in that file

2. Entry point for game function should be should be def main()
  Andorid run time need file should be named as main.py and runs main method in that file

3. Default font in Android is not supported, You have to use font file. Below are few font providers.
  http://www.dafont.com/sf-comic-script.font
  http://cooltext.com/Download-Font-Comics+Cartoon

4. import Andorid module to enable Andorid related functionality adding below lines to your code makes code run in both on PC and andorid device
try:
    import android
except ImportError:
    android = None
  We need to init() the andorid related functions
   android.init()

5. Life Cycle management
if android:
    if android.check_pause():
        android.wait_for_resume()
At any time, Android may ask your game pause to itself. This may occur when the user hits pause button, when the device goes to sleep, or when user switches to another application( like you get calls ).

6. Key Mapping
   To operate game using PC instruction in pygame, We need to map pygame instruction to Android instruction. Below is the way to do it.
 if android:
    android.map_key(android.KEYCODE_BACK, pygame.K_ESCAPE)
 
      Andorid keyCodes can be found here

      Pygame keyCodes can be found here

7. Not every pygame module are supported in Android
      List of supported pygame modules

8. Not every python modules are supported in Android
      List of supported python modules

9. MOUSE BUTTON
    As there is no mouse in andorid, no way to distinction between mouse button action. so event.button event is not supported. If you have below kind of code in your app, its time to remove it.
 event.button == 1:
 

10. android.keystore :
    Its the certificate to install your app into Android device and to update your app in google play. In short dont loose it:D :D.       http://developer.android.com/tools/publishing/app-signing.html

11. Packaging:
  Icon:
  This is the image which you want to see as short cut when you install app in you Andorid device.
File name: pygame-icon.png






Presplash:
This is the first image which is displayed while loading your game
File name: pygame-presplash






These are present in below folder
.\templates


12. Debugging
      In emulator
You can follow the classic way of debugging by using print statements. Your print statement will be executed in android and will display on screen.
  andorid.py logcat 
 
 In andorid device
I have tried app called Log Collector which captures andorid logs and send it to different devices.
Log Collector

13: FPS: Frame Per Second is the rate at which you want to refresh/ draw your screen. You have to choose this as per your device. << ask Eric >> Option 1: Frame Per Second

 # Event constant.
 TIMEREVENT = pygame.USEREVENT

 # The FPS the game runs at.
 FPS = 30

 pygame.time.set_timer(TIMEREVENT, 1000 / FPS)

Option 2:
 self.clock = pygame.time.Clock()
 self.clock.tick(60)

Building package

1. Download and install PGS4A, Python 2.7, the Java Development Kit, and Android USB Drivers
   Install PGS4A from here

2. Use android.py installsdk command to install the Android SDK and set up your development environment.
Next step is to set up the Android SDK and the rest of development environment.

    This step do below things

        Checks that JDK is installed properly or not
        Installs Apache Ant
        Install Android SDK
Use the Android SDK to install the appropriate development packages. Create a signing key that will be used to sign packages that are placed on the market (android.keystore: this will be generated in the PGS4A directory)

run below command
   python android.py installsdk
   

3. After installation we need to configure Android-specific settings.

run below command
   python android.py configure mygame
   
Note: mygame is the folder where your will keep your pygame code
        Other configuration details are self explanatory

4. Use the android.py build command to build a the package on your device.

run below command
   python android.py build mygame release
   
5. Use the android.py build command to install the package on your device.

run below command
   python android.py build mygame release install
   
Note: To install you should have done SDK Manager.exe execution and
        AVD Manager.exe should be running, check below information.

AndoridSDK

SDK Manager.exe

In order to start developing applications, you must install the Platform-tools and at least one version of the Android platform, using the SDK Manager. Android SDK contains only basic SDK tools. It does not contains any andorid platform or any third-party libraries.

To install run
.\android-sdk\SDK Manager.exe and install the required things.
Below is my Andorid SDK Manager configuration.















Updating from command line
  tools\android.bat update sdk --no-ui
Tip:
use --help to see the various command-line options.
  http://developer.android.com/sdk/

AVD Manager.exe

The AVD Manager provides a graphical user interface in which you can create and manage Android Virtual Devices (AVDs), which are required by the Android Emulator. To run
 .\android-sdk\AVD Manager.exe
Below is my Andorid AVD Manager configuration.















Click on start to launch the Emulator






















Go to home, we can see the app which is newly created here
( I am alread did release install )



















Pushing app to google play

Once you are happy:D with your work. Its time to push it to google play store. To do this you need google play developer account. Click below link to create account

    google paly developer link It has four steps:

    1. Sign-in with your Google account
    2. Accept Developer Agreement
    3. Play Registration Fee
    4. Complete your Account details

1 comment: