Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Pygame without a window / GUI

Is it possible to run pygame without creating a pygame window, surface or GUI? I want to utilize certain pygame functions, but I don't want a GUI popping up.

For example, this function won't work unless I have set up a window within pygame.

running = True

def mainloop():
while True:

    for event in pygame.event.get():
        if ( event.type == pygame.QUIT ) or \
        ( event.type == pygame.KEYDOWN and \
        ( event.key == pygame.K_ESCAPE) ):
            running = False
            print "quit"
            pygame.quit()

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RETURN:
                print "working"
like image 533
RatstabOfficial Avatar asked Dec 18 '15 03:12

RatstabOfficial


People also ask

Does pygame have a GUI?

Pygame GUI is a module to help you make graphical user interfaces for games written in pygame. The module is firmly forward looking and is designed to work on Pygame 2 and Python 3.

Can you run pygame in a browser?

Since mid-2022, pygame has partial support for WebAssembly as upcoming python3. 11 and allows for running the same pygame code on desktops and mobile/web. You can publish your game on itch.io or github pages as some people have already done.

Is pygame init () necessary?

Not all pygame modules need to be initialized, but this will automatically initialize the ones that do. You can also easily initialize each pygame module by hand. For example to only initialize the font module you would just call.


2 Answers

DrevanTronder is correct, but he does not answer the question correctly, as you are asking how to do events in pygame without a GUI, as seen in your example coding.

This is not possible. When running a Pygame program, it only detects events if the Pygame window is selected. If there is no Pygame window, there is nothing to select, so events won't work. The following is pulled directly from the Pygame documentation:

"If the display has not been initialized and a video mode not set, the event queue will not really work." https://www.pygame.org/docs/ref/event.html

But, of course, if you are not talking about event handling specifically, then DrevanTronder is correct; all you need to do is import the module with "import pygame" and initialize it with "pygame.init()".

Hope this helps!

like image 161
Douglas - 15 year old Pythoner Avatar answered Nov 16 '22 00:11

Douglas - 15 year old Pythoner


If you are running Linux, you can use Xvfb.

Basically, Xvfb "captures" all display-related computations and performs them in-memory without it ever going to a display.

It may not suit your purposes, depending on your OS' behavior. Capturing keys in PyGame works only if the PyGame window is the active window but seeing that you won't be able to set the active window here, this will probably not work without additional workarounds.

like image 27
skytreader Avatar answered Nov 16 '22 02:11

skytreader