Convert Pygame code to android apk

In this article we compile python pygame code to android apk using p4a (python for android) and buildozer


Pygame Car game source code on github and compile apk link check out for testing


Step 1 - 

Here we need to load audio and images or assests file with obsolute path then it will correctly load on android , if your game using any assets file then do this as below example 


# your imports
import os


img = os.path.abspath('path/to/img')
audiofile = os.path.abspath('path/to/wavfile')


screen.blit(img, obj_loc)
mixer.music.load(audiofile)


Use os.path.abspath(args) method to load assets file in game


Step 2 - 

Also make your game responsive that fits on mobile phone screen height width if needed


And make sure your main code file name is main.py, Everything is ready to compile,


Compile it where you want ie Google colab or Github action or on local computer.


In this article we will use google colab now


Extra - You can use kivy and Pyjnius code too in pygame code to access android api like gyro, storage and more

also you can use all pypi module that work with p4a in your game


Step 3 - 

Click on this link - https://colab.research.google.com/drive/16wji2BWd5HHlp7eNqBBcZONCxxCzV-jy?usp=sharing and this link open online google workspace for you, which have all neccesory command are written, click on file icon to upload game code on it as example given below



Step 4 -

Click on upload icon to upload source code as example given below



Step 5 -

Now run each cell command one by one (ctrl+enter or click on square bracke`[ ]`)) which are shown in image given below 




Step 6 -

Click on Buildozer init command this will generate buildozer.spec file which used to mention app name, app icon, android permissions, and modules more (explore this file yourself)


Generated buildozer.spec file, customize it or play with it


Include file extention name that you are using in code 

for example text file is .txt and audio file .wav (or any other that are not here mentioned) which are not listed on source.include_exts line number 16 buildozer.spec file


example -

# (list) Source files to include (let empty to include all the files)
source.include_exts = py,png,jpg,kv,atlas,wav,txt




Include pip moduls that used in code if you're using any extra module,

These moduls and their version are needed to compile successfully code please add this in line number 40 buildozer.spec file in requirements fields

python3==3.10.12,kivy==2.3.0,pyjnius==1.5.0,hostpython3==3.10.12,pygame


example - 

# (list) Application requirements
# comma separated e.g. requirements = sqlite3,kivy
requirements = python3==3.10.12,kivy==2.3.0,pyjnius==1.5.0,hostpython3==3.10.12,pygame



Step 7-

Run final command buildozer android debug

Enter y and now further this will ask you to accept google license, accept these and this process will take 15 to 30 minute for first time to compile


*sometimes colab page may become upresponsive which is normal you can leave it come back some time later


After successful compilation apk file will be produced in bin directory



Step 8 -

Download the apk and install on android device, and if it crashes then use adb tool to debug it and fix and compile again



Your Pygame will be successfully compiled for Android apk (in distributable form) 


Hope it helps you to bring your game on android

Happy coding


Contact me at wishrohitv@outlook.com


Tags -

how to convert python pygame to android apk online

how to convert pygame code into android apk

online py to apk converter


suggetions - 


python pygame, android apk
1462
1732779090
wishrohitv wishrohitv
1732779090