y = self.location
w,h = self.image.get_size()
surface.blit(self.image, (x-w/2, y-h/2))
def process(self, time_passed):
self.brain.think()
if self.speed > 0 and self.location != self.destination:
vec_to_destination = self.destination - self.location
distance_to_destination = vec_to_destination.get_length()
heading = vec_to_destination.get_normalized()
travel_distance = min(distance_to_destination, time_passed * self.speed)
self.location += travel_distance * heading
class Leaf(GameEntity):
def __init__(self, world, image):
GameEntity.__init__(self, world, "leaf", image)
class Spider(GameEntity):
def __init__(self, world, image):
GameEntity.__init__(self, world, "spider", image)
self.dead_image = pygame.transform.flip(image, 0, 1)
self.health = 25
self.speed = 50.0 + randint(-20,20)
def bitten(self):
self.health -= 1
if self.health <= 0:
self.speed = 0
self.image = self.dead_image
self.speed = 140
def render(self, surface):
GameEntity.render(self, surface)
x,y = self.location
w,h = self.image.get_size()
bar_x = x - 12
bar_y = y + h/2
surface.fill((255,0,0), (bar_x, bar_y, 25, 4))
surface.fill((0,255,0), (bar_x, bar_y, self.health, 4))
def process(self, time_passed):
x,y = self.location
if x > ScreenSize[0] + 2:
self.world.remove_entity(self)
return
GameEntity.process(self, time_passed)
class Ant(GameEntity):
def __init__(self, world, image):
GameEntity.__init__(self, world, "ant", image)
exploring_state = AntStateExploring(self)
seeking_state = AntStateSeeking(self)
delivering_state = AntStateDelivering(self)
hunting_state = AntStateHunting(self)
self.brain.add_state(exploring_state)
self.brain.add_state(seeking_state)
self.brain.add_state(delivering_state)
self.brain.add_state(hunting_state)
self.carry_image = None
def carry(self, image):
self.carry_image = image
def drop(self, surface):
if self.carry_image:
x,y = self.location
w,h = self.carry_image.get_size()
surface.blit(self.carry_image, (x-w, y-h/2))
self.carry_image = None
def render(self, surface):
GameEntity.render(self, surface)
if self.carry_image:
x,y = self.location
w,h = self.carry_image.get_size()
surface.blit(self.carry_image, (x-w, y-h/2))
class AntStateExploring(State):
def __init__(self, ant):
State.__init__(self, "exploring")
self.ant = ant
def random_destination(self):
w,h = ScreenSize
self.ant.destination = Vector2D(randint(0, w), randint(0, h))
def do_actions(self):
if randint(1,20) == 1:
self.random_destination()
def check_conditions(self):
leaf = self.ant.world.got_close_entity("leaf", self.ant.location)
if leaf is not None:
self.ant.leaf_id = leaf.id
return "seeking"
spider = self.ant.world.got_close_entity("spider", NestLocation, NestSize)
if spider is not None:
if self.ant.location.get_distance_to(spider.location) < 100:
self.ant.spider_id = spider.id
return "hunting"
return None
def entry_actions(self):
self.ant.speed = 120 + randint(-30, 30)
self.random_destination()
class AntStateSeeking(State):
def __init__(self, ant):
State.__init__(self, "seeking")
self.ant = |