I wrote a simple (so I thought) starfield animation for a personal Python project of mine. Right about the time that the game got to the “playable” stage, I decided to undertake another project; experimentation with a Windows/Ubuntu dual-boot. While the details of the Windows/Ubuntu experiment are a real page-turner, the relevant issue is more one of, “why does this code work fine on Windows XP but causes all manner of crashes on Ubuntu 12.04”?
This code was written for Python 2.7 using Pygame 1.9.
import pygame, random, sys
from pygame import *
pygame.init()
width = 400
height = 400
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
DISPLAYSURF = pygame.display.set_mode((width, height))
stars = 50
starfield = []
for i in range(stars):
x = random.randrange(0, width)
y = random.randrange(0, height)
starfield.append([x, y])
clock = pygame.time.Clock()
FPS = 60
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
DISPLAYSURF.fill(BLACK)
starCounter = 0
for star in starfield:
starCounter += 1
x, y = star
starDraw = pygame.PixelArray(DISPLAYSURF)
starDraw[x][y] = WHITE
del starDraw
#fake parallax scrolling effect
star[1] += 1
if starCounter % 3 == 1:
star[1] += 1
if starCounter % 5 == 1:
star[1] += 1
if star[1] > height:
star[0] = random.randrange(0, width)
star[1] = 0
Eventually I solved this by replacing the PixelArray nonsense with surface.set_at(), and removing all the code relevant to instantiating PixelArray objects. But it wasn’t just segfaults (of the Pygame parachute variety), I would get all kinds of weird error messages; something about gcclibrary, something about malloc(), etc etc. Keep in mind I wasn’t using any IDLE or anything like that before; I always run everything from the command line and/or Terminal.
The kind of errors I used to experience don’t necessarily concern me; the fact that Windows would just silence them while Ubuntu was more than happy to point and laugh at them does. Clearly, this must have been the result of PEBKAC, but why on Earth would Windows keep that information from me? And on the flipside of this argument; will coding under Ubuntu forgive a different kind of bad habit? If Windows silences certain kinds of segfaults, does Ubuntu catch a different set of errors, and do I really need to be mindful of all the different goofball errors that every major OS might decide to catch or not catch? I’ve only been at this for about a year, so there’s a lot of information I’m just underexposed to and unaware of.
And I mean, I guess if you want to show me the error of my old code in case I ever feel that creating PixelArray objects is something I need to do in the future, I don’t really mind that either. 🙂
The code you provided works without crashing on my machine (Ubuntu 11.10, Python 2.7.2). It also works if I comment out the
while Trueblock so that the later code (PixelArrayetc) runs. Since there is no display loop or anything, nothing much is shown besides a black window popping up briefly, but there are no exceptions of any kind.Python under Windows won’t hide segfaults etc, there is something wrong with the Ubuntu setup. I suspect it is one of three things:
If you edit your question to include the full traceback of the errors, somebody may be able to figure out just where things are broken.