设为首页 加入收藏

TOP

x01.AntWorld: An Python AI Game(二)
2017-11-20 08:34:54 】 浏览:261
Tags:x01.AntWorld: Python Game
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 =
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇pandas选择数据-【老鱼学pandas】 下一篇docker创建镜像发布到远端仓库

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目