可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I recently bought a new macbook and I've been trying endlessly to get pygame
to work, but haven't succeeded yet. I'm getting pretty desperate and I could really use some help.
I've installed pygame 1.9.4
and even though I don't get any error messages when running pygame
code, it won't show me anything but a blank screen. I'm using the following code to test it:
import pygame
pygame.init()
screen = pygame.display.set_mode((800,600))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
screen.fill((255,0,0))
pygame.display.update()
pygame.quit()
On my old macbook the test code gives me a red screen as expected. Both macbooks are running python 2.7.10
.
Does anyone have any idea what I'm doing wrong? I think I installed pygame
exactly like I did on my old macbook and the only difference seems to be the operating system.
edit 1:
I write the script in Sublime Text and run the program in Terminal.
Screenshot
edit 2:
I got pygame working again by downgrading my operating system to macOS High Sierra.
回答1:
LATEST UPDATE for MacOS 10.14.1
If you download the official macOS x64 installer package of Python 3.7.2 from the official python.org page and type pip3 install pygame
, it works.
There's an issue with MacOS. It should be possible to fix in SDL.
- https://discourse.libsdl.org/t/macos-10-14-mojave-issues/25060/8
- https://bugzilla.libsdl.org/show_bug.cgi?id=4274
The pygame issue is here: https://github.com/pygame/pygame/issues/555
The homebrew issue is here: https://github.com/Homebrew/homebrew-core/issues/33016
回答2:
I have tested this on macOS 10.15 Catalina, Python 3.7.5, PyGame 2.0.0 (prerelease as of this writing) and PyGame 1.9.6 (stable as of this writing).
Initially, go to the GitHub page of Pygame and see which stable version is the latest. As of this writing, the latest stable version of PyGame is 1.9.6 and 2.0.0 is prerelease. If you want the latest (unstable/prelease just use master
when cloning below), if however you want the latest stable, change branch accordingly.
Here are the steps:
- Install some dependencies
brew install sdl2 sdl2_gfx sdl2_image sdl2_mixer sdl2_net sdl2_ttf
. This requires homebrew.
- Go to
site-packages
:
- For virtual environment go to
cd ~/.virtualenvs/myvirtualenv/lib/python3.X/site-packages
where ~/.virtualenvs/myvirtualenv
is the path to your virtual environment and python3.X
is the version of your Python.
- For system-wide installation go to
/usr/local/lib/python3.X/site-packages
where python3.X
is the version of your Python.
- Delete any previous pygame,
pip uninstall pygame
(if a pygame
directory exists in site-packages
then remove it: rm -rf pygame*
)
- Clone pygame from GitHub:
git clone https://github.com/pygame/pygame.git
for the latest (possibly not stable version).
git clone -b v1.9.6 https://github.com/pygame/pygame.git
for the latest (stable, based on what you checked above, v1.9.6
as of this writing)
- Go into the
pygame
directory: cd pygame
.
- Run
python setup.py --config --auto --sdl2
.
- Run
python setup.py install
(it will take a while).
Now PyGame should work as expected on macOS.
回答3:
Edit with working workaround:
see bottom of post
I get the same problem on a MacBook Pro (Retina display) with python3.7
and MacOs Mojave.
For the sake of simplicity, I did all my tests with your exact same code and pygame 1.9.4
.
On (two) Windows computers, the program works and shows a red window on :
python2.7
python3.6
python3.7
Hence it doesn't seem to be a Python version problem, I lean toward a MacOS Mojave problem.
Mojave introduced 'Dark mode' which automatically tint windows. When dark mode is disabled, I get a blank screen. When it is enabled, I get a dark screen. Might it be that MacOS takes control of the screen display and fiddles with the way pygame
controls the screen? It is just a theory but I don't know how to test it.
Edit:
I tried a pygame program that records and prints the position of the screen on which you clicked. It does output the position in the console, alas it doesn't display anything.
Apart from the faulty display, pygame
seems to work properly.
Edit:
For now, using the Python version from miniconda and using CogSci's pygame seems to work.
回答4:
I tried upgrading my old macbook to OS Mojave to see if pygame would stop working, and it did!
I guess Mojave isn't compatible with pygame (yet).
Thanks for the help!
回答5:
I ran into this problem with the following environment: Python 3.7, Mojave 10.14.2, Pygame 1.9.4
I found that downgrading to Python 3.6 fixes the issue
$ brew unlink python
$ brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb
That should fix it, if you get a circular dependency problem you should then run:
$ brew install --ignore-dependencies https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb
Details at https://apple.stackexchange.com/questions/329187/homebrew-rollback-from-python-3-7-to-python-3-6-5-x
回答6:
I had the same issue, got it to work by downgrading python to 3.6.5.
> brew unlink python brew install --ignore-dependencies
> https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb
> brew switch python 3.6.5_1 pip3 install pygame
If you already have python 3.6.5 install I guess you can just use switch.