最近工作中碰到一个需求,需要对游戏脚本中的一些数据统一进行处理。
于是就想用学python来搞定之。
一、python入门:
python很强大,官方简介:Python是一种简单易学,功能强大的编程语言,它有高效率的高层数据结构,简单而有效地实现面向对象编程。Python简洁的语法和对动态输入的支持,再加上解释性语言的本质,使得它在大多数平台上的许多领域都是一个理想的脚本语言,特别适用于快速的应用程序开发。
正适合我的需求。
学习一门新的语言,一个好的入门教程很关键,比如我看的这个:简明Python教程;
安装、helloworld等我就不介绍了,上面这个教程写得清清楚楚。
另外,还需要参阅官方文档更为重要,python的文档一般在安装目录下的doc文件夹下。【windows下2.5版本的文档是python25.chm】
比如,这次需求主要涉及到了处理文本,里面用到了常见的几个命令:split,join,count,str,int等,
新手往往很困惑,最恰当的方法不是问度娘,问谷哥,而是查看官方文档。
二、需求:
Aurora的关卡设计文件(以.level结尾)中,会有些x,y坐标,在移植的时候需要修改
(比如从N73大屏幕移植到N7610小屏幕,sprite、地图等图片按比例缩小,则关卡设计中的xy也必须相应等比例缩小)
第一种:普通的actor层:POS x y
LAYER OBJECT_LAYER "itlay_huanying" { FLAGS 0x0000000B ID 68 POS -25 -21 TEMPLATE "hero_aizi" PARAMS 6 0 0 0 1 }
LAYER OBJECT_LAYER "garmany_yuren" { FLAGS 0x0000000B ID 69 POS 7 -18 TEMPLATE "hero_pangzi" PARAMS 13 0 0 0 1 }
LAYER OBJECT_LAYER "yaoming2" { FLAGS 0x0000000B ID 71 POS 646 473 TEMPLATE "hero_pangzi" PARAMS 18 0 0 0 1 }
第二种:Cinematic中:SetPos(x, y)
如下镜头的xy设置:
THREAD CAMERA
{
NAME "Camera"
FLAGS 0x00000001
VIEW_FLAGS 0x00000000
TIME +35 //40
SetPos(395, 0)
但是,如下Thread Basic中的SetPos的特殊命令则不用修改。。
THREAD BASIC
{
NAME "Basic"
FLAGS 0x00000001
VIEW_FLAGS 0x00000000
TYPE 0
TIME +26 //26
SetPos(-3, -2)
SetAction(1)
三、问题总结:
碰到了两个问题:
1.全局变量:在函数中如果要修改全局变量,需要在函数开始地方设定标识:global xxx
2.python是个代码结构比较严谨的语言,需要缩进,而我用Notepad++写python代码,用的是制表键的缩进,而不是用空格,则会造成各种各样乱七八糟的问题。
比如这个:
IndentationError: unindent does not match any outer indentation level
又比如这个:
SyntaxError: invalid syntax
各种莫名其妙。
在StackOverflow上搜了一下,即发现问题所在。参看以下Q&A:
PS:
在网上查看问题答案时候,发现了这么个有意思的网站:PyGame
由python写的一个游戏引擎,这个网站上还放了很多由这个引擎开发的游戏,比如这个:Ulmo的冒险
四、以下附上最终代码:
#remove file line end spaces
#if set folder to process: Find all java files in that folder and process they
#or set a single file to process
import sys, os
g_add = "+"
g_mul = "*"
g_div = "/"
g_param_add = 0
g_param_mul = 1
g_param_div = 1
def GetArgv(index):
#需要设置全局变量global,否则相当于又设置了局部变量g_param_add等。。。
global g_param_add
global g_param_mul
global g_param_div
g_param_add=int(sys.argv[index])
g_param_mul=int(sys.argv[index + 1])
g_param_div=int(sys.argv[index + 2])
if(g_param_div == 0):
g_param_div = 1
print "add", g_param_add
print "mul", g_param_mul
print "div", g_param_div
def amd(value):
ret = int(value)
#print "add", g_param_add
#print "mul", g_param_mul
#print "div", g_param_div
ret = (ret + g_param_add) * g_param_mul / g_param_div
return str(ret)
def amdFile(file_path):
if not os.path.exists(file_path):
print """Remove file end spaces Error:The file "%s" doesn't exits""" %(file_path)
return False
if not os.path.isfile(file_path):
print """Remove file end spaces Error:The "%s" isn't a file.""" %(file_path)
return False
hasAnyChange = False
file = open(file_path, 'r+')
out = open(file_path+"~", 'w')
###POS x y
###LAYER OBJECT_LAYER "zhuangshi_NBA" { FLAGS 0x0000000B ID 5 POS 319 182 TEMPLATE "zhuangshi_NBA" PARAMS 0 1 0 0 }
lines = file.readlines()
for line in lines:
twoParts = line.split("POS ")
if len(twoParts)==1:
#print line
out.write(line)
else:
#print "-------------cur split line-------------"
#print line
###319 182 TEMPLATE "zhuangshi_NBA" PARAMS 0 1 0 0 }
#print twoParts[1]
xys = twoParts[1].split()
#print xys
xys[0] = str(amd(xys[0]))
xys[1] = str(amd(xys[1]))
twoParts[1] = " ".join(xys)
#print xys
line = twoParts[0] + "POS " + twoParts[1] + "\n"
#print line
out.write(line)
hasAnyChange = True
###SETPOS(x, y)
file.close()
out.close()
if hasAnyChange:
os.remove(file_path)
os.rename(file_path+"~", file_path)
else:
os.remove(file_path+"~")
###SetPos(32, 13)
isBasicThread = False
file = open(file_path, 'r+')
out = open(file_path+"~", 'w')
lines = file.readlines()
for line in lines:
#twoParts = line.split("THREAD")
if line.count("THREAD")==0:
#not thread
#SetPos(23, 12)
if line.count("SetPos")!=0:
if not isBasicThread:
twoParts1 = line.split(", ")
twoParts20 = twoParts1[0].split("(")
twoParts21 = twoParts1[1].split(")")
x = amd(twoParts20[1])
y = amd(twoParts21[0])
line = twoParts20[0] + "(" + x + ", " + y + ")\n"
out.write(line)
hasAnyChange = True
else:
#THREAD BASIC
if line.count("THREAD BASIC")==0:
isBasicThread = False
else:
isBasicThread = True
out.write(line)
###SETPOS(x, y)
file.close()
out.close()
if hasAnyChange:
os.remove(file_path)
os.rename(file_path+"~", file_path)
else:
os.remove(file_path+"~")
return True
def amdFiles(folder,type):
if not os.path.exists(folder):
print """Remove file end spaces Error:The folder "%s" doesn't exits""" %(folder)
return False
files = os.listdir(folder)
#print files
for file_name in files:
file_path = folder + os.sep + file_name
if os.path.isfile(file_path) and file_path.endswith(type) :
print "file:%s" %(file_path)
amdFile(file_path)
#print 'done'
return True
if __name__=="__main__":
if len(sys.argv)!=5 and len(sys.argv)!=6:
print 'Usage: AddMulDivXY folderName add mul div'
print 'Usage: AddMulDivXY filePath -f add mul div'
elif len(sys.argv)==6:
GetArgv(3)
if not amdFiles(sys.argv[1], 'level'):
sys.exit(1)
print "AddMulDivXY %s OK!" %(sys.argv[1])
else:
GetArgv(2)
if not amdFile(sys.argv[1]):
sys.exit(1)
print "AddMulDivXY %s OK!" %(sys.argv[1])