Kod Debug

Başlatan utdmr, 10 Haziran 2010 - 00:19:26

« önceki - sonraki »

0 Üyeler ve 1 Ziyaretçi konuyu incelemekte.

utdmr

Arkadaşlar iki saattir boş boş koda bakıyorum, hala neden istediğimi yapmıyor, anlamış değilim:from PIL import Image, ImageDraw
from math import radians,cos,sin
from PyQt4 import QtGui,QtCore
import random,os, sys
import tempfile

random.seed(os.urandom(10))

class phenotype:
def __init__(self,g):
self.im=Image.new("RGB",[240,240],(255,255,255))
self.p=(120,120)
for i in g.genes:
self.add_line(i)
def get(self):
out=tempfile.NamedTemporaryFile(delete=False)
path=out.name
self.im.save(out, "PNG")
out.close()
return path
def add_line(self,t):
self.p=self.draw_line(t.angle,t.length,t.width)
def draw_line(self,angle,length,width):
d=ImageDraw.Draw(self.im)
x=length*cos(radians(angle))
y=length*sin(radians(angle))
new=(self.p[0]+x,self.p[1]+y)
d.line((self.p[0],self.p[1])+new,fill=(0,0,0),width=width)
return new

class gene:
def __init__(self):
self.angle=0
self.length=0
self.width=0
self.mutation()
def mutation(self):
self.angle=random.randint(0,360)
self.length=random.randint(5,20)
self.width=random.randint(1,2)


class genotype:
def __init__(self):
self.genes=[]
a=gene()
self.genes.append(a)
def evolve(self):
g=genotype()
g.genes=self.genes
choice=random.randint(1,3)
if choice==1: # add one gene
a=gene()
g.genes.append(a)   
if choice==2 and len(g.genes)>2: # gene mutation
which=random.randint(0,len(self.genes)-1)
g.genes[which].mutation()
if choice==3 and len(g.genes)>2: # destroy one gene
which=random.randint(0,len(g.genes)-1)
del g.genes[which]
return g

class gui:
def __init__(self):
app = QtGui.QApplication(sys.argv)

w=QtGui.QWidget()
w.show()

l=QtGui.QHBoxLayout()
images=[]

self.genotypes=[]

self.labels=[]

for i in range(5):
g=genotype()

self.genotypes.append(g)

p=phenotype(g)
path=p.get()

label=ClickableQLabel()
label.setPixmap(QtGui.QPixmap(path))

os.unlink(path)

self.labels.append(label)

l.addWidget(label)

QtCore.QObject.connect(self.labels[0],QtCore.SIGNAL("clicked()"),self.clicked0)
QtCore.QObject.connect(self.labels[1],QtCore.SIGNAL("clicked()"),self.clicked1)
QtCore.QObject.connect(self.labels[2],QtCore.SIGNAL("clicked()"),self.clicked2)
QtCore.QObject.connect(self.labels[3],QtCore.SIGNAL("clicked()"),self.clicked3)
QtCore.QObject.connect(self.labels[4],QtCore.SIGNAL("clicked()"),self.clicked4)

w.setLayout(l)

app.exec_()
def clicked0(self):
self.choose(0)
def clicked1(self):
self.choose(1)
def clicked2(self):
self.choose(2)
def clicked3(self):
self.choose(3)
def clicked4(self):
self.choose(4)

def choose(self,a):
chosen=self.genotypes[a]
self.genotypes=[]
for i in range(5):
k=chosen.evolve()
self.genotypes.append(k)
del k

k=0
for i in self.genotypes:
print "--"
print len(self.genotypes[-1].genes)
p=phenotype(i)
path=p.get()
self.labels[k].setPixmap(QtGui.QPixmap(path))
os.unlink(path)
k=k+1

def image_path(s):
out=tempfile.NamedTemporaryFile(delete=False)
path=cikis.name
out.write(s)
out.close()
return path

class ClickableQLabel(QtGui.QLabel):
def __init(self, parent):
QLabel.__init__(self, parent)
def mouseReleaseEvent(self, ev):
self.emit(QtCore.SIGNAL('clicked()'))


def print_genotype(a):
print "printing genotype"
k=0
for i in a:
print k,
print "->",
print len(i.genes)
k=k+1
print "end"
gui()

Programın rastgele oluşturulan beş şekilden tıklananından yola çıkarak yeni şekiller türetmesi gerekiyor, ancak garip bir şekilde, ilk tıklamadan sonra gelen 5 şekil aynı.

Çözene program benden hediye(Zaten bitince GPL olacak) :).
Kişisel Blogum: Çoğunlukla Zararsız - cogunluklazararsiz.org
--
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -and a lot of courage- to move in the opposite direction.