8

I'm very new to python but I'd like to learn it by making games and pygame seems to be the best option. Since PyPy is the fastest implementation of python (I think) I decided to use that one. But I have no idea how to get those two working together.

I'm on windows.

If anyone would be so kind to give me a step by step on what I need to do I'd be really grateful.

So far, I've installed (extracted to a folder) PyPy, set the pypy.exe as the default for opening .py files, installed Pygame and tried running one of the example .py files. I get the "module pygame not found" error with the first import line in the file.

Luka Horvat
  • 4,283
  • 3
  • 30
  • 48
  • 3
    I would recommend just learning Python and PyGame with the regular interpreter first and the checking out PyPy later after you know something. – martineau Nov 29 '12 at 21:40
  • Well, I've programmed before so learning isn't a problem. I even worked in python before but not that much. Thanks for the suggestion though. Looks like it's my only option. – Luka Horvat Nov 29 '12 at 21:48

4 Answers4

9

pygame isn't compatible with pypy, so to use it you'll have to stick with cPython.


Update (April 2018):

As pointed out by in this answer the PyPy v6.0 release now works with pygame - although not yet with the current stable (pygame 1.9.3) release, but with the current developement branch (1.9.4.dev0).

tested on ubuntu 17.10 by:

  • downloading and extracting the latest precompiled version for linux
  • installing the build dependencies for pygame: sudo apt build-dep python-pygame
  • installing pip: ./bin/pypy3 -m ensurepip
  • installing pygame: ./bin/pypy3 -m pip install 'Pygame>=1.9.4.dev0'
  • running the demo: ./bin/pypy3 -m pygame.examples.aliens

Works for both the pypy3 and pypy2 versions.

Community
  • 1
  • 1
mata
  • 67,110
  • 10
  • 163
  • 162
  • 1
    It might be noting that this isn't necessarily as bad as it sounds since PyGame does most of the heavy lifting in C, not Python. Of course, you won't be writing the next Cyrsis using PyGame, but PyGame can still be used to write some pretty fun games, without needing to resort to PyPy to JIT the Python iterpretter. – jlund3 Nov 29 '12 at 22:02
  • 1
    I haven't tested this but by searching I saw https://bitbucket.org/stefanor/pygame-pypy. Has anyone tried this? – Hophat Abc Jan 06 '13 at 06:53
  • Pygame isn't incompatible with PyPy anymore since v6. See @afaulconbridge's [answer](https://stackoverflow.com/a/50045201/6220679) and the [PyPy v6.0 release notes](https://morepypy.blogspot.de/2018/04/pypy27-and-pypy35-v60-dual-release.html). – skrx Apr 26 '18 at 15:01
  • @skrx thanks for the hint, I've verified that it does indeed work now, 5 1/2 years after the initial post... – mata Apr 26 '18 at 18:47
4

Pygame games actually spend very little of their time running python code. The vast, vast majority is spent doing SDL fill and flip operations. Most fills are unnecessary. How important is this? Well, take my computer. Say you write a game that has a loop that just paints the background one color. It will get about 40 fps. This is because it's basically going to every pixel individually and writing to it. This is using 200 x 300 = 60000 operations every frame to do nothing.

So instead of painting the entire background, just paint the parts that were drawn on the previous frame.

This makes your code a bit more complicated, but it produces a huge performance increase.

Also, don't forget to run cProfile to see where the problem areas are. Look, don't guess.

Nick ODell
  • 15,465
  • 3
  • 32
  • 66
3

It looks like PyPy v6 (April 2018) will improve the situation and make PyPy compatible with PyGame and other C python extensions. See https://renesd.blogspot.co.uk/2018/03/pygame-on-pypy-usable.html for an example.

afaulconbridge
  • 1,107
  • 9
  • 21
2

We currently don't have binaries for PyPy on the package index yet. So you need to compile pygame from source, rather than just using pip to install pygame.

Get a PyPy version 6+ (or download a nightly one).

The instructions for compiling from source on windows can be used: https://www.pygame.org/wiki/CompileWindows

Just use the pypy3 binary instead of the python one.

René Dudfield
  • 591
  • 4
  • 10
  • Hi René, could you take a look at this [question](https://stackoverflow.com/q/54049637/6220679) and my answer? When the pygame window loses input focus, the `state` of the `ACTIVEEVENT` is 2, but when it gains focus, the `state` of the `ACTIVEEVENT` is 6. Is that correct or is anything wrong? I assumed the state is only 6 when the window is maximized or minimized. – skrx Jan 05 '19 at 10:35
  • pip does the compilation automatically though, I just installed pygame with `pip install pygame` and pypy3.8. I had to set the ZLIB_ROOT environment variable though. – Octo Poulos Oct 11 '21 at 16:00