Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame,sys
- # bary v1
- pygame.init()
- W,H=900,500;S=pygame.display.set_mode((W,H));pygame.display.set_caption("Barycenter")
- F=pygame.font.SysFont("monospace",13);FB=pygame.font.SysFont("monospace",14,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): sep=au*AU/1000;m2=me*ME;r=sep*m2/(MS+m2);return r,RS-r,RS>r
- def sl(lbl,k,x,y,mn,mx,w=250):
- v=SL[k];px=x+int((v-mn)/(mx-mn)*w)
- pygame.draw.rect(S,(50,50,70),(x,y+7,w,4));pygame.draw.circle(S,(100,190,255),(px,y+9),7)
- S.blit(F.render(f"{lbl}: {v:.4g}",1,(190,190,210)),(x,y-16));return pygame.Rect(x,y+2,w,14)
- BG=(12,12,22);CI=(70,210,110);CO=(240,80,80);CH=(70,170,255)
- cols=[("Body",115),("AU",75),("Mass/ME",80),("Bary km",100),("Diff km",105),("Inside",70)]
- SR={};clock=pygame.time.Clock()
- while True:
- S.fill(BG);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.items()):
- if pygame.Rect(W-155,75+i*26,140,20).collidepoint(e.pos):SL["d"]=a;SL["m"]=m
- if e.type==pygame.MOUSEMOTION and DR:
- cfg=[("d",80,0.1,15000,250),("m",390,1e-6,400,250)]
- for k,x,mn,mx2,w in cfg:
- if DR==k:SL[k]=mn+max(0,min(1,(mx-x)/w))*(mx2-mn)
- SR["d"]=sl("Dist AU","d",80,45,.1,15000);SR["m"]=sl("Mass x ME","m",390,45,1e-6,400)
- for i,(n,(a,m)) in enumerate(PR.items()):
- br=pygame.Rect(W-155,75+i*26,140,20);pygame.draw.rect(S,(35,35,55),br,border_radius=3)
- S.blit(F.render(n,1,(170,170,210)),(W-150,78+i*26))
- cx=20;cy=100
- for h,w in cols:
- pygame.draw.rect(S,(25,35,65),(cx,cy,w,20));S.blit(FB.render(h,1,CH),(cx+2,cy+3));cx+=w
- for i,(nm,au,me) in enumerate(PL+[("Custom",SL["d"],SL["m"])]):
- r,df,ins=bc(au,me);cy2=122+i*20;cx2=20;custom=nm=="Custom"
- 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)):
- pygame.draw.rect(S,(45,45,75) if custom else (22+i%2*8,22+i%2*8,35+i%2*8),(cx2,cy2,w,18))
- tc=(255,240,80) if custom else (CI if(ins and j==5)else(CO if j==5 else(190,190,190)))
- S.blit(F.render(v,1,tc),(cx2+2,cy2+2));cx2+=w
- pygame.display.flip();clock.tick(60)
Advertisement
Add Comment
Please, Sign In to add comment