-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpredator.py
More file actions
70 lines (57 loc) · 2.74 KB
/
predator.py
File metadata and controls
70 lines (57 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from creature import *
import constants
import utils
MAX_HEALTH = constants.PredatorMAX_HEALTH
VIEW_RADIUS = constants.PredatorVIEW_RADIUS
MAX_VELOCITY = constants.PredatorMAX_VELOCITY
SIZE = constants.PredatorSIZE
COLOR = constants.PredatorCOLOR
HEALTH_GAIN = constants.PredatorHEALTH_GAIN
HEALTH_LOSS = constants.PredatorHEALTH_LOSS
MAX_DETECTION_OF_PREY = constants.MAX_DETECTION_OF_PREY
MAX_ATTRACTION_TO_PREY = constants.MAX_ATTRACTION_TO_PREY
MUTATION_AMOUNT = constants.PredatorsMUTATION_AMOUNT
class Predator(Creature):
def __init__(self,creatureFields,detection,attraction):
super(Predator,self).__init__(*creatureFields)
self.detectionOfPrey = detection
self.attractionOfPrey = attraction
self.velocity.scale_to_length(self.maxVelocity)
def getTarget(self, CounterCreatures):
FilteredPrey = utils.PreyFilterUsingEuclideanDistances((self.rect.centerx,self.rect.centery) ,CounterCreatures ,self.fieldRadius)
return utils.PredictPredatorDirection((self.rect.centerx,self.rect.centery) ,FilteredPrey, self.attractionOfPrey)
def move(self, width, height, CounterCreatures):
targetVelocity = self.getTarget(CounterCreatures)
if targetVelocity.magnitude() != 0:
targetVelocity.scale_to_length(self.maxVelocity)
steer = targetVelocity - self.velocity
if steer.magnitude() > constants.maxForce:
steer.scale_to_length(constants.maxForce)
self.velocity = self.velocity + steer
super().move(width, height);
def details(self):
if(self.alive):
super().details("Predator")
print(f"The Predator's detection capability is {self.detectionOfPrey}")
print(f"The Predator's attraction capability is {self.attractionOfPrey}")
else:
print("The Predator is dead. Sorry :(")
def crossbreed(self, other):
childDetectionOfPrey = (self.detectionOfPrey + other.detectionOfPrey) / 2
childAttractionOfPrey = (self.attractionOfPrey + other.attractionOfPrey) / 2
return Predator(
(
MAX_HEALTH,
VIEW_RADIUS,
MAX_VELOCITY,
(random.randint(0, constants.WIDTH), random.randint(0, constants.HEIGHT)),
(random.uniform(-1, 1) * constants.INIT_VELOCITY, random.uniform(-1, 1) * constants.INIT_VELOCITY),
COLOR,
SIZE,
),
childDetectionOfPrey,
childAttractionOfPrey,
)
def mutate(self):
self.detectionOfPrey += random.uniform(-1, 1) * MUTATION_AMOUNT * MAX_DETECTION_OF_PREY
self.attractionOfPrey += random.uniform(-1, 1) * MUTATION_AMOUNT * MAX_ATTRACTION_TO_PREY