Created
June 13, 2025 23:14
-
-
Save EncodeTheCode/2835ac981801362be4251095b5b09a9f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
import random | |
import time | |
# Init Pygame | |
pygame.init() | |
screen = pygame.display.set_mode((800, 600)) | |
pygame.display.set_caption("Level Loading with Stutters") | |
clock = pygame.time.Clock() | |
# Colors | |
WHITE = (255, 255, 255) | |
GRAY = (50, 50, 50) | |
GREEN = (0, 255, 0) | |
RED = (255, 0, 0) | |
class LoadingBar: | |
def __init__(self, x, y, width, height): | |
self.pos = (x, y) | |
self.size = (width, height) | |
self.progress = 0.0 # 0.0 to 1.0 | |
def update(self, progress): | |
self.progress = max(0.0, min(progress, 1.0)) | |
def draw(self, surface): | |
x, y = self.pos | |
w, h = self.size | |
pygame.draw.rect(surface, GRAY, (x, y, w, h), border_radius=5) | |
pygame.draw.rect(surface, GREEN, (x, y, w * self.progress, h), border_radius=5) | |
class Level: | |
def __init__(self, name): | |
self.name = name | |
self.loaded = False | |
def load(self, loading_bar, surface): | |
total_steps = 1250 // 3 # 416.666 | |
progress = 0 | |
while progress < total_steps: | |
# Simulate variable load time per step | |
stutter_chance = random.random() | |
if stutter_chance < 0.1: | |
time.sleep(0.2) # simulate freeze/stutter | |
elif stutter_chance < 0.6: | |
time.sleep(0.0090) | |
elif stutter_chance < 0.3: | |
stutter_chance = random.random() | |
time.sleep(0.025) | |
if stutter_chance < 0.1: | |
progress += 1.5 | |
elif stutter_chance < 0.150: | |
progress += 0.75 | |
elif stutter_chance < 0.6: | |
progress += 0.20 | |
elif stutter_chance < 0.150: | |
stutter_chance = random.random() | |
time.sleep(0.05) | |
if stutter_chance < 0.1: | |
progress += 1.0 | |
elif stutter_chance < 0.150: | |
progress += 0.5 | |
elif stutter_chance < 0.6: | |
progress += 0.10 | |
else: | |
time.sleep(0.01) | |
progress += 0.49 | |
loading_bar.update(progress / total_steps) | |
# Draw update | |
surface.fill((30, 30, 30)) | |
loading_bar.draw(surface) | |
# Optional text | |
font = pygame.font.SysFont(None, 36) | |
text = font.render(f"Loading {self.name}... {int(loading_bar.progress * 100)}%", True, WHITE) | |
surface.blit(text, (loading_bar.pos[0], loading_bar.pos[1] - 40)) | |
pygame.display.flip() | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
pygame.quit() | |
exit() | |
self.loaded = True | |
# Create loading bar and level | |
loading_bar = LoadingBar(200, 300, 400, 40) | |
level = Level("Level 1") | |
# Load level | |
level.load(loading_bar, screen) | |
# Main game loop starts after loading | |
running = True | |
while running: | |
screen.fill((0, 0, 0)) | |
font = pygame.font.SysFont(None, 48) | |
text = font.render("Level Loaded!", True, GREEN) | |
screen.blit(text, (280, 280)) | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
running = False | |
pygame.display.flip() | |
clock.tick(60) | |
pygame.quit() |
Using this code, you can simulate any game's loading bar in almost exactness. It's pretty handy for remastering games and remakes of games. Or just a natural implementation of a loading bar into any game. Enjoy!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A almost perfect emulation of a PS1 (PlayStation One) game loading bar. Inspiration Lego Racers, Toy Story 2, Syphon Filter-series, among many other. It has this type of feel, slow natural loading screens as it loads resources from the disc.