import pygame import asyncio import platform import random import numpy as np # Initialize Pygame pygame.init() # Screen dimensions WIDTH, HEIGHT = 1200, 800 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Advanced Nuclear Reactor Simulation") # Colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) GREEN = (0, 255, 0) RED = (255, 0, 0) BLUE = (0, 0, 255) GRAY = (100, 100, 100) YELLOW = (255, 255, 0) ORANGE = (255, 165, 0) PURPLE = (128, 0, 128) # Simulation parameters max_temp = 1000 # Overheat threshold neutron_base = 15 # Base neutron particles max_history = 200 # Points in history graph credits_per_mw = 0.1 # Credits earned per MW per frame radiation_threshold = 100 # Radiation alarm level maintenance_cost = 500 # Credits for maintenance maintenance_duration = 300 # Frames max_coolant = 1.0 # Maximum coolant flow (0 to 1) # Reactor types reactor_types = { "PWR": {"fuel_heat": 15, "coolant_base": 6, "control_rod_effect": 0.05, "radiation_rate": 0.1}, "BWR": {"fuel_heat": 18, "coolant_base": 4, "control_rod_effect": 0.04, "radiation_rate": 0.15} } # Game state temperature = 300 power_output = 0 control_rod_position = 0.5 # 0 (fully inserted) to 1 (fully withdrawn) coolant_flow = 0.5 # 0 to 1 neutron_flux = 0 radiation_level = 0 reactor_state = "Normal" # Normal, Warning, Critical, Shutdown reactor_type = "PWR" running = True event_timer = 0 event_message = "" history_temp = [] history_power = [] scram_active = False credits = 1000 upgrades = {"coolant": 1, "rods": 1, "backup": 0} operator_log = [] weather = "Clear" # Clear, Storm weather_timer = 600 # Change every 10 seconds maintenance_mode = False maintenance_timer = 0 show_guide = False guide_page = 0 achievements = set() # Font font = pygame.font.SysFont("arial", 24) small_font = pygame.font.SysFont("arial", 18) # Sound sample_rate = 44100 def make_sound(freq, duration, volume=0.5): t = np.linspace(0, duration, int(sample_rate * duration), False) wave = volume * np.sin(2 * np.pi * freq * t) stereo = np.vstack((wave, wave)).T sound = pygame.sndarray.make_sound((32767 * stereo).astype(np.int16)) return sound warning_beep = make_sound(1000, 0.2) critical_beep = make_sound(1500, 0.1) reactor_hum = make_sound(200, 0.5, 0.3) alarm_beep = make_sound(1200, 0.3) achievement_sound = make_sound(800, 0.4) # Neutron particles particles = [] # Guidebook content guidebook = [ {"title": "Welcome to the Reactor", "text": "This simulation lets you manage a nuclear reactor. Your goal is to maintain stable power output while preventing overheating or radiation leaks. Use the control panel to adjust settings."}, {"title": "Components", "text": "Fuel Rods (Green): Generate heat via fission.\nControl Rods (Gray): Absorb neutrons to slow the reaction.\nCoolant (Blue): Removes heat to prevent overheating."}, {"title": "Controls", "text": "UP/DOWN: Adjust control rods.\nLEFT/RIGHT: Adjust coolant flow.\nS: Emergency SCRAM.\nM: Start maintenance.\nG: Open this guide.\nT: Switch reactor type."}, {"title": "Upgrades", "text": "Earn credits by generating power. Spend credits on:\n- Coolant System: Improves heat removal.\n- Control Rods: Better reaction control.\n- Backup Generator: Stabilizes power during failures."}, {"title": "Safety", "text": "Monitor temperature, radiation, and reactor state. Critical states require immediate action. Use SCRAM (S) to shut down if needed. Schedule maintenance (M) to repair wear."} ] def setup(): global clock clock = pygame.time.Clock() reactor_hum.play(-1) log_action("Simulation started") def log_action(message): operator_log.append(f"[{len(operator_log)}] {message}") if len(operator_log) > 10: operator_log.pop(0) def spawn_neutron(): x = random.randint(150, 400) y = random.randint(200, 400) vx = random.uniform(-2, 2) vy = random.uniform(-2, 2) return {"x": x, "y": y, "vx": vx, "vy": vy, "life": 60} def update_particles(): global particles particle_count = int(neutron_base * control_rod_position * upgrades["rods"]) if len(particles) < particle_count and random.random() < 0.2: particles.append(spawn_neutron()) new_particles = [] for p in particles: p["x"] += p["vx"] p["y"] += p["vy"] p["life"] -= 1 if p["life"] > 0 and 150 < p["x"] < 400 and 200 < p["y"] < 400: new_particles.append(p) particles = new_particles def random_event(): global temperature, coolant_flow, event_message, event_timer, radiation_level if random.random() < 0.015 and not maintenance_mode: event = random.choice(["leak", "surge", "pump_failure", "turbine_trip", "radiation_leak"]) event_message = { "leak": "Coolant Leak Detected!", "surge": "Power Surge Detected!", "pump_failure": "Coolant Pump Failure!", "turbine_trip": "Turbine Trip! Power Demand Spiked!", "radiation_leak": "Radiation Leak Detected!" }[event] if event == "leak": coolant_flow = max(0, coolant_flow - 0.3) elif event == "surge": temperature += 150 elif event == "pump_failure": coolant_flow = 0 elif event == "turbine_trip": power_output *= 0.5 elif event == "radiation_leak": radiation_level += 50 event_timer = 300 log_action(event_message) def update_weather(): global weather, weather_timer, coolant_flow weather_timer -= 1 if weather_timer <= 0: weather = random.choice(["Clear", "Storm"]) weather_timer = 600 log_action(f"Weather changed to {weather}") if weather == "Storm": coolant_flow = max(0, coolant_flow - 0.1) def check_achievements(): if power_output > 500 and "HighPower" not in achievements: achievements.add("HighPower") achievement_sound.play() log_action("Achievement: High Power Output!") if len(operator_log) > 10 and reactor_state != "Shutdown" and "Stable" not in achievements: achievements.add("Stable") achievement_sound.play() log_action("Achievement: Stable Operation!") if guide_page == len(guidebook) - 1 and "Educated" not in achievements: achievements.add("Educated") achievement_sound.play() log_action("Achievement: Completed Tutorial!") def draw_reactor(): bg_color = BLACK if reactor_state == "Warning": bg_color = ORANGE elif reactor_state == "Critical": bg_color = RED elif reactor_state == "Shutdown": bg_color = GRAY screen.fill(bg_color) # Draw fuel rods for i in range(5): pygame.draw.rect(screen, GREEN, (150 + i * 50, 200, 40, 200)) # Draw control rods control_height = 200 * (1 - control_rod_position) for i in range(5): pygame.draw.rect(screen, GRAY, (150 + i * 50, 200, 40, control_height)) # Draw coolant coolant_alpha = int(255 * coolant_flow) coolant_surface = pygame.Surface((250, 200), pygame.SRCALPHA) coolant_surface.fill((0, 0, 255, coolant_alpha)) screen.blit(coolant_surface, (150, 200)) # Draw neutrons for p in particles: pygame.draw.circle(screen, YELLOW, (int(p["x"]), int(p["y"])), 3) #