用Pygame写游戏-从入门到精通14

用Pygame写游戏-从入门到精通14

• 当前正在做什么
• 转化到下一件事时候的条件

游戏实体类

``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 class GameEntity(object): def __init__(self, world, name, image): self.world = world self.name = name self.image = image self.location = Vector2(0, 0) self.destination = Vector2(0, 0) self.speed = 0. self.brain = StateMachine() self.id = 0 def render(self, surface): x, 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 &gt; 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 ```

render函数是用来绘制自己的。

process函数首先调用self.brain.think这个状态机的方法来做一些事情（比如转身等）。接下来的代码用来让实体走近目标。

世界类

``` 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 class World(object): def __init__(self): self.entities = {} # Store all the entities self.entity_id = 0 # Last entity id assigned # 画一个圈作为蚁穴 self.background = pygame.surface.Surface(SCREEN_SIZE).convert() self.background.fill((255, 255, 255)) pygame.draw.circle(self.background, (200, 255, 200), NEST_POSITION, int(NEST_SIZE)) def add_entity(self, entity): # 增加一个新的实体 self.entities[self.entity_id] = entity entity.id = self.entity_id self.entity_id += 1 def remove_entity(self, entity): del self.entities[entity.id] def get(self, entity_id): # 通过id给出实体，没有的话返回None if entity_id in self.entities: return self.entities[entity_id] else: return None def process(self, time_passed): # 处理世界中的每一个实体 time_passed_seconds = time_passed / 1000.0 for entity in self.entities.itervalues(): entity.process(time_passed_seconds) def render(self, surface): # 绘制背景和每一个实体 surface.blit(self.background, (0, 0)) for entity in self.entities.values(): entity.render(surface) def get_close_entity(self, name, location, range=100.): # 通过一个范围寻找之内的所有实体 location = Vector2(*location) for entity in self.entities.values(): if entity.name == name: distance = location.get_distance_to(entity.location) if distance &lt; range: return entity return None ```

PythonTab微信公众号:

Python技术交流互助群 ( 请勿加多个群 ):