Using Pygame with PyPy

僤鯓⒐⒋嵵緔 提交于 2019-12-08 17:36:05

问题


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.


回答1:


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.




回答2:


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.




回答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.




回答4:


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.



来源:https://stackoverflow.com/questions/13635144/using-pygame-with-pypy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!