Sushi Screensaver

This commit is contained in:
Justin Oros
2025-07-09 12:01:00 -07:00
committed by GitHub
parent 07d7a69a9a
commit 576a9191c0
7 changed files with 188 additions and 0 deletions
+103
View File
@@ -0,0 +1,103 @@
#!/usr/bin/env python3
# Description: Sushi Screensaver.
# Usage: python3 sushi-screensaver.py
# Author: Justin Oros
# Source: https://github.com/JustinOros
import pygame
import random
import os
import sys
# Initialize pygame modules
pygame.init()
# Get the current display size to create a fullscreen window
SCREEN_WIDTH, SCREEN_HEIGHT = pygame.display.Info().current_w, pygame.display.Info().current_h
FPS = 60 # Frames per second
# Background color (black)
BG_COLOR = (0, 0, 0)
def load_sushi_images():
"""
Loads sushi images from the local directory, scales them to 80x80 pixels,
and returns them as a list of pygame Surfaces.
"""
images = []
for i in range(1, 6): # sushi1.png to sushi5.png
path = os.path.join(os.path.dirname(__file__), f"sushi{i}.png")
if not os.path.exists(path):
print(f"❌ Missing image: sushi{i}.png")
sys.exit() # Exit if any image is missing
img = pygame.image.load(path).convert_alpha() # Load image with transparency
images.append(pygame.transform.scale(img, (80, 80))) # Scale to double size (80x80)
return images
class Sushi(pygame.sprite.Sprite):
"""
A sushi sprite that falls from the top of the screen to the bottom
at a random speed and resets its position once it moves off the bottom.
"""
def __init__(self, images):
super().__init__()
self.images = images
self.image = random.choice(self.images) # Random sushi image
self.rect = self.image.get_rect()
# Start at a random horizontal position within the screen width
self.rect.x = random.randint(0, SCREEN_WIDTH - self.rect.width)
# Start slightly above the visible screen (random y between -100 and -40)
self.rect.y = random.randint(-100, -40)
self.speed = random.uniform(2, 5) # Random falling speed
def update(self):
"""
Update sushi position by moving it downwards.
When it moves off the bottom, reset to a new random position at the top.
"""
self.rect.y += self.speed # Move down by speed
if self.rect.top > SCREEN_HEIGHT: # If sushi is below the screen
# Reset vertical position to top off-screen
self.rect.y = random.randint(-100, -40)
# Reset horizontal position randomly within screen width
self.rect.x = random.randint(0, SCREEN_WIDTH - self.rect.width)
self.speed = random.uniform(2, 5) # New random speed
self.image = random.choice(self.images) # Pick a new sushi image
# Create fullscreen window
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.FULLSCREEN)
pygame.display.set_caption("Sushi Screensaver")
clock = pygame.time.Clock() # Clock to control frame rate
# Load sushi images into a list
sushi_images = load_sushi_images()
# Create a group to hold multiple sushi sprites
sushi_group = pygame.sprite.Group()
for _ in range(30): # Create 30 sushi sprites
sushi_group.add(Sushi(sushi_images))
# Main loop to run the screensaver
running = True
while running:
screen.fill(BG_COLOR) # Fill screen with background color
# Handle events like quit, key presses, and mouse clicks
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False # Exit on window close
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False # Exit on ESC key press
# Other keys do nothing here, but could be handled if needed
elif event.type == pygame.MOUSEBUTTONDOWN:
running = False # Exit on any mouse click
sushi_group.update() # Update all sushi positions
sushi_group.draw(screen) # Draw all sushi sprites on the screen
pygame.display.flip() # Update the full display surface to the screen
clock.tick(FPS) # Maintain the frame rate
pygame.quit() # Clean up and close pygame
+85
View File
@@ -0,0 +1,85 @@
import pygame
import random
import os
import sys
# Initialize pygame
pygame.init()
# Get display size for fullscreen
SCREEN_WIDTH, SCREEN_HEIGHT = pygame.display.Info().current_w, pygame.display.Info().current_h
FPS = 60
# Colors
BG_COLOR = (0, 0, 0) # Black background
# Load sushi images
def load_sushi_images():
images = []
for i in range(1, 6): # sushi1.png to sushi5.png
path = os.path.join(os.path.dirname(__file__), f"sushi{i}.png")
if not os.path.exists(path):
print(f"❌ Missing image: sushi{i}.png")
sys.exit()
img = pygame.image.load(path).convert_alpha()
# Scale sushi images to 80x80 (double size)
images.append(pygame.transform.scale(img, (80, 80)))
return images
# Sushi sprite
class Sushi(pygame.sprite.Sprite):
def __init__(self, images):
super().__init__()
self.images = images
self.image = random.choice(self.images)
self.rect = self.image.get_rect()
self.rect.x = random.randint(0, SCREEN_WIDTH - self.rect.width)
self.rect.y = random.randint(-100, -40)
self.speed = random.uniform(2, 5)
def update(self):
self.rect.y += self.speed
if self.rect.top > SCREEN_HEIGHT:
self.rect.y = random.randint(-100, -40)
self.rect.x = random.randint(0, SCREEN_WIDTH - self.rect.width)
self.speed = random.uniform(2, 5)
self.image = random.choice(self.images)
# Setup fullscreen display
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.FULLSCREEN)
pygame.display.set_caption("Sushi Screensaver")
clock = pygame.time.Clock()
# Load sushi images
sushi_images = load_sushi_images()
# Create sushi sprites
sushi_group = pygame.sprite.Group()
for _ in range(30):
sushi_group.add(Sushi(sushi_images))
# Main loop
running = True
while running:
screen.fill(BG_COLOR)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
else:
# Optional: you can decide if any other key should exit or not
pass
elif event.type == pygame.MOUSEBUTTONDOWN:
running = False
sushi_group.update()
sushi_group.draw(screen)
pygame.display.flip()
clock.tick(FPS)
pygame.quit()
Binary file not shown.

After

Width:  |  Height:  |  Size: 297 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 308 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 212 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 252 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 365 KiB