Advertisement
Guest User

Untitled

a guest
Dec 15th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.66 KB | None | 0 0
  1. class Colore:
  2.     def __init__(self, r, g, b):
  3.         rng = range(0, 256)
  4.         if type(r) == type(g) == type(b) == int and r in rng and g in rng and b in rng:
  5.             self.colore = (r, g, b)
  6.  
  7.     def col_tuple(self):
  8.         return self.colore
  9.  
  10.     def __repr__(self):
  11.         return str(self.colore)
  12.  
  13.  
  14. class Foglio:
  15.  
  16.     def __init__(self, bg):
  17.         self.bg = bg  # bg oggetto di Colore
  18.         self.shapes = []
  19.         print("Sono un foglio di colore {}".format(self.bg))
  20.  
  21.     def aggiungi(self, forma, x):  # forma oggetto di Forma; x = int -> coordinata ascissa
  22.         # Con questo, dico che la forma
  23.         # va bene fino a prova contraria
  24.         errato = False
  25.  
  26.         # La forma di che colore è ?
  27.         if forma.colore.col_tuple() == self.bg.col_tuple():
  28.             # Se il foglio è dello stesso
  29.             # colore della forma, non va bene
  30.             errato = True
  31.  
  32.         # Adesso controlliamo tutte le forme sul foglio...
  33.         for posizione, elemento in self.shapes:
  34.             # Esiste già un'altro oggetto a quella coordinata ?
  35.             if x == posizione:
  36.                 errato = True
  37.                
  38.         if not errato:
  39.             self.shapes.append([x,forma.colore.col_tuple()])
  40.         else:
  41.             print("Non posso aggiungere la forma !")
  42.  
  43. class Forma:
  44.  
  45.     def __init__(self, colore):
  46.         self.colore = colore #colore oggetto di Colore
  47.  
  48.  
  49.  
  50. bianco = Colore(255,255,255)
  51. rosso = Colore(255, 0, 0)
  52.  
  53. foglio1 = Foglio(Colore(0,0,0))
  54. forma1 = Forma(colore=rosso)
  55. foglio1.aggiungi(forma=forma1, x=5)
  56. forma2 = Forma(colore=rosso)
  57. foglio1.aggiungi(forma=forma2, x=5) #-> NON DOVREBBE AGGIUNGERLA
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement