Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.37 KB | None | 0 0
  1. import numpy as np
  2. import pygame
  3. import random
  4.  
  5. #Initializes Pygame & Creates Window
  6. pygame.init()
  7.  
  8. backgroundColor = (255, 255, 255)
  9. screenSize = (800, 600)
  10. screen = pygame.display.set_mode(screenSize)
  11. pygame.display.set_caption("Genetic Algorithm")
  12. screen.fill(backgroundColor)
  13. clock = pygame.time.Clock()
  14.  
  15. #Loads Images & Rectangles
  16. creaturePNG = pygame.image.load("Creature.png").convert_alpha()
  17. foodPNG = pygame.image.load("Food.png").convert_alpha()
  18.  
  19. #Establishes Size Of Population
  20. creatureCount = 40
  21.  
  22. deadCreatures = []
  23.  
  24. numGenerations = 10
  25.  
  26. #Generates Random 12 Digit DNA For First Generation
  27. def initialDNA():
  28. while True:
  29. randomDNA = ""
  30. total = 0
  31. for i in range(12):
  32. digit = random.randint(1, 9)
  33. total += digit
  34. digit = str(digit)
  35. randomDNA = randomDNA + digit
  36. if total <= 60:
  37. break
  38. return randomDNA
  39.  
  40. def reproduce(deadCreatureList, creatureCount):
  41. reproducingCreatures = deadCreatureList[0.5*creatureCount:creatureCount]
  42. for i in range(0.25*creatureCount):
  43. creature1 = reproducingCreatures[0]
  44. del reproducingCreatures[0]
  45. creature2 = reproducingCreatures[0]
  46. del reproducingCreatures[0]
  47. DNA1 = str(creature1.DNA)
  48. DNA2 = str(creature2.DNA)
  49. crosspoint = random.randint(0, 12)
  50. newDNA1 = int(DNA1[0:crosspoint] + DNA2[crosspoint:])
  51. newDNA2 = int(DNA2[0:crosspoint] + DNA1[crosspoint:])
  52. return newDNA1, newDNA2
  53.  
  54.  
  55. #Creates Creatures From DNA
  56. class Creature:
  57. def __init__(self, DNA, image):
  58. self.DNA = DNA
  59. self.speed = (int(self.DNA[0:2])/100) + 1
  60. self.strength = int(DNA[2:4])/10
  61. self.foodCap = int(DNA[4:6])
  62. self.maxHealth = int(DNA[6:8])
  63. self.health = self.maxHealth
  64. self.regeneration = int(DNA[8:10])/10
  65. self.turnProbability = int(DNA[10:12])
  66. self.currentFood = self.foodCap
  67. self.image = image
  68. self.rect = self.image.get_rect()
  69. self.directions = [-1, 1]
  70. self.directionX = random.choice(self.directions)
  71. self.directionY = random.choice(self.directions)
  72. self.isAlive = True
  73.  
  74. def spawn(self):
  75. self.x = random.randint(25, 775)
  76. self.y = random.randint(25, 575)
  77. self.loc = (self.x, self.y)
  78. self.rect = pygame.Rect(0, 0, 25, 25)
  79. self.rect.center = self.loc
  80.  
  81. def move(self):
  82. changeDirection = random.randint(0, 100)
  83. if changeDirection < self.turnProbability:
  84. self.directionX = random.choice(self.directions)
  85. self.directionY = random.choice(self.directions)
  86. self.x += self.directionX * self.speed
  87. self.y += self.directionY * self.speed
  88. if self.x > 775:
  89. self.x = 775
  90. elif self.x < 25:
  91. self.x = 25
  92. elif self.y > 575:
  93. self.y = 575
  94. elif self.y < 25:
  95. self.y = 25
  96. self.loc = (self.x, self.y)
  97. self.rect.center = self.loc
  98.  
  99. def foodCollision(self, foodList):
  100. foodRects = []
  101. for i in range(25):
  102. food = foodList[i]
  103. foodRect = food.rect
  104. foodRects.append(foodRect)
  105. collision = self.rect.collidelist(foodRects)
  106. if collision > 0:
  107. self.currentFood += 20
  108. if self.currentFood > self.foodCap:
  109. self.currentFood = self.foodCap
  110.  
  111. def creatureCollision(self, creatureList, creatureCount, creatureNumber):
  112. creatureRects = []
  113. for i in range(creatureCount):
  114. creature = creatures[i]
  115. creatureRect = creature.rect
  116. creatureRects.append(creatureRect)
  117. collision = self.rect.collidelist(creatureRects)
  118. creature = creatures[collision]
  119. if collision >= 0:
  120. if collision != creatureNumber:
  121. if creature.health > 0:
  122. self.health -= creature.strength
  123. if self.health < 0:
  124. self.health = 0
  125.  
  126. def starve(self):
  127. if self.currentFood == 0:
  128. self.health -= 1
  129.  
  130. def display(self):
  131. screen.blit(self.image, self.loc)
  132.  
  133. #Creates Food Objects For Creatures To Eat
  134. class Food:
  135. def __init__(self, image):
  136. self.image = image
  137. self.rect = self.image.get_rect()
  138.  
  139. def spawn(self):
  140. self.x = random.randint(25, 775)
  141. self.y = random.randint(25, 575)
  142. self.loc = (self.x, self.y)
  143. self.rect = pygame.Rect(0, 0, 25, 25)
  144. self.rect.center = self.loc
  145.  
  146. def creatureCollision(self, creatureList, creatureCount):
  147. creatureRects = []
  148. for i in range(creatureCount):
  149. creature = creatures[i]
  150. creatureRects.append(creature.rect)
  151. collision = self.rect.collidelist(creatureRects)
  152. creature = creatures[collision]
  153. if collision >= 0:
  154. if creature.health > 0:
  155. self.spawn()
  156.  
  157. def display(self):
  158. screen.blit(self.image, self.loc)
  159.  
  160.  
  161. running = True
  162. while running:
  163. for event in pygame.event.get():
  164. if event.type == pygame.QUIT:
  165. running = False
  166.  
  167. screen.fill(backgroundColor)
  168.  
  169. for i in range(numGenerations):
  170. if i == 0:
  171. #Spawns Creatures Into World
  172. creatures = []
  173. for i in range(creatureCount):
  174. DNA = initialDNA()
  175. print (DNA)
  176. creature = Creature(DNA, creaturePNG)
  177. creature.spawn()
  178. creatures.append(creature)
  179.  
  180. elif i > 0:
  181. creatures = []
  182. for i in range(0.5*creatureCount):
  183. DNA1, DNA2 = reproduce(deadCreatures, creatureCount)
  184. print (DNA1, DNA2)
  185. creature1, creature2 = Creature(DNA1, creaturePNG), Creature(DNA2, creaturePNG)
  186. creature.spawn()
  187. creatures.append(creature)
  188.  
  189. #Spawns Food Into World
  190. foodList = []
  191. for i in range(25):
  192. food = Food(foodPNG)
  193. food.spawn()
  194. foodList.append(food)
  195.  
  196. livingCreatures = True
  197.  
  198. while livingCreatures:
  199. for i in range(25):
  200. food = foodList[i]
  201. food.creatureCollision(creatures, creatureCount)
  202. food.display()
  203.  
  204. for i in range(creatureCount):
  205. creature = creatures[i]
  206. if creature.health > 0:
  207. creature.move()
  208. creature.foodCollision(foodList)
  209. creature.creatureCollision(creatures, creatureCount, i)
  210. creature.currentFood -= 0.5
  211. if creature.currentFood < 0:
  212. creature.currentFood = 0
  213. if creature.currentFood > 0:
  214. creature.health += creature.regeneration
  215. if creature.health > creature.maxHealth:
  216. creature.health = creature.maxHealth
  217. creature.starve()
  218. creature.display()
  219.  
  220. if creature.isAlive == True:
  221. if creature.health == 0:
  222. print ("DEATH")
  223. deadCreatures.append(i)
  224. creature.isAlive = False
  225.  
  226. if len(deadCreatures) == creatureCount:
  227. livingCreatures = False
  228.  
  229. pygame.display.flip()
  230. clock.tick(10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement