Make a .py script that somewhat simulates Gargantua blackhole. Main thing - design. Second - functionality. Third - controls (how can I interact with it)
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Creating a Python script that simulates the iconic Gargantua black hole (as seen in Interstellar) is a fun and visually stunning project. We’ll focus on design realism, have acceptable visual simulation of gravitational lensing, and provide interactive controls.
🕳️ Project Objective
Build a Python program that uses the pygame and numpy libraries to visualize a rotating black hole with accretion disk and gravitational lensing, inspired by Gargantua. Although full general relativity is beyond simple simulation, we’ll fake visuals with convincing ray distortion techniques.
📦 Dependencies
Install required Python packages:
bash
pip install pygame numpy
🧠 Features Breakdown
Feature
Details
Design
Simulate light distortion by warping background image via a custom shader
Functionality
Show accretion disk, gravitational lensing, rotating animation
Controls
Keyboard controls to change camera position, zoom, rotation
🔽 Full Code
Save this as gargantua_simulator.py:
python
import pygame
import numpy as np
import sys
import math
# Constants
WIDTH, HEIGHT = 800, 800
CENTER = np.array([WIDTH // 2, HEIGHT // 2])
FPS = 60
# Black Hole Parameters
EVENT_HORIZON_RADIUS = 80
LENS_RADIUS = 200 # Region of distortion
ROTATION_SPEED = 0.02 # Rotation of accretion disk
ZOOM_STEP = 10
class GargantuaSimulator:
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Gargantua Black Hole Simulation")
self.clock = pygame.time.Clock()
# Star field background
self.background = pygame.image.load("stars.jpg").convert()
self.background = pygame.transform.scale(self.background, (WIDTH, HEIGHT))
# Parameters
self.time = 0
self.zoom = 1.0
self.angle = 0.0 # rotation angle of disk
def draw_event_horizon(self):
pygame.draw.circle(self.screen, (0, 0, 0), CENTER, int(EVENT_HORIZON_RADIUS * self.zoom))
def draw_accretion_disk(self):
for r in range(int(EVENT_HORIZON_RADIUS * self.zoom), int(LENS_RADIUS * self.zoom), 2):
intensity = 255 - int((r / (LENS_RADIUS * self.zoom)) * 255)
color = (intensity, intensity // 2, 0)
angle_offset = math.sin(self.time + r * ROTATION_SPEED) * 20
pygame.draw.arc(
self.screen,
color,
[
CENTER[0] - r,
CENTER[1] - r,
r * 2,
r * 2
],
math.radians(90 + angle_offset),
math.radians(270 + angle_offset),
2
)
def apply_gravitational_lens(self):
lens_surface = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
for y in range(HEIGHT):
for x in range(WIDTH):
dx, dy = x - CENTER[0], y - CENTER[1]
distance = np.sqrt(dx**2 + dy**2)
if distance < LENS_RADIUS * self.zoom and distance > EVENT_HORIZON_RADIUS * self.zoom:
distortion_factor = 0.05 / (distance / (LENS_RADIUS * self.zoom))
sx = int(x + dx * distortion_factor)
sy = int(y + dy * distortion_factor)
if 0 <= sx < WIDTH and 0 <= sy < HEIGHT:
lens_surface.set_at((x, y), self.background.get_at((sx, sy)))
self.screen.blit(lens_surface, (0, 0))
def run(self):
running = True
while running:
self.screen.blit(self.background, (0, 0))
self.handle_events()
self.apply_gravitational_lens()
self.draw_accretion_disk()
self.draw_event_horizon()
pygame.display.flip()
self.time += 0.05
self.clock.tick(FPS)
def handle_events(self):
global LENS_RADIUS
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
self.zoom += 0.05
elif event.key == pygame.K_DOWN:
self.zoom -= 0.05
elif event.key == pygame.K_LEFT:
self.angle -= 0.1
elif event.key == pygame.K_RIGHT:
self.angle += 0.1
elif event.key == pygame.K_q:
pygame.quit()
sys.exit()
🌌 Background Image
You’ll need a background image named stars.jpg, preferably a star field (you can download free ones from NASA sources or any public domain site). Save it in the same directory.