您的位置:首页 > 大数据 > 人工智能

[Python]游戏编程--人工智能2

2018-01-28 11:37 489 查看

游戏中的人工智能2

该系列博客都是在学习《making game s with python and pygame》和一个特别好的博主的博客系列中学习的。

‘Festinatione facit vastum’

环境

Win10(神奇的我给kali装了Nivdia驱动,崩了)

Python2.7 or Python3.6

Pycharm

昆虫—我们最常用的建模对象(之一)

尽管我们是对昆虫建模的,这段代码对很多场景都是合适的。把它们替换为巨大的机器人守卫(蜘蛛)、坦克(蚂蚁)、能源(叶子),这段代码依然能够很好的工作。

游戏的实体类

实体(entity英[ˈentəti]美[ˈɛntɪti])实体(entity英[ˈentəti]美[ˈɛntɪti])

在这个时候有三个实体(Ant,Spider,Leaf),这时候我们就要发挥一下—尝试着去写一个通用的实体基类,就可以了,如果加入了其他实体,也可以很方便的拓展出来。

一个实体需要储存:

名字

现在的位置

目标

速度

图形(外观)

但是有些实体可能只有一部分属性(比如叶子应该bia~在那里不动,所以速度为0吧),同时我们还需要准备进入退出的函数以供调用。给出一个完整的GameEntity类:

class GameEntity(object):
"""游戏实体基类"""
def __init__(self, world, name, image):
"""
属性分别有:
所在世界,
名字,
图形,
位置,
目的地,
速度,
大脑(之前举例的身体),
id
"""
self.world = world
self.name = name
self.image = image

c416
self.location = Vector2(0, 0)
self.destination = Vector2(0, 0)
self.speed = 0.
self.brain = StateMachine()
self.id = 0

def render(self, surface):
"""传递他的位置,图形大小给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 > 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


观察这个类,会发现它还保存了一个world,这是对外界的其中的一个类的引用,否则实体是无法知道外界的信息。这里类还有一个id,用来标示自己,他还有一个brain,就是我们之后会定义的一个状态机类。

rander函数我们用来绘制自己

process函数首先调用self.brain.think这个状态机的方法来做一些事情(比如转身等…)。接下来的codes是让实体走进目标。

世界类

我们有了一个GameObject的实体类。这里再有一个世界类World用来描述外界。我们的世界不需要多复杂,仅仅要一个蚂蚁的巢穴,和储存若干个实体位置就足够了:

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 < range:
return entity
return None


优化:因为我们有很多的GameObject,使用一个列表来储存及时很自然的事情。不过如果实体增加,搜索列表就会变得缓慢,所以我们使用了字典来存储。(打开我的Sai):



这两个类还不足以构筑我们的昆虫世界,但是却是整个模拟的基础,下一次我们就要讲述实际的蚂蚁类和大脑(状态机类)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息