gud_os.py

Created by frablock

Created on June 22, 2022

7.66 KB

Un simulateur d’os, avec

  • un éditeur de fichier

  • un bloc note

  • paint

  • calculatrice

  • time

https://my.numworks.com/python/frablock/cfg

Marche avec Omega


from kandinsky import *;from ion import *;from time import sleep,localtime;from cfg import *;from turtle import *
try:import os
except:print("pas os")
_B,_D="white","black"
app=["file","note","cmd","time","calcul","paint"]
#app+home
icone=["100000011110011110011110011110000000","000000011110010010011110010010000000","000000011110010110011010010110000000","000000010110010110011000011110000000","101111000100101111010100101111010100","111111001100111111110011111111001100","110011100001000000100001100001100001"]
speed(10)
penup()
fill_rect(0,0,320,225,"0")
goto(-200,200)
draw_string("GUD OS",110,100,_B,"0")
for i in range(100):
  fill_rect(110,150,0+i,20,"green")
  sleep(0.05)
sleep(0.1)
def d_icone(x,y,g,ico):
    p=0
    for py in range(6):
        for px in range(6):
            fill_rect(x+px*g,y+py*g,g,g,icone[ico][p])
            p+=1
def i(im,pal,t=4,po=0,py=0,suppr="",bs=80):
 r,i=0,0
 while r<len(im):
  s,n=im[r],'';r+=1
  while r<len(im)and'9'>=im[r]>='0':n+=im[r];r+=1
  nb=1 if n==''else int(n);c=pal[ord(s)-65]
  for j in range(nb):
    if str(s)!=str(suppr):fill_rect(po+t*(i%bs),py+t*(i//bs),t,t,(c[0],c[1],c[2]))
    i+=1
def gm_tp(txt,ma=32,st=""):
 a,b,A,d=list(txt)+[''],'','\n'+st,[" ","\'",",","."];l=len(a)
 if l>ma:
  mb=int(ma)
  for j in range(l//ma):
   nl=1
   for i in range(1,ma//4):
    if d.count(a[mb-i])>=1:a[mb-i]=A;mb+=mb-i;nl=0;break
   if nl:a.insert(mb,A);mb+=mb
  for i in range(l):b+=a[i]
 else:b=str(txt)
 return b,l//ma
def pu(a,c=_B,x=10,y=75,w=300,h=75):fill_rect(x,y,w,h,(0,0,0));fill_rect(x+1,y+1,w-2,h-2,c);draw_string(gm_tp(a,31," ")[0],x+2,y+2,_D,c);draw_string('[OK]',x+2,y+h-20,_D,c);gm_c(1,1,1,1,5,1);sleep(0.2)
def gm_r():reset()
def gm_e():pu("Une erreur est survenue")
def background():i(bg[0],bg[1],bg[2],bg[3],bg[4],bg[5])
def gm_c(a,b,c,d,e,a5=0):
 while 1:
  if keydown(1)and e>=1:return a
  if keydown(2)and e>=2:return b
  if keydown(0)and e>=3:return c
  if keydown(3)and e>=4:return d
  if keydown(4)and e>=5:return a5
def gm_a(a,b,c,d,e,a5='#err:str?'):
 draw_string('[up]'+str(a),0,0)
 if e>=2:draw_string('[down]'+str(b),0,18)
 if e>=3:draw_string('[left]'+str(c),0,36)
 if e>=4:draw_string('[right]'+str(d),0,54)
 if e>=5:draw_string('[OK]'+str(a5),0,72)
def pick_menu(x,y,m):
  global po,launch
  if keydown(KEY_UP):
    fill_rect(x,y+20*po,10,10,(80,80,80));po-=1
    if 0>po:po=0
    fill_rect(x,y+20*po,10,10,"red");sleep(0.1)
  if keydown(KEY_DOWN):
    fill_rect(x,y+20*po,10,10,(80,80,80));po+=1
    if m<po:po=m
    fill_rect(x,y+20*po,10,10,"red");sleep(0.1)
  if keydown(KEY_OK):launch=1
def f_l():
  gm_r();fill_rect(0,0,320,225,(80,80,80))
  for i in range(len(os.listdir())):draw_string(os.listdir()[i],20,i*20,"1",(80,80,80))
  sleep(0.2)
def fichier():
  sleep(0.2)
  global po,launch
  po,launch=0,0
  f_l()
  while not keydown(KEY_HOME):
    pick_menu(0,0,len(os.listdir())-1)
    if launch:
      sleep(0.2)
      gm_a("Editer","Copier","Executer/Lire","Supprimer",5,"Revenir a la liste des fichiers")
      a=gm_c("e","c","r","s",5,"l")
      sp=os.listdir()[po]
      if a=="e":
        try:
         txt=input("Texte dans le fichier  \n\\n pour passer une ligne\n")
         nfile=open(sp,"w")
         nfile.write(txt)
         nfile.close()
         pu(sp+" a ete modifie")
        except:gm_e()
        launch=0;f_l()
      elif a=="c":
        try:
         dst=input(sp+" sera copie dans :\n")
         with open(sp) as file:
          nfile=open(dst,"w")
          nfile.write(file.read())
          nfile.close()
         pu(sp+" a ete copie dans "+dst)
        except:gm_e()
        launch=0;f_l()
      elif a=="r":
        gm_r()
        try:
          nfile=open(sp,"r")
          draw_string(nfile.read(),0,40)
        except:pu("Lecture imposible")
        gm_a("Executer","Retour",0,0,2)
        r_o_b=gm_c(1,0,0,0,2)
        if r_o_b:
          try:
            gm_r();draw_string(0,200,"Loading")
            nfile=open("file_runner_gudos.py","w")
            nfile.write("#fill_rectom "+sp.replace(".py","")+" import *")
            nfile.close()
            from file_runner_gudos import *
          except:gm_e()
        launch=0;f_l()
      elif a=="s":
        try:os.remove(sp);pu(sp+" a ete supprime")
        except:gm_e()
        launch=0;f_l()
      else:launch=0;f_l()
def note():
  try:
    nfile=open("note_gudos.noot","r")
    print(nfile.read())
    txt=input("Nouveau contenu des notes :\n-exit pour sortir\n")
    if txt!="-exit":
      nfile=open("note_gudos.noot","w")
      nfile.write(txt)
      nfile.close()
      pu("Vos notes ont bien ete modifiee")
  except:open("note_gudos.noot","w").close();gm_e()
def cmd():
  i_=input("GUD OS TERMINAL\n>GUD>root>")
  while not "exit" in i_:
    j_=i_.split(" ")
    if j_[0]=="echo":print(i_.replace("echo ",""))
    elif j_[0]=="dir":
      for i in os.listdir():print(i)
    elif j_[0]=="rename":
      try:
        os.rename(j_[1],j_[2]);print(j_[1],"devient",j_[2])
      except:print("Fichier introuvable")
    elif j_[0]=="mkfile":
      try:open(j_[1],"w").close();print(dst,"cree")
      except:print("Ce fichier existe deja")
    elif j_[0]=="cls":print("\n"*15)
    elif j_[0]=="cp":
      try:
        with open(j_[1]) as file:
          nfile=open(j_[2],"w")
          nfile.write(file.read())
          nfile.close()
        print(j_[1],"a ete copie dans",j_[2])
      except:print("Une erreur est survenue")
    elif j_[0]=="sysinfo":
      for i in list(os.uname().values()):print(i+"\n")
    elif j_[0]=="help":print("cls\ndir\nrename\nmkfile\ncp\nsysinfo\nhelp\nexit")
    else:print("Commande introuvable")
    i_=input(">GUD>root>")
  home()

def time():
  pu(str(ti[3])+":"+str(ti[4])+" "+str(ti[2])+"/"+str(ti[1])+"/"+str(ti[0]))
def c_add(x,y):return x+y
def c_sub(x,y):return x-y
def c_mul(x,y):return x*y
def c_div(x,y):return x/y
def calcul():
 q=0
 while not keydown(5) or not q:
  sleep(0.2)
  gm_r()
  gm_a("Somme","Difference","Produit","Quotient",5,"Home");c_v=gm_c("+","-","*","/",5,"close")
  if c_v=="close":q=1
  c_d={"+":c_add,"-":c_sub,"*":c_mul,"/":c_div}
  try:
    c_n1 = float(input("Premier nombre : "))
    c_n2 = float(input("Second nombre: "))
  except:print("nombre invalide");sleep(2);c_n1,c_n2=0,0
  sleep(0.2)
  pu(str(c_n1)+str(c_v)+str(c_n2)+"="+str(c_d[c_v](c_n1,c_n2)))
 home()
def paint():
  fd=forward
  q=0
  try:x=int(input("Precision : "))
  except:x=5
  reset()
  draw_string("P="+str(x),0,0)
  while not q:
    if keydown(KEY_RIGHT):
      setheading(0)
      fd(x)
    if keydown(KEY_UP):
      setheading(90)
      fd(x)
    if keydown(KEY_LEFT):
      setheading(180)
      fd(x)
    if keydown(KEY_DOWN):
      setheading(270)
      fd(x)
    if keydown(KEY_OK) or keydown(KEY_HOME):q=1
app_l=[fichier,note,cmd,time,calcul,paint]
def home():
    global po,launch
    gm_r()
    print("\n"*15)
    background()
    fill_rect(0,200,320,25,(80,80,80))
    d_icone(0,205,2,6)
    draw_string("Menu",15,205,_B,(80,80,80))
    draw_string(str(ti[3])+":"+str(ti[4]),270,205,_B,(80,80,80))
    home_show_menu=0
    po,launch=0,0
    while 1:
        if keydown(KEY_HOME):
          if home_show_menu:
              gm_r()
              background()
              fill_rect(0,200,320,25,(80,80,80))
              d_icone(0,205,2,6)
              draw_string("Menu",15,205,_B,(80,80,80))
              home_show_menu=0
              sleep(0.1)
          else:
            home_show_menu=1
            fill_rect(0,80,100,120,(80,80,80))
            for a in range(len(app)):
              draw_string(app[a],20,80+a*20,_B,(80,80,80))
              d_icone(0,80+a*20,2,a)
              fill_rect(90,80+20*po,10,10,"red")
            sleep(0.1)
        if home_show_menu:
            pick_menu(90,80,5)
            if launch:sleep(0.1);app_l[po]();gm_r();home()
        if keydown(5):break
home()

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.