Lua的初探学习

来源:互联网 发布:风火轮软件 编辑:程序博客网 时间:2024/06/05 00:16
Lua语言开发  lua.org

**LuaInterface**
通过LuaInterface完成Lua和C#之间的互相调用


lua解析工具
1.ulua      ulua.org
2.Nlua     nlua.org
3.UniLua
4.sLua

lua安装
A.Luaforwindows
B.luaer.cn  lua开发者
C.luaforge.net


lua的编写
1.安装luaforwindows
2.使用SciTE.exe 编写代码


lua语言(不需要以;结尾)
1.字符串用"" 或 ‘’ 均可表示  如: print("hello world",‘hello world’)
2.变量不要定义类型,直接赋值   如:age=100   name="Jane"   isMan=false
3.单行注释:  --        多行注释:  --[[  注释内容  ]]-- 
4.nil表示空,等同C#中的null
5.lua没有整数类型,只有小数类型


6.lua的五种变量类型:
  ①空数据
  ②boolean布尔类型
  ③string字符串类型
  ④number小数类型
  ⑤table表类型


7.可以用type()直接获得一个变量的类型


8.table:  myTable={34,134,1344,1354,6}
           myTable[1]=34
  lua的索引都从1开始


9.lua中所有默认定义的变量都是全局变量
  局部变量的定义需在变量前加local  local name="law"


10.运算符
①算数运算符  + - * / % (lua中没有++ -- 这样的运算符)
②关系运算符 <= < > >= ==
③逻辑运算符 and or not 分别表示 与 或 非(类似于C#的 && || !)
  and  = &&  or = ||  not = !


11.if逻辑
1.if 条件 then
    执行逻辑
  end


2.if 条件 then
    执行逻辑
  else
    执行逻辑
  end


3.if 条件 then
    执行逻辑
  elseif 条件 then
    执行逻辑
  else
    执行逻辑
  end




如:
  hp = 60
  if hp<50 then
    print("血量少于50")
  elseif hp<0 then
    print("血量少于0")
  else
    print("血量大于50")
  end


12.while循环
   while 条件 do
     执行逻辑
   end


13.repeat循环
   repeat
     执行逻辑
   until 条件
   (当条件满足时跳出循环)


14.for循环
   for index = [start],[end] do
      执行逻辑
   end
   (break结束循环,但没有C#中continue语法)


如:for index=1,100 do
     print(index)




15.函数定义
   function 方法名(参数一,参数二)
     执行逻辑
   end
   
如:function Plus(num1,num2)
       return num1+num2
    end


16.lua内置的常用函数(自行查找lua开发文档)
①数学处理的math相关函数
②字符串处理的string相关函数
③表处理的table相关函数
④文件操作的io相关函数


如:
print(math.abs(-100))
print(math.max(1,2,3,5,54,5))


17.string字符串
   .. 表示字符串的相加
   tostring() 把一个数字转化为字符串
   tonumber() 把一个字符串转化为数字


如:
 one = "abc"
 two = "def"
 english = one..two
 print(english)


18.table表:键值对


表的长度=table.getn(表名)


赋值方法:


①纯数字键
myTable = {12,535,1,41,58,4,8}


②非纯数字键


--定义一个空表
myTable ={}


myTable.name = "表的第一个值"
mytable["age"] = "表的第二个值"
myTable.num = 3



遍历方法:


①纯数字键遍历
for index=1,table.getn(表名) do
  运行逻辑
end



②所有表遍历
for index,value in pairs(表名)do
  运行逻辑
  print(index,value)
end



*****通过表来实现面向对象*************


Enemy={} --声明怪物对象
local this = Enemy --声明this为怪物对象


--声明对象属性
this.name="BigBoss"
this.hp =100


--声明对象中的方法
①this.SayHello = function()
    print("Hello,My name is"..this.name)
  end


②function Enemy.attack()
    this.SayHello
    print("我在打你!")
  end


19.通过LuaInterface完成Lua和C#之间的互相调用



0 0
原创粉丝点击