import tkinter as tk, math, random

# --- Settings
W, H = 1000, 700
FPS = 30
root = tk.Tk()
root.title("Solar System Demoscene")
c = tk.Canvas(root, width=W, height=H, bg="black")
c.pack()

# --- Stars
stars = [(random.randint(0,W), random.randint(0,H),
          random.randint(150,255)) for _ in range(200)]

# --- Planets (distance, size, speed, color)
planets = [
    (70,  8, 0.05, "gray"),      # Mercury
    (110, 12, 0.04, "orange"),   # Venus
    (160, 13, 0.03, "blue"),     # Earth
    (220, 10, 0.025, "red"),     # Mars
    (320, 20, 0.015, "brown"),   # Jupiter
    (420, 17, 0.012, "gold"),    # Saturn
    (520, 14, 0.010, "cyan"),    # Uranus
    (600, 13, 0.008, "blue")     # Neptune
]
angles = [random.random()*360 for _ in planets]

# --- Comets
comets = []
for _ in range(5):
    comets.append({
        'x': random.randint(-200, 0),
        'y': random.randint(0, H//2),
        'dx': random.uniform(4,6),
        'dy': random.uniform(1,3),
        'trail': []
    })

# --- NLOs
nlos = []
for _ in range(3):
    nlos.append({'x':random.randint(200,800),
                 'y':random.randint(100,400),
                 'dx':random.choice([-2,2]),
                 'dy':random.choice([-1,1])})

# --- Animation
def animate():
    c.delete("all")

    # stars
    for x,y,b in stars:
        col = f'#{b:02x}{b:02x}{b:02x}'
        c.create_rectangle(x,y,x+1,y+1,fill=col,outline=col)

    # Sun
    c.create_oval(W//2-40,H//2-40,W//2+40,H//2+40,fill="yellow",outline="")

    # planets
    for i,(r,size,speed,col) in enumerate(planets):
        angles[i]+=speed
        rad=math.radians(angles[i])
        x=W//2+r*math.cos(rad)
        y=H//2+r*math.sin(rad)
        c.create_oval(x-size,y-size,x+size,y+size,fill=col,outline="")
        # orbit path
        c.create_oval(W//2-r,H//2-r,W//2+r,H//2+r,outline="#222")

    # comets
    for cm in comets:
        cm['x']+=cm['dx']; cm['y']+=cm['dy']
        cm['trail'].insert(0,(cm['x'],cm['y']))
        if len(cm['trail'])>20: cm['trail'].pop()
        for i,(tx,ty) in enumerate(cm['trail']):
            size=int(6*(1-i/20))
            col=f'#{255:02x}{215:02x}{max(0,50-i*10):02x}'
            c.create_oval(tx-size,ty-size,tx+size,ty+size,fill=col,outline="")
        if cm['x']>W+100 or cm['y']>H+100:
            cm['x']=-200;cm['y']=random.randint(0,H//2);cm['trail']=[]

    # NLOs
    for u in nlos:
        u['x']+=u['dx'];u['y']+=u['dy']
        c.create_oval(u['x']-20,u['y']-10,u['x']+20,u['y']+10,outline="#39ff14")
        c.create_oval(u['x']-10,u['y']-5,u['x']+10,u['y']+5,fill="#39ff14")
        if u['x']<50 or u['x']>W-50: u['dx']*=-1
        if u['y']<50 or u['y']>H-50: u['dy']*=-1

    root.after(int(1000/FPS), animate)

root.after(50, animate)
root.mainloop()
