solar.py

Created by andreanx

Created on November 09, 2020

8.32 KB


# Solar v.1.01 NW 23/10/2020
# https://nsi.xyz/solar
# par Jil Saint-Martin et Emmy Vadon

def get_infos():
  fnop = lambda : None
  screen_w, screen_h, color_bits, font_w, font_h, poly_has_color, poly_set_pixel, poly_show, poly_fill_rect, poly_fill_circle, poly_draw_ellipse, poly_sleep, poly_get_key, poly_draw_string, poly_pause, poly_set_buffer, poly_sleep, show_need_pause_on_exit = 320, 222, [5, 6, 5], 0, 18, 1, fnop, fnop, fnop, fnop, fnop, fnop, fnop, fnop, fnop, fnop, fnop, 0
  try:
    import graphic, nsp, cas, time
    def col3_2_rgb(c, bits, bgr):
      return c[2*bgr]//2**(8 - bits[0]) + c[1]//2**(8 - bits[1])*2**bits[0] + c[2*(not(bgr))]//2**(8-bits[2])*2**(bits[0] + bits[1])
    def poly_fill_rect(x1, y1, x2, y2, c):
      graphic.draw_filled_rectangle(int(x1), int(y1), int(x2), int(y2), c)
    def poly_fill_circle(x, y, r, c):
      graphic.draw_filled_circle(int(x), int(y), int(r), c)
    def poly_draw_ellipse(x, y, rx, ry, c):
      graphic.draw_arc(int(x), int(y), int(rx), int(ry), 0, 360, c)
    def poly_draw_string(s, x, y, cf, cb):
      st = 'draw_string("' + s + '", ' + str(float(x)) + ', ' + str(float(y)) + ', ' + str(col3_2_rgb(cf, color_bits, 1)) + ', ' + str(col3_2_rgb(cb, color_bits, 1)) + ')'
      print(st)
      cas.caseval(st)
    def poly_get_key():
      key = int(cas.caseval("get_key()"))
      #    left up   down rightenteresc  (       )
      d = {2:0, 3:1, 4:2, 1:3, 5:4, 9:5, 133:40, 134:41}
      for k in d:
        if key == d[k]:
          return k
      return 0
    def poly_sleep(d):
      s = time.monotonic() + d*1000
      while time.monotonic() < s:
        pass
    font_w, poly_set_pixel, poly_show, poly_pause = 15, graphic.set_pixel, graphic.show_screen, nsp.waitKeypress
  except:
    try: # NumWorks
      import kandinsky, ion, time
      from math import sqrt
      font_w, poly_set_pixel, poly_draw_string, poly_sleep = 11, kandinsky.set_pixel, kandinsky.draw_string, time.sleep
      def poly_draw_line(x1, y1, x2, y2, c):
        m, a1, b1, a2, b2 = 0, int(x1), int(y1), int(x2), int(y2)
        if (x2 - x1) ** 2 < (y2 - y1) ** 2:
          m, a1, a2, b1, b2 = 1, b1, b2, a1, a2
        if min(a1, a2) != a1: a1, b1, a2, b2 = a2, b2, a1, b1
        for k in range(a2 - a1 + 1):
          a, b = a1 + k, int(b1 + (b2 - b1) * k / ((a2 - a1) or 1))
          poly_set_pixel((a, b)[m], (b, a)[m], c)
      def poly_fill_rect(x, y, w, h, c):
        kandinsky.fill_rect(int(x), int(y), int(w), int(h), c)
      def poly_draw_ellipse(x, y, rx, ry, c):
        for h in range(-int(ry), int(ry)+1):
          w = sqrt(max(0, rx*rx*(1-h*h/ry/ry)))
          x1, x2 = int(x - w), int(x + w)
          yc = y + h
          poly_set_pixel(x1, yc, c)
          poly_set_pixel(x2, yc, c)
        for w in range(-int(rx), int(rx)+1):
          h = sqrt(max(0, ry*ry*(1-w*w/rx/rx)))
          y1, y2 = int(y - h), int(y + h)
          xc = x + w
          poly_set_pixel(xc, y1, c)
          poly_set_pixel(xc, y2, c)
      def poly_fill_circle(x, y, r, c):
        for h in range(-int(r), int(r)+1):
          w = sqrt(max(0, r*r*(1-h*h/r/r)))
          x1, x2 = int(x - w), int(x + w)
          yc = y + h
          poly_draw_line(x1, yc, x2, yc, c)
      def poly_pause():
        def key_down():
          for k in range(53):
            if ion.keydown(k):
              return 1
          return 0
        while key_down():
          pass
        while not (key_down()):
          pass
      def poly_draw_string(s, x, y, cf, cb):
        kandinsky.draw_string(s, x, y, cf, cb)
      def poly_get_key():
        # left up down right enter esc ( )
        d = {2:0, 3:1, 4:2, 1:3, 5:52, 9:5, 133:33, 134:34}
        for k in d:
          if ion.keydown(d[k]):
            return k
        return 0
    except:
      pass

  return screen_w, screen_h, font_h, poly_set_pixel, poly_fill_rect, poly_fill_circle, poly_draw_ellipse, poly_draw_string, poly_show, poly_pause, poly_get_key, poly_sleep

from math import sqrt
from random import randint

screen_w, screen_h, font_h, set_pixel, fill_rect, fill_circle, draw_ellipse, draw_string, show, pause, get_key, sleep = get_infos()

def cercle(x0,y0,r,c,e):
 for i in range(2*e):
   rt = max(0,r-i*0.5)
   if rt:
     draw_ellipse(x0,y0,rt,rt,c)

def cercle_plein(x0,y0,r,c1,e,c2):
 fill_circle(x0,y0,r-e,c2)
 fill_circle(x0,y0,r,c1)

def cercle_grade(x0,y0,R,c1,e,c2):
 for i in range(R):
   cercle(x0,y0,i,(c1[0]+i*(c2[0]-c1[0])//R,c1[1]+i*(c2[1]-c1[1])//R,c1[2]+i*(c2[2]-c1[2])//R),1)

systeme = (("Soleil",'centre du syteme solaire','0','1,989 × 10^30','696340', '0'),
           ("Mercure",'88','57','3,285 × 10^23','2439.7', '0'),
           ("Venus",'225','104','4,867 × 10^24','6051.8', '0'),
           ("Terre",'365','150','5,972 × 10^24','6371', 'Lune'),
           ("Mars",'687','220 ','6.39 × 10^23 ','3389,5','2'),
           ("Jupiter",'12','780 ','1.898 × 10^27','69911','3'),
           ("Saturne",'29','1,493','5.683 × 10^26','58232','~ 200'),
           ("Uranus",'84','2,9592','8.681 × 10^25','25362','27'),
           ("Neptune",'165','4,4762','1.024 × 10^26','24622','14'),
           )
coord = [[160,111,16], [180,110,6], [185,95,8], [120,100,9], [160,160,9], [115,150,15], [200,50,12], [170,20,9], [55,80,8]]
couleur1 = ((255,255,0), (251,136,7), (190,183,150), (30,160,173), (245,0,0), (162,125,105), (200,169,133), (191,209,232), (0,33,240))
couleur2 = ((235,128,0), (106,73,32), (121,104,83), (6,67,29), (101,12,12), (134,56,32), (189,138,80), (117,163,224), (5,15,81))

for k in range(len(coord)):
  coord[k][0] = coord[k][0]*screen_w//320
  coord[k][1] = coord[k][1]*screen_h//222
  coord[k][2] = coord[k][2]*screen_w//320
fill_rect(0,0,screen_w,screen_h,(0,0,0))
cercle_grade(screen_w//2,screen_h//2,197*screen_w//320,(0,35,143),10,(0,0,0))
cercle_grade(screen_w//2,screen_h//2,40*screen_w//320,couleur2[0],2,(0,35,143))
cercle_grade(screen_w//2,screen_h//2,coord[0][2],couleur1[0],2,couleur2[0])

def degrade(c1,c2,k):
 return (c1[0]+int(k*(c2[0]-c1[0])),c1[1]+int(k*(c2[1]-c1[1])),c1[2]+int(k*(c2[2]-c1[2])))

def tirage(n):
 for i in range(n):
   x=randint(0,screen_w)
   y=randint(0,screen_h)
   fill_rect(x,y,2,2,couleur(x,y))

def couleur(x,y):
 c=(couleur2[0],(13,89,175),(13,89,175),(44,122,211),(207,230,230),(207,230,230))
 d=sqrt((x-screen_w//2)**2+(y-screen_h//2)**2)/(screen_w/10+.5)
 return degrade(c[int(d)],c[(int(d)+1)%6],d-int(d))

tirage(250)
couleur, tirage = None, None

c_blanc=(255,255,255)
r_orb = (22,32,42,52,62,76,92,109)
for i in range(8):
   cercle(screen_w//2,screen_h//2,r_orb[i],(119,135,135),1)
r_orb = None
 
def solar_systeme_dessine(n, degrade=1):
 # Affiche un corps du sytème solaire, numéro du corps dans systeme[], degradé
 cercle_grade(coord[n][0], coord[n][1], coord[n][2], degrade and couleur1[n] or c_blanc, 2 + 9 * (n != 0), degrade and couleur2[n] or c_blanc)
 
def solar_systeme_texte(n=42, t=0): #affichage du texte en bas, de couleur associée à omega, avec les diverse infos
 try:
   get_keys()
   os = (192,53,53)
 except:
   os = (255,183,52)
 if n == 42 :
   fill_rect(1,screen_h-font_h,screen_w,font_h,os)
   draw_string("nsi.xyz/solar par Emmy & Jil ",2,screen_h-font_h,c_blanc,os)
 else:
   fill_rect(1,screen_h-font_h,screen_w,font_h,os)
   s = systeme[n][t]
   if t==1 and n:
     s = "periode de revo: " + s + ((n <=4 ) and " jours" or " ans")
   elif t==2:
     s = "dis soleil: " + s + " milli" + ((n <= 5 ) and "on" or "ard") + "s km"
   elif t==3:
     s = "masse: " + s + " kg"
   elif t==4:
     s = "rayon: " + str(s) + " km"
   elif t==5:
     s = "satellites connus: " + str(s)
   draw_string(s,2,screen_h-font_h,c_blanc,os)
 
solar_systeme_texte()
 
def navigation(): #navigation grâce aux flèches
  position = 0
  texte = 0  

  key = 0
  while key != 9:

    show()
    key = get_key()

    if key >=1 and key <=4:
      if key <= 2: #Gauche Droite
        avant = position
        position = min(max(position+2*(key == 1)-1, 0), 8) # le soleil [0] + 8 planète
        if avant != position:
          solar_systeme_dessine(avant)
        solar_systeme_dessine(position, 0)

      elif  key >= 3:  #haut bas
        texte = max(min(texte + 2*(key == 3) - 1, 5), 0) # Il faut donc avoir 5 données en plus du nom dans systeme[]

      solar_systeme_texte(position,texte)
      sleep(0.120) 
      

solar_systeme_dessine(0,0)
show()
sleep(1)
solar_systeme_dessine(0)

for k in range(1,9):
  cercle_grade(coord[k][0],coord[k][1],coord[k][2],couleur1[k],10,couleur2[k])

navigation()

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.