DEV Community

Amar Husain
Amar Husain

Posted on

Setup pygame in termux

How to succefully install pygame in termux and get started

If you try to directly run

pip install pygame
Enter fullscreen mode Exit fullscreen mode

It will not work and you will get an error, because pygame is build on top of SDL library which is not present by default in termux environment but can be installed and set up easily, and this is what we will do in this tutorial inshallah.

Step 1

Install x-11 repo because this is where sdl2 modules are.

pkg i x11-repo
Enter fullscreen mode Exit fullscreen mode

Step 2

Install sdl2 modules.

pkg install sdl2 sdl2-gfx sdl2-image sdl2-mixer sdl2-ttf
Enter fullscreen mode Exit fullscreen mode

Step 3

install xorgproto package as it required to provides the header files required to build the X Window system.

pkg i xorgproto
Enter fullscreen mode Exit fullscreen mode

Step 4

Now you can install pygame from and you will not get any errors.

pip install pygame
Enter fullscreen mode Exit fullscreen mode

Step 5

let's start with a simple game, a ball move around the screen controlled by arrows keys of the keyboard.
create a python file and paste this code in it:

nano game.py
Enter fullscreen mode Exit fullscreen mode
# Example file showing a circle moving on screen
import pygame

# pygame setup
pygame.init()
screen = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock()
running = True
dt = 0

player_pos = pygame.Vector2(screen.get_width() / 2, screen.get_height() / 2)

while running:
    # poll for events
    # pygame.QUIT event means the user clicked X to close your window
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # fill the screen with a color to wipe away anything from last frame
    screen.fill("black")

    pygame.draw.circle(screen, "red", player_pos, 40)

    keys = pygame.key.get_pressed()
    if keys[pygame.K_UP]:
        player_pos.y -= 300 * dt
    if keys[pygame.K_DOWN]:
        player_pos.y += 300 * dt
    if list( keys)[80]:
        player_pos.x -= 300 * dt
    if list(keys)[79]:
        player_pos.x += 300 * dt

    # flip() the display to put your work on screen
    pygame.display.flip()

    # limits FPS to 60
    # dt is delta time in seconds since last frame, used for framerate-
    # independent physics.
    dt = clock.tick(60) / 1000

pygame.quit()

Enter fullscreen mode Exit fullscreen mode

Now pygame is installed and we have writen first game!

Let's run the program and see what will happen

python game.py
Enter fullscreen mode Exit fullscreen mode

Boom!!!

Nothing happened right?!
Don't be supprised, this is because termux is a text based CLI environment not a GUI, so we need to use an agent that support GUI to be able to render the game.
In this tutorial we will use an android app called "Xserver XSDL" you can install it for free from this link

once you installed the app open it and you will get an interface like this:

image of Xserver XSDL interface

what the app basically does is it offers a display that we can use to render GUI material from termux like pygame games.

to connect Xserver XSDL and termux go to termux open your .bashrc file by running:

nano ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

and add these two lines:

export DISPLAY="127.0.0.1:0"
export PULSE_SERVER="tcp:127.0.0.1:4713"
Enter fullscreen mode Exit fullscreen mode

we almost there!

now to allow our games to have sound effects we need to install xfwm4 package, just run:

pkg i xfwm4
Enter fullscreen mode Exit fullscreen mode

Now reload bashrc file by closing termux app and starting it again.

Now we are already to run our first game follow the fowlliwing steps each time you want to run a game:

  1. insure Xserver xsdl is working in the background with the blue screen.

  2. if your game involve sound effects (pygame mixer module) then run in terminal:

xfwm4 &
Enter fullscreen mode Exit fullscreen mode

to run pulseaudioserver.

  1. run your python file:
python game.py
Enter fullscreen mode Exit fullscreen mode

and Boom Here we are!!!
image of the first game

Top comments (0)