I know these segmentation fault questions are common, but I couldn’t find answer and I am pulling my hair out as the code works without issue in some parts of the code, but not others or in some cases it works the first time then, the next day, it falls over.
Basically, I am importing spritesheets for a simply pygame game. With help, I wrote a small class which allows spritesheets to be imported and, through a few methods turns them into a list using the json file. That program is as follows:
import pygame
import json
pygame.init()
class Spritesheet:
def __init__(self, filename):
self.filename = filename
self.sprite_sheet = pygame.image.load(filename).convert()
self.data_addr = self.filename.replace('png', 'json')
with open(self.data_addr, encoding = 'utf8') as f:
self.data = json.load(f)
f.close()
def check_file(self):
print(self.data)
def get_sprite(self, x, y, w, h):
sprite = pygame.Surface((w, h))
sprite.set_colorkey((0,0,0))
sprite.blit(self.sprite_sheet,(0, 0),(x, y, w, h))
return sprite
def parse(self, index):
sprite = self.data['frames'][index]['frame']
x, y, w, h = sprite["x"], sprite["y"], sprite["w"], sprite["h"]
image = self.get_sprite(x, y, w, h)
return image
def dict_len(self):
length = len(self.data['frames'])
return length
This has worked fine in the main code. I have a player class, an enemy class etc and, upon initiallising, this class is called to create a list of pngs for the graphics (that I can step through etc.) A pertinent cut of the player class is as follows:
class Player(pygame.sprite.Sprite):
def __init__(self, player_posx, health):
super().__init__()
#right facing sprites
self.spritesr = Spritesheet('../graphics/player/players.png')
self.player_spritesr = []
for value in range(self.spritesr.dict_len()):
self.player_spritesr.append(self.spritesr.parse(value))
# left facing sprites
self.spritesl = Spritesheet('../graphics/player/playersl.png')
self.player_spritesl = []
for value in range(self.spritesl.dict_len()):
self.player_spritesl.append(self.spritesl.parse(value))
#static sprites
self.sprites_static = Spritesheet('../graphics/player/player_static.png')
self.player_static = []
for value in range(self.sprites_static.dict_len()):
self.player_static.append(self.sprites_static.parse(value))
self.image_index = 0
self.image = self.player_spritesr[self.image_index]
self.rect = self.image.get_rect()
In this class it works fine with the Spritesheets class. However, when I decided to create new file to import all the graphics in a separate file, I get the segmentation error. The code is simply:
from spritesheets import Spritesheet
test = Spritesheet('../graphics/player/playersl.png')
Calling exactly the same class with the same argument, but I get an error saying:
Fatal Python error: (pygame parachute) Segmentation Fault
Python runtime state: initialized
I really don’t understand at all, especially why it runs fine in one file but falls over everywhere else.
1
The likely problem is that you are doing your import before you have called pygame.init()
, so pygame has not been initialized.
Consider doing this in your second file:
from spritesheets import Spritesheet
def get_sprites():
return [
Spritesheet('../graphics/player/players.png'),
Spritesheet('../graphics/player/playersl.png'),
Spritesheet('../graphics/player/players_static.png')
]
Now you can put your import
at the top, and call
sprites = xxx.get_sprites()
after you have initialized pygame.