您的位置:首页 > 编程语言 > Python开发

用Python和Pygame写游戏-从入门到精通(14)

2014-08-14 14:56 399 查看

用Python和Pygame写游戏-从入门到精通(14)

在继续我们的AI之旅前,分享一个在煎蛋上看到的有趣新闻,能通过读说明书来学习的AI,这个世界真是变得越来越不可琢磨啦!机器人很快就要超越咱了……



因为这一次是接着上面的内容的,所以请不要跳过直接看这里。

哭!!!写完了上传出错,丢失啊,重伤重写~~~~~~~~~~~

蚂蚁实例类

在我们正式建造大脑之前,我们得先做一个蚂蚁类出来,就是下面的这个,从GameEntity继承而来:

Python

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):
# 放下carry图像
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):
# 先调用基类的render方法
GameEntity.render(self, surface)
# 额外绘制carry_image
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))

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

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):

# 放下carry图像
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):
# 先调用基类的render方法

GameEntity.render(self,
surface)
# 额外绘制carry_image

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))

这个Ant类先调用了父类的__init__,都是Python基础不多说了。下面的代码就是一些状态机代码了,对了还有一个carry_image变量,保持了现在蚂蚁正在搬运物体的图像,或许是一片树叶,或许是一只死蜘蛛。这里我们写了一个加强的render函数,因为我们可能还需要画一下搬的东西。

建造大脑

我们给每一只蚂蚁赋予四个状态,这样才能足够建造我们的蚂蚁的状态机。在建造状态机之前,我们得先把这些状态的详细信息列出来。

状态动作
探索(Exploring)随机的走向一个点
搜集(Seeking)向一篇树叶前进
搬运(Dellivering)搬运一个什么回去
狩猎(Hunting)攻击一只蜘蛛
我们也需要定义一下各个状态之间的链接,或者可以叫转移条件。这里举两个例子(实际上不止):

条件转移状态
发现树叶搜集
有蜘蛛攻击狩猎
我们还是最终画一张图来表示整个状态机:



高水平的你也许可以看着上面的图写状态机了,不过为了方便先建立一个State类,来保存一个状态。很简单,只是一个框子,实际上什么都不做:

Python

class State():
def __init__(self, name):
self.name = name
def do_actions(self):
pass
def check_conditions(self):
pass
def entry_actions(self):
pass
def exit_actions(self):
pass

1
2
3
4
5
6
7
8
9
10
11

class
State():

def
__init__(self,
name):
self.name
= name

def
do_actions(self):
pass

def
check_conditions(self):
pass

def
entry_actions(self):
pass

def
exit_actions(self):
pass

然后可以建立一个状态机类来管理这些状态,这个状态机可是整个代码的核心类。

Python

class StateMachine():
def __init__(self):
self.states = {} # 存储状态
self.active_state = None # 当前有效状态
def add_state(self, state):
# 增加状态
self.states[state.name] = state
def think(self):
if self.active_state is None:
return
# 执行有效状态的动作,并做转移检查
self.active_state.do_actions()
new_state_name = self.active_state.check_conditions()
if new_state_name is not None:
self.set_state(new_state_name)
def set_state(self, new_state_name):
# 更改状态,执行进入/退出动作
if self.active_state is not None:
self.active_state.exit_actions()
self.active_state = self.states[new_state_name]
self.active_state.entry_actions()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

class
StateMachine():

def
__init__(self):
self.states
= {} # 存储状态

self.active_state
= None # 当前有效状态
def
add_state(self,
state):

# 增加状态
self.states[state.name]
= state

def
think(self):
if
self.active_state
is None:

return
# 执行有效状态的动作,并做转移检查

self.active_state.do_actions()
new_state_name
= self.active_state.check_conditions()

if
new_state_name is
not None:
self.set_state(new_state_name)

def
set_state(self,
new_state_name):
# 更改状态,执行进入/退出动作

if
self.active_state
is not
None:
self.active_state.exit_actions()

self.active_state
= self.states[new_state_name]
self.active_state.entry_actions()

然后就可以通过继承State创建一系列的实际状态了,这些状态传递给StateMachine保留并运行。StateMachine类的think方法是检查当前有效状态并执行其动作的,最后还可能会调用set_state来进入下一个状态。

我们应该根据上面的四个状态表格建立状态了,有鉴于本次的代码已经很多了,需要好好消化…… 下一次将会一次性给出完整代码,到时候就能看到一个丰富多彩的昆虫世界了!尽请期待~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: