Skip to content

RedEgs/pygame-qt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Pygame-QT

Pygame-Qt is a little demo of how I was able to make Pygame games run inside of PyQT5. At the time I was told it was impossible, so I decided to make it public for those who also want to implement this feature in their own projects.

main.py contains a demo that you can run, if you wish to test it out. There's LOTS of room for optimisation so this is just the minimum viable product to get working in projects.

How to use it

I recommend you become familiar with PyQT5 before attempting to implement this into your own projects straight away.

If you're already familiar with PyQT5, the pygamwidget.py is all you need for the Qt side of things.

As far as the pygame side goes, your pygame file will need a bit of tweaking to make the process as seamless as possible.

First, your whole game needs to be contained within a single class, you can check the top of the main.py file for reference.

Second, inside of your class, you will need a method which is responsible for running your game loop, which should contain your event loop, draw loop, update loop etc. In my main.py, I used a method called run() which looks like so:

def run_game(self):
    while self.run:
        self.clock.tick()
        self.handle_events()
        self.update()
        self.draw()

        yield self.screen   

Third, at the end of this run() method, you will need to yield the surface that you draw everything to, for example, I yield the window's surface. This makes it so the pygame loop returns the surface value every frame without breaking execution of the game.

Fourth, once you've configured you pygame code as described, we can now move onto the Qt side of things. Inside your main Qt window class, you will need to have a reference to the pygame widget through a variable or something similar. Once again, you can check my main.py file for reference.

Once you have a reference to it, whenever you wish to start the pygame game, you mush first instance the pygame class under a variable name, then you must call the pygame widget's set_process method. The method args require you to pass in your game instance as the game= arg, then you must pass in your method which yields your display surface into the process= arg.

Now you should have your game running inside of the widget. If you messed up or struggled to follow instruction, you can follow my actual code for reference.

Disclaimers

Performance

Using this method is going to have far from native pygame performance. Factors like pygame being software rendered, pygame and PyQT5 must both be on the main thread, surface -> image conversion, etc. These really slow down the pygame side of things, so there's definitely room for optimisation, even so, you probably won't even get half as much given all the stars align.

Event Handling

Since the pygame window is no longer the one recieving events, you must manually push your own constructed events to your pygame instance. This honestly isn't hard at all, I even briefly implemented keyboard input inside of the example, so you may expand that. It just becomes very tedious and in order for events to be sent to the pygame window, the Qt window must be capable of recieving them, this might become a problem when considering joystick input.

Extra

If you wish to check out a full project using this technique, you can look at the videos at this git repo here. This is what is capable using this method.

If you do use it in your project, i'd love to see it in action, so send it to me on discord or something! (My discord is redegs 😁). Same goes for if you need help, you can message me or raise an issue.

About

Enables pygame to be drawn to a pyqt5/pyside5 widget

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages