SonofPoseidon
SonofPoseidon

Reputation: 51

How do I deal with pause/resume methods in pygame?

I hope I can to the right place.

I have produced a pygame app for android, however the p4a recipe does not include certain pygame-sdl methods such as APP_WILLENTERBACKGROUND or APP_DIDENTERFOREGROUND. When the app moves into the background (it continues to run, but the graphics freeze when brought forward). When I want to show ads, the app crashes. I understand how to use on_pause/on_resume methods with kivy, but I am using the pygame recipe. With pygame-sdl, I'd do something like this:

if pygame.APP_WILLENTERBACKGROUND==TRUE:

   game_loop=False
  
elif pygame.APP_DIDENTERFOREGROUND==TRUE:

   game_loop=True

However, is there another way I can do this without having to use pyjnius? cheers,

Nick

Upvotes: 3

Views: 128

Answers (1)

Vito Gentile
Vito Gentile

Reputation: 14386

Looks like you've found the answer on reddit. I'm posting it here too, just in case someone else need it in the future.

It seems you can use the SDL2 constant values to check for them on Android:

for event in pygame.event.get():
    if event.type == 259: # SDL_APP_APP_WILLENTERBACKGROUND 
        print('App about to enter background')
    if event.type == 260: # SDL_APP_DIDENTERBACKGROUND
        print('App entered background')
    if event.type == 261: # SDL_APP_WILLENTERFOREGROUND
        print('App about to enter foreground')
    if event.type == 262: # SDL_APP_DIDENTERFOREGROUND
        print('App entered foreground')

Not very elegant, but it works also on Pygame 2 built with buildozer.

Upvotes: 0

Related Questions