页游开发中的Python组件与模式Pythoncomponentsandpatternsinpagetraveldevelopment上课讲义

上传人:yuzo****123 文档编号:139496468 上传时间:2020-07-22 格式:PPT 页数:56 大小:711.50KB
返回 下载 相关 举报
页游开发中的Python组件与模式Pythoncomponentsandpatternsinpagetraveldevelopment上课讲义_第1页
第1页 / 共56页
页游开发中的Python组件与模式Pythoncomponentsandpatternsinpagetraveldevelopment上课讲义_第2页
第2页 / 共56页
页游开发中的Python组件与模式Pythoncomponentsandpatternsinpagetraveldevelopment上课讲义_第3页
第3页 / 共56页
页游开发中的Python组件与模式Pythoncomponentsandpatternsinpagetraveldevelopment上课讲义_第4页
第4页 / 共56页
页游开发中的Python组件与模式Pythoncomponentsandpatternsinpagetraveldevelopment上课讲义_第5页
第5页 / 共56页
点击查看更多>>
资源描述

《页游开发中的Python组件与模式Pythoncomponentsandpatternsinpagetraveldevelopment上课讲义》由会员分享,可在线阅读,更多相关《页游开发中的Python组件与模式Pythoncomponentsandpatternsinpagetraveldevelopment上课讲义(56页珍藏版)》请在金锄头文库上搜索。

1、页游开发中的 Python 组件与模式,赖勇浩() 2012-10-21,去年我来过,回顾,幻灯: 录像(上海45分钟版): 录像(广州91分钟版): 偏向于“最佳实践”的经验分享,今天不一样,有什么问题?,def do_sth_1(. def do_sth_2(. def do_sth_3(. def do_sth_4(. ,一般这样解决掉,ensure_signin def do_sth(self, *a, *kw): Decorator !,还有什么问题?,def do_sth_1(. def do_sth_2(. def do_sth_3(. def do_sth_4(. ,if no

2、t self._signin: . if not self._in_battle: . if not self._is_dead: . .,还是这样解决掉?,ensure_signin ensure_in_battle ensuer_is_alive def do_sth(self, *a, *kw): ?,好像哪里不对,戴太多“帽子”不好看 method 的数量没有减少。 需要一点新思路!,python-state,stateful class Player(object): class NeedSignin(State): default = True behavior def signi

3、n(self, usr, pwd): . switch(self, Player.Signin),class Signin(State): behavior def move(self, dst): . behavior def atk(self, other): . behavior def ,python-state,stateful class Player(object): class NeedSignin(State): default = True behavior def signin(self, usr, pwd): . switch(self, Player.Signin),

4、class Signin(State): behavior def move(self, dst): . behavior def atk(self, other): . behavior def ,python-state,stateful class Player(object): class NeedSignin(State): default = True behavior def signin(self, usr, pwd): . switch(self, Player.Signin),class Signin(State): behavior def move(self, dst)

5、: . behavior def atk(self, x): . behavior def ,python-state,stateful class Player(object): class NeedSignin(State): default = True behavior def signin(self, usr, pwd): . switch(self, Player.Signin),class Signin(State): behavior def move(self, dst): . behavior def atk(self, other): . behavior def ,py

6、thon-state,stateful class Player(object): class NeedSignin(State): default = True behavior def signin(self, usr, pwd): . switch(self, Player.Signin),class Signin(State): behavior def move(self, dst): . behavior def atk(self, other): . behavior def ,适用场景,根据状态授权特定的 RPC API 访问权限 例如未登陆不能调用攻击 编写网络协议、文本的

7、parser FSM-based Game AI ? No ! 没有比协程更适合做这件事的机制了。,小结一下,跟 decorator 一样去掉了 if 语句 但是摘掉了许多帽子 而且真正地没有写 if 语句噢!,小结一下,跟 decorator 一样去掉了 if 语句 但是摘掉了许多帽子 而且真正地没有写 if 语句噢! 把很多 method 分到多个 State 类中 重用 划分功能,更大的好处是,更容易修正错误 因为出错信息是 AttributeError !,更容易修正错误,因为出错信息是 AttributeError !, 2010-5-30 shanghai,更容易修正错误,因为出错

8、信息是 AttributeError !,好,继续!,什么引起状态的切换?,属性变化!,什么引起属性变化?,有事情发生!,怎么知道有事情发生?,上网、读报、看电视、听收音机,大海捞针,好莱坞原则,Dont call me, Ill call you.,一个简单的状态切换场景,建个模吧!,建个模吧!,class Lamp(object): is_on = False class Swith(object): def turn(self): .,建个模吧!,class Lamp(object): is_on = False class Swith(object): def turn(self):

9、self.line.lamp.is_on = True,class Line(object): . line = Line() lamp = Lamp(line) swith = Swith(line) liine.connect(lamp, swith) swith.turn() assert lamp.is_on,好像感觉哪里不对,现实世界中需要真实的 Line,编程中也是吗? 如果一个 Switch 对应着很多 Lamp ? 循环引用? 隐形的 Line?可扩展的 Line?,python-message,class Switch(object): Turn = state.exampl

10、es.Switch.Trun def turn(self): message.pub(Switch.Turn, self),python-message,stateful class Lamp(object): class Off(State): default = True behavior def _on_turn(self, s): switch(self, Lamp.On) class On(State): behavior def _on_turn(self, s): switch(self, Lamp.Off),class Lamp(object): def bind(self,

11、s): self._switch = s message.sub(Switch.Turn, self.on_turn) def on_turn(self, s): self._on_turn(s),python-message,s = Switch() l = Lamp() l.bind(s) s.turn() s.turn(), begin Off state. end Off state. begin On state. end On state. begin Off state.,解耦,应用场景,任务 获得道具时 怪物死亡时 世界状态 玩家(好友)上下线 网络编程 数据可读 ,更多功能,

12、取消与中止,import message def hello(name): print hello, %s.%name message.unsub(greet, hello) message.sub(greet, hello) message.pub(greet, lai) message.pub(greet, u cannt c me.),import message def hello(name): print hello %s % name ctx = message.Context() ctx.discontinued = True return ctx def hi(name): p

13、rint u cannt c me. message.sub(greet, hello) message.sub(greet, hi) message.pub(greet, lai),进阶,改变调用次序 在调用 sub 的时候加上 front = True sub(greet, hello, front = True) 订阅过去的消息 declare/retract declare(topic, *a, *kw) 用来向“公告栏”发布一个消息 把“公告栏”的消息撤消用 retract(topic) 函数 get_declarations()/has_declaration(topic),退化观

14、察者模式,from message import observable def greet(people): print hello, %s.%people.name observable class Foo(object): def _init_(self, name): self.name = name self.sub(greet, greet) foo = Foo(lai) foo.pub(greet, foo),现在玩家有了状态也能知晓世事变幻,那玩家们如何交互?,网络通信,需要有个 rpc 基于 google protobuf 实现一套就不错 如果有 greenlet 实现同步编程

15、就更好了,abu.rpc,class EchoService(echo.EchoService): abu.rpc.ret def Echo(self, controller, request): resp = echo.Packet(text = request.text) return resp service = EchoService() handler = abu.rpc.Handler(abu.rpc.Transport, service) server = gevent.server.StreamServer(, 10086), handler) app = abu.rpc.Ap

16、plication(server) print serving. app.run(),abu.rpc,conn = gevent.socket.create_connection(127.0.0.1, 10086) channel = abu.rpc.Channel(abu.rpc.Transport(conn) server = abu.rpc.Proxy(echo.EchoService_Stub(channel) req = echo.Packet(text = hello*30) resp = server.Echo(req) assert resp.text = req.text,放心,不是 abu.rpc 教程!,讲 abu.rpc 在真实应用场景中遇到的问题及解决方法。,这世界太复杂了,抽象传输层,message.observable class Transport(obje

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 教学课件 > 高中课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号