Segmentation fault: 11 when I run Pygame

2019-07-18 09:36发布

问题:

Somebody asked a similar question here, but it didn't get answered.

I have Python 2.7.11, OSX 10.11, and pygame 1.9.1. Every time I run the program, the window appears, and then closes with the error "Segmentation fault: 11". I got this from the pygame tutorials, and then modified it slightly to prevent an infinite loop. (the same error occurs even if I use the "while 1" loop used in the tutorial.)

Why is this happening and how can I fix it? Thanks!

import sys, pygame
pygame.init()

size = width,height = 320,240
speed = [2,2]
black = 0,0,0

screen = pygame.display.set_mode(size)

ball = pygame.image.load("ball.gif")
ballrect = ball.get_rect()

for i in range(100):
    ballrect = ballrect.move(speed)
    if ballrect.left < 1 or ballrect.right > width - 1:
        speed[0] = -speed[0]
    if ballrect.top < 1 or ballrect.bottom > height - 1:
        speed[1] = -speed[1]

    screen.fill(black)
    screen.blit(ball,ballrect)
    pygame.display.flip()

回答1:

After a bit of research it appears to be a know issue when the interpreter is running in interactive mode. I have found an official python patch that may help. To use it, open a terminal session in Terminal.app (or other shell), then enter:

curl -O http://bugs.python.org/file32324/patch_readline_issue_18458.sh
openssl sha1 patch_readline_issue_18458.sh
# the digest should be 7cb0ff57820a027dd4ca242eb2418930f8f46b4c

then

sh ./patch_readline_issue_18458.sh

Enter password if prompted! Let me know if this worked!



回答2:

Just in case anyone else has the same problem, I figured it out. It crashes because the SDL1 that comes with OS X 10.11 is a bit broken.

The solution is to download the runtime library SDL1 from here. Open the dmg after downloading it, and move the SDL.framework file from it into /Library/Frameworks. Since there was already one there, I merged the two, but replace should also work. Pygame works now!