Cateva sugestii, majoritatea PEP8 related:
- constantele ar trebui sa fie UPPERCASEd: (e.g: targetRadius ar trebui redenumit ca TARGET_RADIUS)
- ar trebui sa lasi doua linii noi intre functiile din afara unei clase sau intre functii si clase
Asta:
..
particleColor = sf.Color(255, 255, 255, 50)
def distance(x1, y1, x2, y2):
"""
Calculate the distance from 2 points. (X1, Y1) and (X2, Y2)
Formula: ( (X1-X2)**2 + (Y1-Y2)**2 )**1/2
"""
return math.sqrt((x1 - x2)**2 + (y1 - y2)**2)
class DNA():
...
Ar trebui sa arate asa:
..
particleColor = sf.Color(255, 255, 255, 50)
def distance(x1, y1, x2, y2):
"""
Calculate the distance from 2 points. (X1, Y1) and (X2, Y2)
Formula: ( (X1-X2)**2 + (Y1-Y2)**2 )**1/2.
"""
return math.sqrt((x1 - x2)**2 + (y1 - y2)**2)
class DNA():
...
- denumirea variabilelor tre' sa fie snake_cased (la fel si numele metodelor): e.g: def applyForce -> def apply_force etc
- cand iterezi printr-o lista, si ai nevoie si de index si de valoare, e recomandat sa faci asa:
lista_mea = ['malai', 'faina', 'pui', 'clatite']
for i, value in enumerate(lista_mea):
print(i, value)
"""
output:
(0, 'malai')
(1, 'faina')
(2, 'pui')
(3, 'clatite')
"""
- cand creezi o clasa care nu are nici-un argument poti renunta la paranteze: class DNA() -> class DNA:
- urmatoarea functie:
def crossover(self, partner):
"""
Take the genes from 2 parents and create 1 offspring
"""
newDNA = []
# Pick one random point from the genes
randomPoint = random.randint(0, len(self.genes))
for x in range(len(self.genes)):
if x < randomPoint:
newDNA.append(self.genes[x])
else:
newDNA.append(partner.genes[x])
return DNA(newDNA)
Poate fi rescrisa cu recomandarea de mai sus si folosind list comprehensions astfel:
def crossover(self, partner):
"""
Take the genes from 2 parents and create 1 offspring.
"""
random_point = random.randint(0, len(self.genes))
new_dna = [
value if i < random_point else partner.genes[i]
for i, value in enumerate(self.genes)
]
return DNA(new_dna)
Probabil ca se pot face mai multe schimbari insa nu am timp sa ma uit peste tot codu'. Felicitari oricum pentru efort si keep it goin'