Segmentation fault: 11 when trying to run Pygame - python

I am using OSX 10.11.6, Python 2.17.12 and Pygame 1.9.1.
I made this simple program that should display a black rectangle in the middle of a white field. However, when I try to run it I get an error saying:
Segmentation fault: 11
I have tried several things, but nothing seems to work. Here is my code:
import pygame
pygame.init()
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
gameDisplay = pygame.display.set_mode((800, 600))
pygame.display.set_caption('Slither')
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay, black, [400, 300, 20, 20])
pygame.display.update()
pygame.quit()
quit()
Does someone know how I can solve this issue? Thanks in advance!
Note: I am writing my code in Atom, and running it in Terminal using this command:
$ python2.7-32 slither.py

This is due to a flaw in the built-in SDL library that Pygame depends on. Pygame can create a screen, but attempting to touch it will immediately crash it with Segmentation error 11.
From the official SDL website, go to the download page and get the runtime library 1.2.15 for Mac. Open the .dmg you downloaded and you'll be given an SDL.framework file. Open /Library/Frameworks in Finder and move the framework file there. You may need to select Replace.

Related

pygame.display.update() doesn't work well [duplicate]

This question already has an answer here:
Pygame: Display not updating until after delay
(1 answer)
Closed 2 years ago.
I was programming pygame in Mac. Then, there was a problem.
I wrote the code like below, but pygame.display.update() doesn't work.
It should update and wait 3 seconds, but it wait 3 seconds first, and it updates after pygame.quit().
Can anyone tell me how to solve this problem?
this doesn't work:
import pygame
import sys
pygame.init()
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("hello")
font = pygame.font.SysFont("comicsans", 100)
text = font.render("hello", 1, (255, 255, 255))
win.blit(text, (200, 200))
pygame.display.update()
pygame.time.delay(3000)
pygame.quit()
sys.exit(0)
this works normaly:
import pygame
import sys
pygame.init()
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("hello")
font = pygame.font.SysFont("comicsans", 100)
text = font.render("hello", 1, (255, 255, 255))
win.blit(text, (200, 200))
pygame.display.update()
pygame.quit()
pygame.time.delay(3000)
sys.exit(0)
OS: Mac
Python Version 3.8.3
Pygame Version 1.9.6
Editor: Jupyter Notebook
The issue is on MacOS (and potentially other OS's), the screen only updates after pygame events are checked.
Normally, this is done by calling pygame.event.get() or pygame.event.poll().
However, if your program doesn't care about checking events, then a call to pygame.event.pump() will work as well.
This issue is briefly mentioned in the documentation for pygame.event.pump(), link here.
There are important things that must be dealt with internally in the
event queue. The main window may need to be repainted or respond to
the system. If you fail to make a call to the event queue for too
long, the system may decide your program has locked up.
So the solution is to call pygame.event.pump() (or another event function) after pygame.display.update().
That the corrected code would then be:
import pygame
import sys
pygame.init()
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("hello")
font = pygame.font.SysFont("comicsans", 100)
text = font.render("hello", 1, (255, 255, 255))
win.blit(text, (200, 200))
pygame.display.update()
pygame.event.pump() # Make sure events are handled
pygame.time.delay(3000)
pygame.quit()
sys.exit(0)

why wont python pygame wont work in spyder?

I recently downloaded pygame on my school pc(I have experience with it before), but when I try to open a window it does not work and are black even though I colored the window in the code.
I even copied this code:
import pygame
background_colour = (255,255,255)
(width, height) = (300, 200)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Tutorial 1')
screen.fill(background_colour)
pygame.display.flip()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
But it does the same thing.
Extra:
I use Spyder with python 3.6
Thanks in advance:)
Your code work in my mac. If you are in Windows, try to restart the PC. Also, try to execute your example from a command line.

Pygame runs without any issues, but nothing is drawn on the screen [duplicate]

This question already has answers here:
Problems getting pygame to show anything but a blank screen on Macos
(10 answers)
Closed 4 years ago.
I've been trying to get Pygame to work for a while now. It runs without any issues, but nothing is shown on the screen.
I did try to run the Pygame alien example game, it does not show up either - even though music starts playing
import pygame
pygame.init()
display_width = 800
display_height = 600
white = (0, 0, 0)
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption("This is a game!")
clock = pygame.time.Clock()
running = True
while running is True:
for event in pygame.event.get():
if event.type is pygame.QUIT:
running = False
gameDisplay.fill(white)
pygame.display.update()
clock.tick(60)
In the above code I tried filling the screen with the color white, but the window is just blank (the default Mac OS colour)
EDIT:
It seems like it is a problem with Mac OS Mojave
To clarify the code, I tried with red as well as black and white in the end, and nothing shows up
According to this issue at GitHub this is a bug of Python installed with Hombrew.
UPDATE FIX: if you download the official macOS x64 installer package of Python 3.7.2 from the official python page and then pip3 install pygame it works.

pygame square following mouse cursor

When I run a pygame program, a white square (about 15px by 15px) follows my cursor around the window. This happens on the most basic pygame program and a few others, too.
My most basic pygame program:
import pygame, sys
DISPLAYSURF = pygame.display.set_mode((500,500))
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
# --- Limit to 60 frames per second
clock.tick(30)
Why is this happening? Is it my code, my system, or Pygame?
I am running python 3.7, pygame 1.9.4, and macOS 10.13.6.
EDIT:
After running the same program on Crouton (linux) there is no square.
Where is your pygame.init() at the beginning? That might be what is causing the problem. Also you do not have a background, which shouldn't be a problem, but I would try it if nothing else works.
I tried your code on my PC and it worked fine so I'm not sure what is happening but you could try the following:
Enter the command pygame.init() on the second line
Replace pygame.display.update() with pygame.display.flip()

PyCharm can't find reference in PyGame __init__.py?

OK, I’m very new to coding, and I am just learning Python. I figured I would start with some basic Pygame exercises to try to have something to program.
I’ve installed Python 3.4.3 and PyCharm. I also installed the Pygame executable “pygame-1.9.2a0-hg_5974ff8dae3c+.win32-py3.4.msi” from here: https://bitbucket.org/pygame/pygame/downloads
I ran the Pygame installer, and it seemed to complete without visible issues, though there were no obvious signs like new shortcuts on my desktop.
I then went here to try some basic test code involving Pygame:
http://pythonprogramming.net/pygame-python-3-part-1-intro/
So I copied the code from that example into my Pycharm, and ran it. It seems to create a blank Pycharm windows alright, but the PyCharm Code Inspector is giving me several warnings, and I really want to know why I am getting these warnings.
The first Pycharm warning is from line 5, “Cannot find reference ‘init’ in ‘__init__.py’
The next warning is line 16, “Cannot find reference ‘QUIT’ in ‘__init__.py’
The third and final warning is line 24, “Cannot find reference ‘quit’ in ‘__init__.py’
Why can’t it find these references? What’s wrong?
The code itself I paste below:
#! /usr/bin/python
import pygame
pygame.init()
gameDisplay = pygame.display.set_mode((800, 600))
pygame.display.set_caption('A bit Racey')
clock = pygame.time.Clock()
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
print(event)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
This will work for you with no errors... import the system and do a system exit instead
#! /usr/bin/python
import pygame
import sys
pygame.init()
gameDisplay = pygame.display.set_mode((800, 600))
pygame.display.set_caption('A bit Racey')
clock = pygame.time.Clock()
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
print(event)
pygame.display.update()
clock.tick(60)
pygame.quit()
sys.exit()

Categories

Resources