-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
executable file
·161 lines (118 loc) · 4.95 KB
/
main.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import pygame
from pygame.locals import *
from entity import Ant, Leaf, Spider
from random import randint
from gameobjects.vector2 import Vector2
from config import ANT_COUNT, SCREEN_SIZE, NEST_POSITION, NEST_SIZE
class World(object):
"""
The World class that the simulation entities live in.
It contain the nest, represented by a circle in the center of the screen, and a number of Ants, Spiders and Leafs
entities.
"""
def __init__(self):
self.entities = {}
self.entity_id = 0
# Draw the nest (a circle) on the background
self.background = pygame.surface.Surface(SCREEN_SIZE).convert()
self.background.fill((255, 255, 255))
pygame.draw.circle(self.background, (200, 222, 187), NEST_POSITION, int(NEST_SIZE))
def add_entity(self, entity):
"""
Stores the entity then advances the current id.
:param entity: The entity instance to be added
:return:
"""
self.entities[self.entity_id] = entity
entity.id = self.entity_id
self.entity_id += 1
def remove_entity(self, entity):
"""
Remove the entity from the world.
:param entity: The entity instance to be removed
:return:
"""
del self.entities[entity.id]
def get(self, entity_id):
"""
Find the entity, given its id (or None if it is not found).
:param entity_id: The ID of the entity
:return:
"""
if entity_id in self.entities:
return self.entities[entity_id]
else:
return None
def process(self, time_passed):
"""
Process every entity in the world.
:param time_passed: Time passed since the last render
"""
time_passed_seconds = time_passed / 1000.0
for entity in self.entities.values():
entity.process(time_passed_seconds)
def render(self, surface):
"""
Draw the background and all the entities.
:param surface: The pygame surface
"""
surface.blit(self.background, (0, 0))
for entity in self.entities.itervalues():
entity.render(surface)
def get_close_entity(self, name, location, distance_range=100.):
"""
Find an entity within range of a location.
:param name: Name of the entity
:param location: location
:param distance_range: The circular distance of the range (circular "field of view")
"""
location = Vector2(*location)
for entity in self.entities.itervalues():
if entity.name == name:
distance = location.get_distance_to(entity.location)
if distance < distance_range:
return entity
return None
def run():
pygame.init()
screen = pygame.display.set_mode(SCREEN_SIZE, 0, 32)
pygame.display.set_caption('Ant Simulation')
world = World()
w, h = SCREEN_SIZE
clock = pygame.time.Clock()
ant_image = pygame.image.load("ant.png").convert_alpha()
leaf_image = pygame.image.load("leaf.png").convert_alpha()
spider_image = pygame.image.load("spider.png").convert_alpha()
for ant_no in xrange(ANT_COUNT):
ant = Ant(world, ant_image)
ant.location = Vector2(randint(0, w), randint(0, h))
ant.brain.set_state("exploring")
world.add_entity(ant)
full_screen = False
while True:
for event in pygame.event.get():
if event.type == QUIT:
return
if event.type == KEYDOWN:
if event.key == K_f:
full_screen = not full_screen
if full_screen:
screen = pygame.display.set_mode(SCREEN_SIZE, FULLSCREEN, 32)
else:
screen = pygame.display.set_mode(SCREEN_SIZE, 0, 32)
time_passed = clock.tick(30)
if randint(1, 10) == 1:
leaf = Leaf(world, leaf_image)
leaf.location = Vector2(randint(0, w), randint(0, h))
world.add_entity(leaf)
if randint(1, 100) == 1:
# Make a 'dead' spider image by turning it upside down
spider = Spider(world, spider_image, pygame.transform.flip(spider_image, 0, 1))
spider.location = Vector2(-50, randint(0, h))
spider.destination = Vector2(w+50, randint(0, h))
world.add_entity(spider)
world.process(time_passed)
world.render(screen)
pygame.display.update()
if __name__ == "__main__":
run()