Pygame on Android
Asked Answered
T

8

32

I was wondering if someone could give me a detailed explanation on how to run a game/app developed using Pygame on an Android phone. I recently finished programming PacMan and it works perfectly on my computer, but I think it would be awesome if I could get it running on my phone. I tried following the instructions at http://pygame.renpy.org/android-packaging.html, but every time i run "import android" on the IDLE I get an error saying it did not find the module. Could someone clearly explain how to set up the android module?

Also, in my program I used code such as if (event.key == K_UP or event.key == K_w): direction = UP. However there are no arrow keys on a phone. What code would I need to use to see if the user swiped the screen with their fingers from up -> down or left -> right, etc.

Tombouctou answered 29/5, 2014 at 13:20 Comment(1)
Possible duplicate #102254Grassplot
S
12

There is a pyGame subset for android. However this requires special reworking and changing the program. Hopefully it will not be to hard.

http://pygame.renpy.org/writing.html

http://pygame.renpy.org/index.html

However about your second question i am unable to awnser because I am Not yet experienced enough.

Sholem answered 27/10, 2014 at 20:58 Comment(0)
T
3

i think the pygame subset for android would be good but i dont trust its functionality, i use kivy as its cross platform and if you ever decide to use the pygame subset for android your touch of flips on screen of an android device would be your mouse movement on the desktop so i ma saying treat the touch as the mouse good luck

Thievery answered 4/11, 2016 at 12:57 Comment(0)
A
3

I've runned pygame for android!!!!

Firstly, I'm debugged app using saving error to file. I got error that on android it can be runned only under fullscreen. I've created small app and it working:

import sys, os
andr = None
try:
    import android
    andr = True
except ImportError:
    andr = False
try:
    import pygame
    import sys
    import pygame
    import random
    import time
    from pygame.locals import *
    pygame.init() 
    fps = 1 / 3
    width, height = 640, 480
    screen = pygame.display.set_mode((width, height), FULLSCREEN if andr else 0)
    width, height = pygame.display.get_surface().get_size()
    while True:
        screen.fill((random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
        pygame.display.flip()
        time.sleep(fps)
except Exception as e:
    open('error.txt', 'w').write(str(e))

Screenshot: https://i.sstatic.net/4qXPe.png
requirements in spec file:

requirements = python3,pygame

APK File size is only 12 MB!

Anagnorisis answered 10/7, 2021 at 1:36 Comment(1)
That's amazing! How did you create an APK file from the python files?Edgardoedge
M
2

There are some pretty good answers for your first part already so I won't answer that. (I came here looking into what to use for it too!)

However the second part of your question should be a lot easier.

Have a mouse object that on a mouse down event will save the coordinates of the touch to an MX and MY variable Then when the mouse up event is triggered takes the new coordinates and calculates a vector using the MX and MY and this new point ie. The distance and angle of the swipe. Use trigonometry or the math module for the angle (research arctan2).

You can then use this in an if, elif, else to determine what quadrant the angle was and the distance to determine whether the swipe was valid if it's greater than a certain value.

I'm on mobile so unfortunately I can't give an example, however I'm certain you're apt to work out the solution with this guidance.

Mudpack answered 25/1, 2017 at 6:31 Comment(1)
This is a great idea. I am trying to apply this to my own game which is using pygame, pygame_sdl2 and rapt. Ideally, it will be a platformer for Android, so I hope to get these instructions to work. meanwhile, if you have suggestions/examples, it would be great. Thanks.Leviticus
I
1

For your second question, there is an answer in another website.

https://amp.reddit.com/r/Python/comments/2ak14j/made_my_first_android_app_in_under_5_hours_using/

it says You can try code like this

if android:
    android.map_key(android.KEYCODE_BACK, pygame.K_ESCAPE)

I hope it will help you.

Intussusception answered 21/2, 2020 at 3:26 Comment(0)
L
0

pygame.event has multiple touch-screen events. Here are some useful ones:

FINGERMOTION: touch_id, finger_id, x, y, dx, dy

FINGERDOWN: touch_id, finger_id, x, y, dx, dy

FINGERUP: touch_id, finger_id, x, y, dx, dy

MULTIGESTURE: touch_id, x, y, pinched, rotated, num_fingers

Loaiasis answered 8/2, 2023 at 19:59 Comment(0)
H
0

Python application into an Android APK - https://github.com/kivy/python-for-android/

Generic Python packager for Android and iOS - https://buildozer.readthedocs.io/en/latest/

Heirship answered 26/5, 2023 at 5:43 Comment(0)
A
0

Hey I can help you with your 2nd question First of all,as there are no keys on Android you need to make buttons and use them instead for that watch this video it's extremely helpful: https://youtu.be/G8MYGDf_9ho?si=JE8l7EjHE3NkXe6z This video will help you to create buttons to control your game. There's a part in video at 14:55 where he uses the function if start_button.draw():

You just need to put the buttons you made using your images from that tutorial in place of star_button and under this if condition write according to your code I hope this works out for you For you first question I'm not sure but you might need to import kivy first because when I was trying to import Android in the pydroid 3 app in my phone the python interpreter said Android is inside the kivy module so i need to import it first

Aramaic answered 5/2, 2024 at 11:18 Comment(1)
this isn't an answer but a comment... please provide specific details and codes as needed to get it qualified as an answer...Dionnedionysia

© 2022 - 2025 — McMap. All rights reserved.