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.
Okay, let's create a Python script using Pygame to simulate the visual appearance of Gargantua, focusing on the design elements like the accretion disk and lensing effect. This will be a simplified visual representation, not a physically accurate simulation.
Core Design Ideas:
Black Hole: A perfect black circle at the center.
Accretion Disk:
A bright, thin, yellowish-orange disk viewed somewhat edge-on (an ellipse).
Brighter towards the center.
Key Feature: Gravitational lensing bends the light from the back of the disk over and under the black hole, creating bright arcs above and below the main disk plane.
Gravitational Lensing (Stars): Stars near the black hole appear distorted or duplicated (simplified here by just distorting their positions radially).
Background: A starfield.
Animation: Subtle rotation/shimmering of the disk.
Functionality:
Display the black hole, accretion disk (including lensed parts), and background stars.
Apply a basic distortion effect to stars near the black hole.
Allow zooming and panning.
Subtle animation for the disk.
Controls:
Arrow Keys: Pan the view (Up, Down, Left, Right).
'+'/'-' Keys (or PageUp/PageDown): Zoom In / Zoom Out.
'R' Key: Reset view (zoom and pan).
ESC Key: Quit.
python
# gargantua_simulator.py
import pygame
import random
import math
# --- Constants ---
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 800
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
YELLOW = (255, 255, 0)
ORANGE = (255, 165, 0)
DARK_ORANGE = (200, 100, 0)
NEAR_WHITE_YELLOW = (255, 255, 200)
# Simulation Parameters
BH_RADIUS_BASE = 50 # Base radius of the black hole event horizon
DISK_OUTER_RADIUS_BASE = 350 # Base outer radius of the accretion disk
DISK_THICKNESS_BASE = 35 # Base visual thickness of the disk edge
LENSING_RADIUS_FACTOR = 2.5 # How far out the noticeable lensing distortion goes (times BH_RADIUS)
STAR_COUNT = 500
DISK_ROTATION_SPEED = 0.0005 # Radians per frame
# --- Initialization ---
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Gargantua Visual Simulator")
clock = pygame.time.Clock()
random.seed() # Initialize random number generator
# --- Simulation State Variables ---
zoom_level = 1.0
min_zoom = 0.2
max_zoom = 5.0
pan_offset_x = 0.0
pan_offset_y = 0.0
disk_angle = 0.0 # For subtle rotation animation
# --- Helper Functions ---
def generate_stars(count):
"""Generates random star positions and brightness within a large area."""
stars = []
# Generate stars in a much larger area than the screen to allow panning
area_scale = 3
for _ in range(count):
# Distribute more stars further away for a pseudo-3D feel
dist_factor = random.uniform(0.1, 1.0) ** 0.5 # Power makes distribution denser further out
angle = random.uniform(0, 2 * math.pi)
dist = dist_factor * max(SCREEN_WIDTH, SCREEN_HEIGHT) * area_scale
x = dist * math.cos(angle)
y = dist * math.sin(angle)
# Brightness based on distance (closer appears brighter is faked here)
brightness = random.uniform(50, 200) * (1.0 - dist_factor * 0.5) # Dimmer further out
size = random.randint(1, 3)
stars.append({
'x': x,
'y': y,
'brightness': int(brightness),
'size': size
})
return stars
def world_to_screen(x, y, center_x, center_y, current_zoom):
"""Converts world coordinates to screen coordinates based on pan and zoom."""
screen_x = center_x + (x + pan_offset_x) * current_zoom
screen_y = center_y + (y + pan_offset_y) * current_zoom
return int(screen_x), int(screen_y)
def apply_lensing_distortion(star_x, star_y, bh_center_x, bh_center_y, bh_radius_screen, lensing_radius_screen):
"""Applies a simple radial distortion to star positions near the black hole."""
dx = star_x - bh_center_x
dy = star_y - bh_center_y
dist_sq = dx*dx + dy*dy
if dist_sq < 1e-6 or dist_sq > lensing_radius_screen**2:
return star_x, star_y # No distortion if too far or exactly at center
dist = math.sqrt(dist_sq)
# Simple distortion: push stars radially outwards.
# The closer the star is to the BH radius, the stronger the push.
# Avoid division by zero or extreme push inside the horizon visually
effective_dist = max(dist, bh_radius_screen * 0.8) # Don't lens things *inside* visually
# More distortion closer to the BH radius
push_factor = 1.0 + (bh_radius_screen**2 / effective_dist**2) * 1.5 # Adjust '1.5' for strength
# Cap the push factor to prevent stars flying too far
push_factor = min(push_factor, 3.0)
new_x = bh_center_x + dx / dist * effective_dist * push_factor
new_y = bh_center_y + dy / dist * effective_dist * push_factor
return new_x, new_y
# --- Generate Stars ---
stars = generate_stars(STAR_COUNT)
# --- Main Loop ---
running = True
while running:
# --- Event Handling ---
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
elif event.key == pygame.K_EQUALS or event.key == pygame.K_PLUS or event.key == pygame.K_PAGEUP:
zoom_level = min(max_zoom, zoom_level * 1.2)
elif event.key == pygame.K_MINUS or event.key == pygame.K_UNDERSCORE or event.key == pygame.K_PAGEDOWN:
zoom_level = max(min_zoom, zoom_level / 1.2)
elif event.key == pygame.K_LEFT:
pan_offset_x += 50 / zoom_level
elif event.key == pygame.K_RIGHT:
pan_offset_x -= 50 / zoom_level
elif event.key == pygame.K_UP:
pan_offset_y += 50 / zoom_level
elif event.key == pygame.K_DOWN:
pan_offset_y -= 50 / zoom_level
elif event.key == pygame.K_r: # Reset view
zoom_level = 1.0
pan_offset_x = 0.0
pan_offset_y = 0.0
disk_angle = 0.0
# --- Update State ---
disk_angle += DISK_ROTATION_SPEED
center_x = SCREEN_WIDTH // 2
center_y = SCREEN_HEIGHT // 2
# Calculate screen-space dimensions based on zoom
bh_radius_screen = BH_RADIUS_BASE * zoom_level
disk_outer_radius_screen = DISK_OUTER_RADIUS_BASE * zoom_level
disk_thickness_screen = DISK_THICKNESS_BASE * zoom_level
lensing_radius_screen = bh_radius_screen * LENSING_RADIUS_FACTOR
# --- Drawing ---
screen.fill(BLACK)
# 1. Draw Stars (applying pan, zoom, and lensing)
for star in stars:
# Get initial screen position (applying pan and zoom)
sx, sy = world_to_screen(star['x'], star['y'], center_x, center_y, zoom_level)
# Apply lensing distortion if star is near the black hole on screen
sx_lensed, sy_lensed = apply_lensing_distortion(
sx, sy,
center_x + pan_offset_x * zoom_level, # BH screen center might shift due to pan
center_y + pan_offset_y * zoom_level,
bh_radius_screen,
lensing_radius_screen
)
# Draw star only if it's within screen bounds (with some margin)
margin = 20
if -margin < sx_lensed < SCREEN_WIDTH + margin and -margin < sy_lensed < SCREEN_HEIGHT + margin:
brightness = star['brightness']
color = (min(255, brightness), min(255, brightness), min(255, brightness * 0.8)) # Slightly yellowish
size = max(1, int(star['size'] * math.sqrt(zoom_level))) # Make stars slightly bigger when zoomed, but not linearly
# Simple square for stars, circles are slower
pygame.draw.rect(screen, color, (int(sx_lensed - size/2), int(sy_lensed - size/2), size, size))
# Use circle for slightly nicer look (optional, might be slower)
# pygame.draw.circle(screen, color, (int(sx_lensed), int(sy_lensed)), size // 2 if size > 1 else 1)
# 2. Draw Accretion Disk (Main Plane - Ellipse)
# We draw it as multiple ellipses for a faux brightness gradient
num_disk_layers = 5
for i in range(num_disk_layers):
lerp = (i + 1) / num_disk_layers
# Interpolate color from near-white-yellow (inner) to orange (outer)
r = int(NEAR_WHITE_YELLOW[0] * (1 - lerp) + ORANGE[0] * lerp)
g = int(NEAR_WHITE_YELLOW[1] * (1 - lerp) + ORANGE[1] * lerp)
b = int(NEAR_WHITE_YELLOW[2] * (1 - lerp) + ORANGE[2] * lerp)
color = (r, g, b)
# Interpolate radius
current_outer_radius = bh_radius_screen + (disk_outer_radius_screen - bh_radius_screen) * (1 - i / num_disk_layers)
current_inner_radius = bh_radius_screen * 0.9 # Ensure overlap with BH
width = int(current_outer_radius * 2)
height = int(disk_thickness_screen * (current_outer_radius / disk_outer_radius_screen)**0.7) # Thinner further out
if width < 2 or height < 2: continue # Don't draw tiny ellipses
disk_rect = pygame.Rect(0, 0, width, height)
disk_rect.center = world_to_screen(0, 0, center_x, center_y, 1.0) # Disk centered at origin (before pan/zoom applied implicitly by center)
# Subtle shimmer/rotation effect by slightly offsetting layers or changing intensity - simple version: offset angle
angle_offset = math.sin(disk_angle * (i + 1) * 5) * 0.02 # Small angular wobble
# We need to draw the ellipse using polygons or arcs to clip against the BH later
# Pygame's ellipse drawing doesn't easily allow clipping or drawing parts.
# Let's *approximate* with arcs for the lensed parts instead.
# For the main plane, draw a simple ellipse for now. Clipping is hard.
pygame.draw.ellipse(screen, color, disk_rect)
# 3. Draw Lensed Disk Arcs (Above and Below) - Simplified
# These represent the back of the disk warped over/under the black hole
arc_thickness = int(disk_thickness_screen * 0.8) # Lensed part might appear thinner/thicker
arc_radius_outer = int(bh_radius_screen * 1.6) # How far out the lensed arc stretches vertically
arc_radius_inner = int(bh_radius_screen * 1.05) # Start just outside BH
if arc_radius_outer > arc_radius_inner and arc_thickness > 1:
# Define the bounding box for the arcs
# Arc horizontal extent should be related to the main disk, but wider near BH
arc_width = int(bh_radius_screen * 3.5 + abs(math.sin(disk_angle*2)) * bh_radius_screen * 0.5) # Pulsating width slightly
arc_height = int(arc_radius_outer * 2)
# Calculate center position on screen including pan/zoom
bh_screen_center_x, bh_screen_center_y = world_to_screen(0, 0, center_x, center_y, 1.0)
# --- Upper Arc ---
# Create a surface for the upper arc gradient
upper_arc_surf = pygame.Surface((arc_width, arc_height // 2), pygame.SRCALPHA)
upper_arc_rect = pygame.Rect(0, 0, arc_width, arc_radius_outer - arc_radius_inner)
# Draw multiple arcs for gradient
for i in range(num_disk_layers):
lerp = i / (num_disk_layers -1) if num_disk_layers > 1 else 1.0
r = int(NEAR_WHITE_YELLOW[0] * (1 - lerp) + DARK_ORANGE[0] * lerp)
g = int(NEAR_WHITE_YELLOW[1] * (1 - lerp) + DARK_ORANGE[1] * lerp)
b = int(NEAR_WHITE_YELLOW[2] * (1 - lerp) + DARK_ORANGE[2] * lerp)
color = (r, g, b, 230) # Slightly transparent
# Define arc bounding box - width slightly smaller further from center
width_i = int(arc_width * (1 - 0.1 * lerp))
rect_i = pygame.Rect( (arc_width - width_i) // 2, 0, width_i, arc_height)
try:
# Arcs can fail if rect is too small or angles identical
pygame.draw.arc(upper_arc_surf, color, rect_i, math.radians(20), math.radians(160), arc_thickness // num_disk_layers)
except ValueError: pass # Ignore drawing error for tiny arcs
# Blit the upper arc surface above the black hole center
screen.blit(upper_arc_surf, (bh_screen_center_x - arc_width // 2, bh_screen_center_y - arc_radius_outer))
# --- Lower Arc --- (Similar to upper, but flipped)
lower_arc_surf = pygame.Surface((arc_width, arc_height // 2), pygame.SRCALPHA)
lower_arc_rect = pygame.Rect(0, 0, arc_width, arc_radius_outer - arc_radius_inner)
# Draw multiple arcs for gradient
for i in range(num_disk_layers):
lerp = i / (num_disk_layers -1) if num_disk_layers > 1 else 1.0
r = int(NEAR_WHITE_YELLOW[0] * (1 - lerp) + DARK_ORANGE[0] * lerp)
g = int(NEAR_WHITE_YELLOW[1] * (1 - lerp) + DARK_ORANGE[1] * lerp)
b = int(NEAR_WHITE_YELLOW[2] * (1 - lerp) + DARK_ORANGE[2] * lerp)
color = (r, g, b, 230) # Slightly transparent
# Define arc bounding box - width slightly smaller further from center
width_i = int(arc_width * (1 - 0.1 * lerp))
rect_i = pygame.Rect( (arc_width - width_i) // 2, -(arc_radius_outer - arc_radius_inner), width_i, arc_height) # Offset y for lower arc
try:
# Angles flipped for lower arc
pygame.draw.arc(lower_arc_surf, color, rect_i, math.radians(200), math.radians(340), arc_thickness // num_disk_layers)
except ValueError: pass # Ignore drawing error
# Blit the lower arc surface below the black hole center
screen.blit(lower_arc_surf, (bh_screen_center_x - arc_width // 2, bh_screen_center_y + arc_radius_inner - (arc_radius_outer - arc_radius_inner) )) # Adjust y
# 4. Draw Black Hole Event Horizon (on top of everything else at the center)
bh_screen_center_x, bh_screen_center_y = world_to_screen(0, 0, center_x, center_y, 1.0)
if bh_radius_screen >= 1:
pygame.draw.circle(screen, BLACK, (bh_screen_center_x, bh_screen_center_y), int(bh_radius_screen))
# --- Display Update ---
pygame.display.flip()
# --- Frame Limiting ---
clock.tick(60) # Limit to 60 FPS
# --- Cleanup ---
pygame.quit()
Explanation:
Setup: Imports Pygame, math, random. Defines constants for colors, screen size, and simulation parameters (radii, star count, etc.). Initializes Pygame and sets up the display window.
State Variables:zoom_level, pan_offset_x, pan_offset_y track the user's view. disk_angle is used for simple animation.
generate_stars: Creates a list of star dictionaries. Stars are generated in a large area around the origin (0,0) so panning reveals new stars. Brightness and size are randomized.
world_to_screen: A crucial function that takes a world coordinate (relative to the black hole at (0,0)) and converts it to the actual pixel coordinate on the screen, applying the current pan_offset and zoom_level.
apply_lensing_distortion: This function simulates lensing. It takes a star's screen position. If the star is close enough to the black hole's screen position, it calculates a new position pushed radially outwards. The closer to the black hole radius, the stronger the push. This is a visual cheat, not real physics.
Main Loop:
Event Handling: Checks for QUIT, Escape key, Arrow keys (updates pan offset), +/-/PageUp/PageDown keys (updates zoom level, clamped between min_zoom and max_zoom), and 'R' key (resets view). Note that panning speed is adjusted by zoom level so it feels somewhat consistent.
Update State: Increments disk_angle. Calculates screen-space dimensions (like the black hole radius) based on the current zoom_level.
Drawing:
Clears the screen (screen.fill(BLACK)).
Stars: Iterates through the star list. Converts world coordinates to initial screen coordinates using world_to_screen. Applies the apply_lensing_distortion function to get the final lensed screen position. Draws the star if it's within the screen bounds. Star size is slightly affected by zoom.
Accretion Disk (Main Plane): Draws multiple filled ellipses with slightly varying radii and colors (from bright yellow/white inside to orange outside) to create a basic gradient effect. The disk_rect.center uses world_to_screen to place it correctly according to pan/zoom.
Lensed Arcs: This is the trickiest part visually. It calculates bounding boxes and uses to draw arcs above and below the black hole. Multiple arcs are drawn with varying colors for a gradient, similar to the main disk. These are drawn onto separate transparent surfaces () first, then blitted onto the main screen. This allows for better positioning and alpha blending. A slight width pulsation is added using .
How to Run:
Make sure you have Python installed.
Install Pygame: pip install pygame
Save the code as a .py file (e.g., gargantua_simulator.py).
Run it from your terminal: python gargantua_simulator.py
Enjoy your somewhat simulated view of Gargantua! Remember to use the controls to explore.
pygame.draw.arc
pygame.SRCALPHA
disk_angle
Note: Pygame's arc drawing can be finicky and might look jagged.
Black Hole: Draws a simple black circle using the calculated bh_radius_screen at the world_to_screen converted center position. This is drawn last so it correctly obscures parts of the disk and arcs behind it.
Display Update:pygame.display.flip() shows the drawn frame.
Frame Limiting:clock.tick(60) pauses briefly to aim for 60 FPS.
Cleanup:pygame.quit() uninitializes Pygame when the loop ends.