我用Python寫了一個相當簡單的程序。代碼如下:import pygameimport timefrom math import *from random import *from pygame.locals import *def mutateBrain(brain): a = 0 for element in brain: brain[a][0] = element[0] + (1 * (0.5 - random())) brain[a][1] = element[1] + (1 * (0.5 - random())) a = a + 1 return brain;def generateFirstBrain(): genbrain = [] h = 0; while randint(0,5) != 0: asd = [2 * random(), 2 * random()] genbrain.insert(h, asd) h = h + 1 return genbrainpygame.init()width, height = 640, 480screen=pygame.display.set_mode((width, height))screen.fill(pygame.Color(255,255,255))pygame.draw.rect(screen,(255,0,0),(310,0,30,30))movesa = generateFirstBrain()movesb = generateFirstBrain()movesc = generateFirstBrain()cola = (255,255,0)colb = (255,0,255)colc = (0,255,255)while 1: movesa = mutateBrain(movesa) movesb = mutateBrain(movesb) movesc = mutateBrain(movesc) step = 0 acurrentx = 320 acurrenty = 240 bcurrentx = 320 bcurrenty = 240 ccurrentx = 320 ccurrenty = 240 totalsn = 0 if (len(movesa) >= len(movesb)) and (len(movesa) >= len(movesc)): totalsn = len(movesa) elif (len(movesb) >= len(movesa)) and (len(movesb) >= len(movesc)): totalsn = len(movesb) else: totalsn = len(movesc) for g in range(totalsn): screen.fill(pygame.Color(255,255,255)) pygame.draw.rect(screen,(255,0,0),(305,0,30,30)) try: acurrentx = acurrentx + 1 - movesa[step][0] acurrenty = acurrenty + 1 - movesa[step][1] except IndexError: acurrentx = acurrentx在這里,在第一個 while 循環(huán)結束時,無論我運行突變大腦函數(shù)多少次,movel 總是得到相同的值。但它會發(fā)生變化,因此隨機應該存在問題。有人可以幫我這個嗎?謝謝。
添加回答
舉報
0/150
提交
取消