I’ve recently started building a small platformer style game in Python using Pygame, inspired by Super Mario or rather, my own take on it called Super Marie Adventure. Everything was going well, a segmentation fault triggered right after calling pygame.init()
.
This was unexpected. It wasn’t just a normal Python error it was a fatal crash. Here’s how I traced the issue, fixed my code, and added new features to make the game run better and cleaner.
The Original Code That Cause the Crash
Here’s the code I was working with initially. At first glance, it looked fine. I had a Map
class to handle background movement and a mainGame()
function to launch the loop.
import pygame
from pygame.locals import *
import sys
SCREENWIDTH = 822
SCREENHEIGHT = 199
FPS = 30
class Map():
def __init__(self, x, y):
# load background image
self.backgroud = pygame.image.load("images/bg.png").convert_alpha()
self.x = x
self.y = y
def map_rolling(self):
if self.x < -790:
self.x = 800
else:
self.x -= 5
def map_update(self):
SCREEN.bilt(self.backgroud, (self.x, self.y)) # Typo here
def mainGame():
global SCREEN, FPSCLOCK
score = 0
over = False
pygame.init()
FPSCLOCK = pygame.time.Clock()
SCREEN = pygame.dispaly.set_mode((SCREENWIDTH, SCREENHEIGHT)) # Another typo
pygame.display.set_caption("玛丽冒险")
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
FPSCLOCK.tick(FPS)
if __name__ == "__main__":
mainGame()
What Went Wrong? Debugging the Crash
The error I got was:
Fatal Python error: (pygame parachute) Segmentation Fault
This error appeared as soon as pygame.init()
was called. Here’s what I discovered after some digging:
- Typographical mistakes:
pygame.dispaly
should bepygame.display
.SCREEN.bilt()
should beSCREEN.blit()
this was silently breaking everything!- I also had
self.backgroud
instead ofself.background
(just for code clarity).
- Missing resources:
- If
images/bg.png
doesn’t exist or is corrupted, Pygame can crash hard when calling.convert_alpha()
.
- If
- Unused classes:
- Even though I defined the
Map
class, I never actually used it inside the game loop wasted potential!
- Even though I defined the
Final Fix and Improve Version
After identifying the root causes, I rewrote the game with the following improvements:
- Fixed typos that broke core logic
- Error handling for image loading
- Used the
Map
class properly to scroll the background - Cleaned up the main loop for stability and better structure
Here’s the polished version:
import pygame
from pygame.locals import *
import sys
import os
# Constants
SCREENWIDTH = 822
SCREENHEIGHT = 199
FPS = 30
class Map():
def __init__(self, x, y):
try:
self.background = pygame.image.load("images/bg.png").convert_alpha()
except pygame.error as e:
print(f"Error loading background image: {e}")
sys.exit()
self.x = x
self.y = y
def map_rolling(self):
if self.x < -790:
self.x = 800
else:
self.x -= 2 # Reduced speed for smoother scroll
def map_update(self, screen):
screen.blit(self.background, (self.x, self.y))
def mainGame():
pygame.init()
FPSCLOCK = pygame.time.Clock()
SCREEN = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT))
pygame.display.set_caption("Super-Marie Adventure")
# Load Map
game_map = Map(0, 0)
# Main loop
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
game_map.map_rolling()
SCREEN.fill((0, 0, 0)) # Clear screen each frame
game_map.map_update(SCREEN)
pygame.display.update()
FPSCLOCK.tick(FPS)
if __name__ == "__main__":
mainGame()
New Feature I Added
With the fixed foundation, I went a step further and added useful functionality:
Error Handling
The game now prints an error and exits cleanly if the background image is missing, rather than crashing.
Map Scrolling
I implemented map_rolling()
so the background keeps moving, simulating side-scrolling — a key feature for a Super Mario-style game.
Clean Game Loop
Calling SCREEN.fill((0, 0, 0))
clears the previous frame to prevent graphical glitches and ghosting.
Final Thought
This little bug was a big lesson in attention to detail. A couple of typos caused a low-level segmentation fault something you don’t usually see in Python. Once I cleaned up the code and handled missing resources gracefully, everything started working beautifully.
I also learned that it’s not enough to just define classes or structures you have to use them meaningfully in your game logic. Now that the base is stable, I’m excited to start adding player movement, enemies, and score tracking next.