Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Colore:
- def __init__(self, r, g, b):
- rng = range(0, 256)
- if type(r) == type(g) == type(b) == int and r in rng and g in rng and b in rng:
- self.colore = (r, g, b)
- def col_tuple(self):
- return self.colore
- def __repr__(self):
- return str(self.colore)
- class Foglio:
- def __init__(self, bg):
- self.bg = bg # bg oggetto di Colore
- self.shapes = []
- print("Sono un foglio di colore {}".format(self.bg))
- def aggiungi(self, forma, x): # forma oggetto di Forma; x = int -> coordinata ascissa
- # Con questo, dico che la forma
- # va bene fino a prova contraria
- errato = False
- # La forma di che colore è ?
- if forma.colore.col_tuple() == self.bg.col_tuple():
- # Se il foglio è dello stesso
- # colore della forma, non va bene
- errato = True
- # Adesso controlliamo tutte le forme sul foglio...
- for posizione, elemento in self.shapes:
- # Esiste già un'altro oggetto a quella coordinata ?
- if x == posizione:
- errato = True
- if not errato:
- self.shapes.append([x,forma.colore.col_tuple()])
- else:
- print("Non posso aggiungere la forma !")
- class Forma:
- def __init__(self, colore):
- self.colore = colore #colore oggetto di Colore
- bianco = Colore(255,255,255)
- rosso = Colore(255, 0, 0)
- foglio1 = Foglio(Colore(0,0,0))
- forma1 = Forma(colore=rosso)
- foglio1.aggiungi(forma=forma1, x=5)
- forma2 = Forma(colore=rosso)
- foglio1.aggiungi(forma=forma2, x=5) #-> NON DOVREBBE AGGIUNGERLA
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement