Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame,sys,math
- # bary v2
- pygame.init()
- W,H=980,580;S=pygame.display.set_mode((W,H));pygame.display.set_caption("Solar Barycenter Calculator")
- F=pygame.font.SysFont("monospace",13);FB=pygame.font.SysFont("monospace",14,bold=True);FT=pygame.font.SysFont("monospace",17,bold=True)
- MS=1.989e30;RS=696000;AU=1.496e8;ME=5.972e24
- PL=[("Mercury",57.9,.0553),("Venus",108.2,.815),("Earth",149.6,1),("Mars",227.9,.107),("Jupiter",778.5,317.8),("Saturn",1432,95.2),("Uranus",2867,14.5),("Neptune",4515,17.1),("Pluto",5906,.0022),("Eris",10120,.0028),("Ceres",414,.000157)]
- PR=[("Eris",10120,.0028),("Pluto",5906,.0022),("Ceres",414,.000157)]
- SL={"d":10120.,"m":.0028};DR=None
- def bc(au,me):
- r=(au*AU/1000)*me*ME/(MS+me*ME);return r,r-RS,r<RS
- def panel(rect,col=(25,25,45),r=6): pygame.draw.rect(S,col,rect,border_radius=r);pygame.draw.rect(S,(50,50,80),rect,1,border_radius=r)
- def sldr(lbl,k,x,y,mn,mx,w=270):
- v=SL[k];pct=max(0,min(1,(math.log10(v)-math.log10(mn))/(math.log10(mx)-math.log10(mn))))
- kx=x+int(pct*w);panel(pygame.Rect(x-8,y-22,w+16,44),(20,20,38))
- pygame.draw.rect(S,(40,40,65),(x,y+5,w,5),border_radius=3);pygame.draw.rect(S,(80,170,255),(x,y+5,kx-x,5),border_radius=3)
- pygame.draw.circle(S,(30,30,50),(kx,y+7),9);pygame.draw.circle(S,(80,170,255),(kx,y+7),7);pygame.draw.circle(S,(180,220,255),(kx-2,y+5),2)
- S.blit(F.render(f"{lbl}: {v:.4g}",1,(170,190,220)),(x,y-18));return pygame.Rect(x-4,y+1,w+8,14)
- BG=(10,10,20);CI=(60,220,100);CO=(255,70,70);CH=(80,180,255);CK=(140,100,255);CY=(255,230,60)
- COLS=[("Body",120),("Dist AU",80),("Mass/ME",88),("Bary (km)",105),("Diff (km)",110),("Inside?",72)]
- TW=sum(w for _,w in COLS);SR={};clock=pygame.time.Clock()
- def stars():
- t=pygame.time.get_ticks()
- for i in range(45):b=80+(i*37+t//80)%120;pygame.draw.circle(S,(b,b,b+20),((i*173+11)%W,(i*97+7)%110),1 if i%3 else 0)
- while True:
- S.fill(BG);stars();mx,my=pygame.mouse.get_pos()
- for e in pygame.event.get():
- if e.type==pygame.QUIT:sys.exit()
- if e.type==pygame.MOUSEBUTTONUP:DR=None
- if e.type==pygame.MOUSEBUTTONDOWN:
- if SR.get("d") and SR["d"].collidepoint(e.pos):DR="d"
- if SR.get("m") and SR["m"].collidepoint(e.pos):DR="m"
- for i,(n,a,m) in enumerate(PR):
- if pygame.Rect(W-165,130+i*32,150,26).collidepoint(e.pos):SL["d"]=a;SL["m"]=m
- if e.type==pygame.MOUSEMOTION and DR:
- for k,x,mn,mx2,w in [("d",30,.1,15000,270),("m",340,1e-6,400,270)]:
- if DR==k:SL[k]=mn*(mx2/mn)**max(0,min(1,(mx-x)/w))
- S.blit(FT.render("SOLAR BARYCENTER CALCULATOR",1,CH),(30,12))
- S.blit(F.render(f"Sun radius: {RS:,} km | Inside = barycenter within solar surface",1,(80,80,110)),(30,34))
- SR["d"]=sldr("Distance (AU)","d",30,75,.1,15000);SR["m"]=sldr("Mass (× Earth)","m",340,75,1e-6,400)
- panel(pygame.Rect(W-170,108,160,120),(18,18,36));S.blit(F.render("PRESETS",1,(100,100,150)),(W-160,113))
- for i,(n,a,m) in enumerate(PR):
- br=pygame.Rect(W-162,130+i*32,144,24);hot=br.collidepoint(mx,my)
- panel(br,(45,35,80) if hot else (30,25,55));S.blit(FB.render(n,1,CK if hot else (150,130,220)),(W-155,134+i*32))
- hx=18;hy=140;panel(pygame.Rect(hx-2,hy,TW+4,22),(20,30,65))
- for h,w in COLS: S.blit(FB.render(h,1,CH),(hx+3,hy+4));hx+=w
- for i,(nm,au,me) in enumerate(PL+[("Custom",SL["d"],SL["m"])]):
- r,df,ins=bc(au,me);ry=164+i*20;rx=18;custom=nm=="Custom"
- pygame.draw.rect(S,(30,22,55) if custom else((24,24,42) if i%2 else(18,18,32)),(rx-2,ry,TW+4,19))
- if custom:pygame.draw.rect(S,(70,50,120),(rx-2,ry,TW+4,19),1)
- vs=[nm,f"{au:.1f}",f"{me:.5g}",f"{r:,.0f}",f"{abs(df):,.0f}","YES" if ins else "NO"]
- for j,((h,w),v) in enumerate(zip(COLS,vs)):
- tc=CI if(j==5 and ins) else CO if j==5 else CY if custom else(200,200,210)
- S.blit(F.render(v,1,tc),(rx+2,ry+3));rx+=w
- pygame.draw.line(S,(40,40,70),(18,H-28),(W-18,H-28))
- S.blit(F.render("Green=inside Sun Red=outside Yellow=custom Sliders are log-scale",1,(65,65,95)),(18,H-20))
- pygame.display.flip();clock.tick(60)
Advertisement
Add Comment
Please, Sign In to add comment