PyBox / Blog / How to

pygame on Android: how to run and tune a game

pygame runs in a window on the PyBox X11 desktop. A small game and a few settings are all it takes.

pygame on Android: how to run and tune a game: the PyBox screen this guide uses
PyBox is the Python IDE I build. Everything below works with it, and the steps are the same ideas you would use anywhere. Get it on Google Play.

Install and run

Add pygame to requirements.txt, sync, and run a script. The starter projects include Pygame Bouncing Ball, Snake Game, Pong, Breakout, Flappy Bird Clone and Pygame Particles. OpenGL Spinning Cube uses pygame with OpenGL.

A minimal game loop

import pygame

pygame.init()
screen = pygame.display.set_mode((640, 400))
clock = pygame.time.Clock()
x, y, vx, vy = 60, 60, 4, 3

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    x += vx
    y += vy
    if x < 20 or x > 620:
        vx = -vx
    if y < 20 or y > 380:
        vy = -vy
    screen.fill((12, 12, 16))
    pygame.draw.circle(screen, (127, 209, 174), (x, y), 20)
    pygame.display.flip()
    clock.tick(60)

pygame.quit()

Input

With touch mode set to Direct, a tap is a mouse click where you touch. Turn on Multitouch in Settings to send every finger to the app. A hardware keyboard works as usual, and PyBox has a toggle to hide the on-screen keyboard when one is attached.

If it draws wrong

  1. Open Settings, then GUI display, then OpenGL renderer.
  2. Switch from GPU to GPU safe.
  3. If it still misdraws, try GPU exact, and as a last resort CPU, which is exact but slow.

Frame rate

Use clock.tick(60) to cap your own loop. Settings also has a frame rate limit and the panel shows live FPS.

See GUI apps and OpenGL on Android for the whole picture.

Questions

Does pygame work on Android?

Yes, with PyBox. pygame is available as a prebuilt wheel and its window opens on the X11 desktop.

What if a pygame game draws incorrectly?

Try another OpenGL renderer in Settings: GPU safe (OpenGL 3.3), GPU exact (GL 2.1 and GLES 3.2) or CPU.

Anshuman MishraBuilds PyBox at Vytros, a Python IDE for Android. About the developer.

Code Python on your phone.