为了方便喜欢Python的同学能使用Python开发自己的球队,所以编写了此SDK。这个SDK 基本上是参照C#版SDK改过来的,除了一些复杂的几何算法没有实现外,其他功能都已实现。喜欢的朋友可以自己下了慢慢改善,我也会不断更新这个SDK。 下面介绍一下基本的使用吧。
一、导入soccer模块
不需要过多的import导入语句,轻轻松松,简简单单,只需要一句导入语句:
from soccer import *
二、模块介绍
1. 导入soccer后,我们可以使用如下的一些实例对象:
field_settings #球场设置信息
game_settings #比赛设置信息
rule_settings #规则设置信息
server_settings #服务器设置信息
communicator #通信对象
game_settings #比赛设置信息
rule_settings #规则设置信息
server_settings #服务器设置信息
communicator #通信对象
2. 类对象如下:
Vector2f #二维坐标
GameState #比赛状态(比分信息)
ClientInfo #球队信息(球队名,作者)
GameState #比赛状态(比分信息)
ClientInfo #球队信息(球队名,作者)
命令相关的对象及常量如下:
Command #命令
CommandType_Catch = 'Catch' #扑球
CommandType_Dash = 'Dash' #跑
CommandType_Turn = 'Turn' #转身
CommandType_Stay ='Stay' #原地不动
CommandType_Kick = 'Kick' #踢球
CommandType_Unknow = 'Unknow' #未知命令
CommandType_Catch = 'Catch' #扑球
CommandType_Dash = 'Dash' #跑
CommandType_Turn = 'Turn' #转身
CommandType_Stay ='Stay' #原地不动
CommandType_Kick = 'Kick' #踢球
CommandType_Unknow = 'Unknow' #未知命令
所有实现了的类和C#版SDK基本一致,比如Vector2f的操作符重载等。上面列的是主要的一些类和对象,除此之外还包括比如一些角度计算的模块(anglehelper),矩形对象(rectangle) 等。
三、创建球队实例
创建方法和C#版本基本一样,下面的代码应该不需要过多解释:
if __name__ == "__main__":
"""
entry point of the great team!
"""
myteam = TeamNancy('NancyTeam', 'CoderZh')
if communicator.connect(myteam.info):
print 'Platform Connected!!!'
while True:
"""
Start the game cycle
"""
wmdata = communicator.getworldmodel() #get the infomation from the server
if (wmdata == None):
print 'Game Over!'
exit()
#get the infomation of the game to my team
myteam.getworldmodel(wmdata)
#my team think for a while and send the commands to the server
communicator.send_commands(myteam.think())
else:
print 'Connect Fail!!!'
"""
entry point of the great team!
"""
myteam = TeamNancy('NancyTeam', 'CoderZh')
if communicator.connect(myteam.info):
print 'Platform Connected!!!'
while True:
"""
Start the game cycle
"""
wmdata = communicator.getworldmodel() #get the infomation from the server
if (wmdata == None):
print 'Game Over!'
exit()
#get the infomation of the game to my team
myteam.getworldmodel(wmdata)
#my team think for a while and send the commands to the server
communicator.send_commands(myteam.think())
else:
print 'Connect Fail!!!'
再来看看如何创建自己的AI球队的类:
class TeamNancy(object):
def __init__(self, teamname, author):
self.info = ClientInfo(teamname, author)
self.wm = WorldModel()
self.cmds = [Command() for i in range(5)]
def getworldmodel(self, wmdata):
self.wm = wmdata
def think(self):
for i in range(rule_settings.AgentNum):
temppos = self.wm.ball.pos - self.wm.MyAgents[i].pos
if temppos.getlength() < rule_settings.MaxKickBallDistance:
self.cmds[i].type = CommandType_Kick
self.cmds[i].param1 = 1.0
self.cmds[i].param2 = 0
elif math.fabs(temppos.getdirection() - self.wm.MyAgents[i].dir) < 2:
self.cmds[i].type = CommandType_Dash
self.cmds[i].param1 = rule_settings.MaxDashVel
else:
self.cmds[i].type = CommandType_Turn
self.cmds[i].param1 = temppos.getdirection()
return self.cmds;
def __init__(self, teamname, author):
self.info = ClientInfo(teamname, author)
self.wm = WorldModel()
self.cmds = [Command() for i in range(5)]
def getworldmodel(self, wmdata):
self.wm = wmdata
def think(self):
for i in range(rule_settings.AgentNum):
temppos = self.wm.ball.pos - self.wm.MyAgents[i].pos
if temppos.getlength() < rule_settings.MaxKickBallDistance:
self.cmds[i].type = CommandType_Kick
self.cmds[i].param1 = 1.0
self.cmds[i].param2 = 0
elif math.fabs(temppos.getdirection() - self.wm.MyAgents[i].dir) < 2:
self.cmds[i].type = CommandType_Dash
self.cmds[i].param1 = rule_settings.MaxDashVel
else:
self.cmds[i].type = CommandType_Turn
self.cmds[i].param1 = temppos.getdirection()
return self.cmds;
四、下载SDK
https://files.cnblogs.com/coderzh/SoccerSDK.rar
五、感谢
感谢 逖靖寒 同学给我们带来了那么好玩的游戏,丰富了我们的生活,带来了很多乐趣。同时希望此Python版SDK能给同学们带来一些帮助,也希望同学们提出宝贵意见,不断的完善这个SDK。谢谢!!