-
Notifications
You must be signed in to change notification settings - Fork 0
/
hoik.py
98 lines (72 loc) · 3.12 KB
/
hoik.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Pygame
from pygame import Vector2
import pygame as pg
# Random
import random
# Rules class
from rules import Rules
class Hoik(Rules):
def __init__(self, screen_width, screen_height):
super().__init__(screen_width, screen_height)
self.position = Vector2(random.randrange(0, screen_width), random.randrange(0, screen_height))
self.velocity = Vector2(random.uniform(-1, 1), random.uniform(-1, 1))
self.radius = 100 # Radius of the hoiks vision
self.size = 5 # Size of the hoiks
### Draw the hoiks on the screen
def draw(self, screen):
pg.draw.circle(screen, (255, 0, 100), self.position, self.size)
# Gain size when eating boids
def grow(self, boids):
for boid in boids:
if (boid.position - self.position).length() < self.size + 2: # + 2 for extra reach
self.size += 1 # Gain size
if self.size > 20:
self.size = 20 # Max size
# Lose size when not eating boids for a while
def shrink(self):
self.size -= 0.01 # Lose size continuously
if self.size < 5: # Min size
self.size = 5
def reproduce(self, hoiks):
if len(hoiks) < 10:
if self.size > 10:
if random.random() < 0.001:
# Reproduce a new hoik next to the parent without overlapping and crashin the game
hoiks.append(Hoik(self.screen_width, self.screen_height))
hoiks[-1].position = self.position + Vector2(random.uniform(-1, 1), random.uniform(-1, 1))
def die(self, hoiks):
# Die if there are too many hoiks
if len(hoiks) > 2:
if self.size < 6:
if random.random() < 0.0005:
hoiks.remove(self)
# Randomly die, maybe heart attack or something
if random.random() < 0.0001:
hoiks.remove(self)
### Update the position of the hoiks
def update(self, boids, hoiks, obstacles=[]):
### Weights of the rules. Attemt to make it more realistic
w1 = 0.7 # Weight for chasing the closest boid
w2 = 0.5 # Keep distance away from other hoiks
w3 = 0.4 # Match velocity
### Rules hoiks follow
chase = w1 * Rules.chase(self, boids) # Chase the closest boid
efficiency = w2 * Rules.keep_distance_away(self, hoiks, 50) # Keep distance away from other hoiks. Avoid collision and converging on each other
dodge = Rules.tend_to_place(self, obstacles) # Avoid obstacles
align = w3 * Rules.match_velocity(self, boids) # Match velocity
# Size, reproduction and death
self.grow(boids)
self.shrink()
self.reproduce(hoiks)
self.die(hoiks)
# Update velocity
self.velocity += chase + efficiency + align + dodge
# Limit the speed of the hoiks
if self.size > 10:
self.velocity.scale_to_length(2)
else:
self.velocity.scale_to_length(7)
# Update position
self.position += self.velocity
# Wrap the position of the hoiks
Rules.bound_position(self)