def upX(self, screen): self.x += self.speed self.rect.move_ip(self.speed, 0) wDistances = distances(self.walls, self, screen, self.visRad, False) hit = False for l in wDistances: if l < 5: hit = True if hit: self.x -= self.speed self.rect.move_ip(-self.speed, 0)
def upY(self, screen): self.y += self.speed self.rect.move_ip(0, self.speed) wDistances = distances(self.walls, self, screen, self.visRad, False) hit = False for l in wDistances: if l < 5: hit = True if hit: self.y -= self.speed self.rect.move_ip(0, -self.speed)
def getInputs(self, players, hunters, walls, holes, screen): d = 5000 for h in holes: distance = math.sqrt((self.x - (h.x + h.w / 2))**2 + (self.y - (h.x + h.h / 2))**2) if distance < d: d = distance self.closestHoleX = h.x + h.w / 2 self.closestHoleY = h.y + h.h / 2 nnInputArray = [ self.x / 1000, self.y / 1000, self.closestHoleX / 1000, self.closestHoleY / 1000 ] for h in hunters: nnInputArray.append(h.x / 1000) nnInputArray.append(h.y / 1000) for p in players: if p.x == self.x and p.y == self.y: continue nnInputArray.append(p.x / 1000) nnInputArray.append(p.y / 1000) nnInputArray.append(self.isInHole) wDistances = distances(walls, self, screen, self.visRad) for i in range(len(wDistances)): wDistances[i] = wDistances[i] / self.visRad nnInputArray.extend(wDistances) return nnInputArray
def getInputs(self, players, walls, holes, screen): #punishes staying still if self.prevX == self.x and self.prevY == self.y: self.fitness += -10 self.prevX = self.x self.prevY = self.y d = 5000 for h in holes: distance = math.sqrt((self.x - (h.x + h.w / 2))**2 + (self.y - (h.x + h.h / 2))**2) if distance < d: d = distance self.closestHoleX = h.x + h.w / 2 self.closestHoleY = h.y + h.h / 2 nnInputArray = [ self.x / 1000, self.y / 1000, self.closestHoleX / 1000, self.closestHoleY / 1000 ] wDistances = distances(walls, self, screen, self.visRad) playerLines = [] for p in players: if p.isInHole == 0: playerLines.append([(p.rect.x, p.rect.y), (p.rect.x + p.rect.w, p.rect.y)]) playerLines.append([(p.rect.x, p.rect.y + p.rect.h), (p.rect.x + p.rect.w, p.rect.y + p.rect.h) ]) playerLines.append([(p.rect.x, p.rect.y), (p.rect.x, p.rect.y + p.rect.h)]) playerLines.append([(p.rect.x + p.rect.w, p.rect.y), (p.rect.x + p.rect.w, p.rect.y + p.rect.h) ]) pDistances = distances(playerLines, self, screen, self.visRad) for i in range(len(pDistances)): if wDistances[i] < pDistances[i]: pDistances[i] == self.visRad for d in range(len(pDistances)): #tiny incentive to get closer to players so that the hunter can more easily stumble into points if pDistances[d] != self.visRad: self.fitness += 1 - pDistances[d] / self.visRad #bumps down player score a little bit so that they try and avoid the hunter #too lazy to change the code to return which player it is so I'll just bump #them all down and hope its helpful. it might make them coordinate? ¯\_(ツ)_/¯ for p in players: p.loss += 1 - pDistances[d] / self.visRad for p in players: d = distances([[(p.rect.x, p.rect.y), (p.rect.x + p.rect.w, p.rect.y)], [(p.rect.x, p.rect.y + p.rect.h), (p.rect.x + p.rect.w, p.rect.y + p.rect.h)], [(p.rect.x, p.rect.y), (p.rect.x, p.rect.y + p.rect.h)], [(p.rect.x + p.rect.w, p.rect.y), (p.rect.x + p.rect.w, p.rect.y + p.rect.h)]], self, screen, self.visRad) for i in range(len(d)): if wDistances[i] < d[i]: d[i] = self.visRad for n in d: if n < 8: p.capture() for i in range(len(wDistances)): wDistances[i] = wDistances[i] / self.visRad for i in range(len(pDistances)): pDistances[i] = pDistances[i] / self.visRad nnInputArray.extend(wDistances) nnInputArray.extend(pDistances) return nnInputArray