Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Pygame with PyPy

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.

like image 477
Luka Horvat Avatar asked Nov 29 '12 21:11

Luka Horvat


People also ask

Does PyPy support Python 3?

PyPy supports Python 2.7. PyPy3, released in beta, targets Python 3.

Does pygame work on 64-bit?

You can download 64-bit pygame for Windows but not for Mac.

Can you use pygame in Visual Studio?

Pygame is a library that helps us create games using Python. Pygame uses SDL (short for Simple DirectMedia Layer) which helps us get access to the keyboard, mouse, and graphics. Pygame runs on almost every platform. This guide shows you how to import Pygame in Visual Studio Code.

Does Python 3.8 support pygame?

Pygame on Python 3.8 You should use the same command you use to run a Python terminal session on your system, which might be python , python3 , py , python3. 8 , or something else. If you've had any issues running Pygame on macOS, this version of Pygame should address those issues as well.


4 Answers

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.

like image 192
mata Avatar answered Oct 04 '22 18:10

mata


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.

like image 33
Nick ODell Avatar answered Oct 04 '22 17:10

Nick ODell


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.

like image 40
afaulconbridge Avatar answered Oct 04 '22 17:10

afaulconbridge


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.

like image 28
René Dudfield Avatar answered Oct 04 '22 19:10

René Dudfield