i working on program evolves creatures on time using genetic algorithm. however, reason, pygame display stopped working , have absolutely no idea why. when run program, window opens sits on black screen. have tested see program gets , 38 creatures die nothing happens. however, these creatures should displaying before deaths also, aren't.any wonderful! thank time!
here's code:
import numpy np import pygame import random #initializes pygame & creates window pygame.init() backgroundcolor = (255, 255, 255) screensize = (800, 600) screen = pygame.display.set_mode(screensize) pygame.display.set_caption("genetic algorithm") screen.fill(backgroundcolor) clock = pygame.time.clock() #loads images & rectangles creaturepng = pygame.image.load("creature.png").convert_alpha() foodpng = pygame.image.load("food.png").convert_alpha() #establishes size of population creaturecount = 40 deadcreatures = [] numgenerations = 10 #generates random 12 digit dna first generation def initialdna(): while true: randomdna = "" total = 0 in range(12): digit = random.randint(1, 9) total += digit digit = str(digit) randomdna = randomdna + digit if total <= 60: break return randomdna def reproduce(deadcreaturelist, creaturecount): reproducingcreatures = deadcreaturelist[0.5*creaturecount:creaturecount] in range(0.25*creaturecount): creature1 = reproducingcreatures[0] del reproducingcreatures[0] creature2 = reproducingcreatures[0] del reproducingcreatures[0] dna1 = str(creature1.dna) dna2 = str(creature2.dna) crosspoint = random.randint(0, 12) newdna1 = int(dna1[0:crosspoint] + dna2[crosspoint:]) newdna2 = int(dna2[0:crosspoint] + dna1[crosspoint:]) return newdna1, newdna2 #creates creatures dna class creature: def __init__(self, dna, image): self.dna = dna self.speed = (int(self.dna[0:2])/100) + 1 self.strength = int(dna[2:4])/10 self.foodcap = int(dna[4:6]) self.maxhealth = int(dna[6:8]) self.health = self.maxhealth self.regeneration = int(dna[8:10])/10 self.turnprobability = int(dna[10:12]) self.currentfood = self.foodcap self.image = image self.rect = self.image.get_rect() self.directions = [-1, 1] self.directionx = random.choice(self.directions) self.directiony = random.choice(self.directions) self.isalive = true def spawn(self): self.x = random.randint(25, 775) self.y = random.randint(25, 575) self.loc = (self.x, self.y) self.rect = pygame.rect(0, 0, 25, 25) self.rect.center = self.loc def move(self): changedirection = random.randint(0, 100) if changedirection < self.turnprobability: self.directionx = random.choice(self.directions) self.directiony = random.choice(self.directions) self.x += self.directionx * self.speed self.y += self.directiony * self.speed if self.x > 775: self.x = 775 elif self.x < 25: self.x = 25 elif self.y > 575: self.y = 575 elif self.y < 25: self.y = 25 self.loc = (self.x, self.y) self.rect.center = self.loc def foodcollision(self, foodlist): foodrects = [] in range(25): food = foodlist[i] foodrect = food.rect foodrects.append(foodrect) collision = self.rect.collidelist(foodrects) if collision > 0: self.currentfood += 20 if self.currentfood > self.foodcap: self.currentfood = self.foodcap def creaturecollision(self, creaturelist, creaturecount, creaturenumber): creaturerects = [] in range(creaturecount): creature = creatures[i] creaturerect = creature.rect creaturerects.append(creaturerect) collision = self.rect.collidelist(creaturerects) creature = creatures[collision] if collision >= 0: if collision != creaturenumber: if creature.health > 0: self.health -= creature.strength if self.health < 0: self.health = 0 def starve(self): if self.currentfood == 0: self.health -= 1 def display(self): screen.blit(self.image, self.loc) #creates food objects creatures eat class food: def __init__(self, image): self.image = image self.rect = self.image.get_rect() def spawn(self): self.x = random.randint(25, 775) self.y = random.randint(25, 575) self.loc = (self.x, self.y) self.rect = pygame.rect(0, 0, 25, 25) self.rect.center = self.loc def creaturecollision(self, creaturelist, creaturecount): creaturerects = [] in range(creaturecount): creature = creatures[i] creaturerects.append(creature.rect) collision = self.rect.collidelist(creaturerects) creature = creatures[collision] if collision >= 0: if creature.health > 0: self.spawn() def display(self): screen.blit(self.image, self.loc) running = true while running: event in pygame.event.get(): if event.type == pygame.quit: running = false screen.fill(backgroundcolor) in range(numgenerations): if == 0: #spawns creatures world creatures = [] in range(creaturecount): dna = initialdna() print (dna) creature = creature(dna, creaturepng) creature.spawn() creatures.append(creature) elif > 0: creatures = [] in range(0.5*creaturecount): dna1, dna2 = reproduce(deadcreatures, creaturecount) print (dna1, dna2) creature1, creature2 = creature(dna1, creaturepng), creature(dna2, creaturepng) creature.spawn() creatures.append(creature) #spawns food world foodlist = [] in range(25): food = food(foodpng) food.spawn() foodlist.append(food) livingcreatures = true while livingcreatures: in range(25): food = foodlist[i] food.creaturecollision(creatures, creaturecount) food.display() in range(creaturecount): creature = creatures[i] if creature.health > 0: creature.move() creature.foodcollision(foodlist) creature.creaturecollision(creatures, creaturecount, i) creature.currentfood -= 0.5 if creature.currentfood < 0: creature.currentfood = 0 if creature.currentfood > 0: creature.health += creature.regeneration if creature.health > creature.maxhealth: creature.health = creature.maxhealth creature.starve() creature.display() if creature.isalive == true: if creature.health == 0: print ("death") deadcreatures.append(i) creature.isalive = false if len(deadcreatures) == creaturecount: livingcreatures = false pygame.display.flip() clock.tick(10)
i suspect livingcreatures
variable never set false
, pygame.display.flip()
never gets called--and won't see on screen @ until flip buffers. fact you're filling screen color, still seeing black, dead giveaway sort of problem.
in future, should try reproduce problem in simpler example without domain-specific, irrelevant code.