荣耀彩票代理

  • 热门专题

游戏开发 小白学Lua(上)

作者:马三小伙儿  发布日期:2016-11-14 20:24:04
  •   ZAIHENDUOYOUXIZHONG,JIAOBENYUYANSHIBUKEHUOQUEDEYIBUFEN,HENDUOYOUXIDOUSHIYONGDAOLELua,js,pythonYILEIDEJIAOBEN,JIAOBENYUYANKEYIZAIHENDUOFANGMIANJIKAIFAJINCHENGDAILAIBANGZHU。JIAOBENYUYANKEYIZUOWEICHUSHIHUAWENJIANDURUBIANLIANGHEYOUXISHUJUDEYIGEKUAISUERFANGBIANDEFANGFA。TAMENTONGCHANGYINWEIJUYOUJIESHENGBIANYISHIJIANTIGAOSHENGCHANXIAOLV、TIGAOCHUANGZAOXINGHETUOZHANXINGDEYOUDIANERSHOUDAOZHONGDUOYOUXIKAIFACHANGSHANGDEQINGLAI。ZAIBENPIANBOKEZHONG,MASANJIANGHEDAJIAYIQIXUEXIYIXIALuaZHEIMENJIAOBENYUYAN。

      一、搭建Lua运行环境

      学习任何一门编程语言恐怕都是要先搭建起来它的开发环境,这样才能多写代码多练习。Lua有一个交互的解释程序(lua.exe),你可以用它来验证一些小的代码片段,但是对于比较长和复杂的代码,最好是使用C/C++的Lua API来运行,下面的代码就是从C/C++程序中运行一个Lua脚本所需的代码块:

    extern 'C'
    {
        #include <Lua.h>
        #include <lualib.h>
        #include <lauxlib.h>
    }

      DIYIBUYAOZUODEJIUSHIBAOHANXIANGGUANDETOUWENJIAN,YINWEILuaSHIYIGECHUNCDEKU,SUOYIBIXUXIANSHIDIRANGBIANYIQIZHIDAOZHEIYIDIAN,BURANJIUHUIYUDAOWENTI。TONGGUOJIANG#include HE extern 'C'YIQISHIYONGJIUKEYIJIEJUELE。

    //包含lua库。如果你的编译器不支持这个指令的话,那么别忘了在你的项目设置中加入这些库
    #pragma comment(lib,'lua.lib')
    #pragma comment(lib,'lualib.lib)
    #include <iostream>
    int main(){
        //创建一个lua state
        lua_State *pL=lua_open();
    }

      MEIYIGEYUNXINGDEJIAOBENWENJIANDOUZAIYIGEDONGTAIFENPEIDEJIAOZUOlua_StateDESHUJUJIEGOUZHONGYUNXING。LuaKUZHONGDEMEIYIGEHANSHUDEDIAOYONGDOUXUYAOBAlua_StateDEZHIZHENZUOWEIYIGECANSHUCHUANDIJINEIGEHANSHU,YINCIZAIYUNXINGYIGELuaJIAOBENWENJIANZHIQIAN,BIXUTONGGUOlua_openLAICHUANGJIANYIGELua State。

    //开启lua中的标准库
    lua_open_base(pL);
    luaopen_string(pL);
    luaopen_table(pL);
    luaopen_math(pL);
    luaopen_io(pL);

    荣耀彩票代理  LuaZHONGYOUJIGEBIAOZHUNKU,TAMENTIGONGLESHURUSHUCHU、SUANSHUJISUAN、ZIFUCHUANCAOZUODENGYIXILIEDEGONGNENGHANSHU。SHANGMIANDEJIXINGDAIMAKEYIRANGNIZAIJIAOBENLIDIAOYONGZHEIXIEKUMINGLING,JIUXIANGC++#includeXIANGYINGDEKUYIYANG。

      

    if(int error=lua_dofile(pL,'lua_script.lua') !=0 ){
        std::cout<<'error!'<<endl;
        return -1;
    }

    荣耀彩票代理  SHIYONGlua_dofileMINGLINGKEYIZHUANGRU、BIANYI、YUNXINGLuaJIAOBEN。RUGUOYUNXINGDESHIHOUBAOCUOLE,NEIMETAHUIFANHUIYIGECUOWUDEDAIMA。

      

      二、Lua的变量、类型、操作符和条件结构

      DAJIANHAOHUANJINGZHONGYUKEYIXIEXIELuaDAIMALIANLIANSHOULE。DANSHIZAIZHEIZHIQIANWOMENHAISHIXIANLAISHUXIYIXIALuaDEBIANLIANG、LEIXING、CAOZUOFUHETIAOJIANJIEGOU(RUGUONIZHIQIANYOURENHEYIMENBIANCHENGYUYANDEJICHU,NEIMEZHEIXIENEIRONGXUEQILAIJIANGHUISHIFENQINGSONG)。

      1.Lua变量

      Lua是一门动态语言,它和C++/Java不同,变量是可以被赋予任何类型的值(这点和js,python等脚本一样),比如:

      

    --lua脚本开始啦!
    name='masanxiaohuoer'
    print('Name='..name);
    pi=3.1415926
    name=pi
    pi=false

      ZAILuaZHONG,ZHUSHITONGCHANGSHIYI“--”KAISHIDE,RUGUOYAOXIEDUOXINGDEZHUSHI,KEYIZHEIYANGDEJIEGOU:--[[ZHEISHIYIDUANZHUSHINEIRONG]]。ZAIluaZHONG,MEIJUDAIMAHOUMIANDEFENHAOKEXIEKEBUXIE,DANSHIDANGNIDEDAIMAKUAXINGDESHIHOUFENHAOSHIBIBUKESHAODE。TONGSHIZAIluaZHONGKEYITONGSHIJIDUOGEBIANLIANGFUYUDUOGEZHI。

      

    a,b,c,d = 1,2,3,4

    荣耀彩票代理  RUGUOZUOCEDEBIANLIANGGESHUBIYOUCEDEDUODEHUA,NEIMEDUOYUCHULAIDEBIANLIANGJIUHUIBEIFUYUnilZHI,nilZAIluaZHONGBIAOSHIMEIYOUYIYI,KONGZHI(LEISIYUC++ZHONGDENULL)。RUGUOYOUCEDEZHIDUODEHUA,NEIMEDUOYUDEZHIJIUHUIBEIDIUQIDIAOLE。ZAILuaZHONG,YOU3ZHONGBUTONGDEBIANLIANG:global(QUANJU)、local(JUBU)、table fields(BIAO)。RUGUOYIGEBIANLIANGMEIYOUYONGlocalXIUSHIDEHUA,NEIMETAMORENJIUSHIglobalDE。ZAIYIGEBIANLIANGBEIFUZHIZHIQIAN,TADEZHISHInil。

      2.Lua类型

      Lua中有以下的8种基本数据类型:

      (1)NilKONG

        nilHEQITADELEIXINGDOUBUTONG,TAYONGLAIBIAOSHIMEIYOUYIYI,KONGZHI。YIDANJIYIGEZHIFUYULEnil,TAJIUXIAOSHILE,HAOXIANGCONGLAIMEIYOUCUNZAIGUOYIYANG。

      (2)NumberSHUZHI

        numberLEIXINGYONGLAIBIAOSHIFUDIANSHU。ZAIluaNEIBU,ZHEIGEZHIBEICHULIWEIdouble。YINCI,DANGCHUANDInumberLEIXINGDEBIANLIANGJIC/C++CHENGXUDESHIHOU,YAOJIDEJIANGTAMENYINGSHECHENGDUIYINGDEZHENGQUELEIXING。  

      (3)StringZIFUCHUAN

        stringLEIXINGSHIZIFUCHUANLEIXING,NIKEYISHIYONG“..”LAILIANJIELIANGGEZIFUCHUAN。RUGUOYOUYIBIANDELEIXINGBUSHIstringDEHUA,NEIMETAHUIBEIZHUANXINGWEIstringLEIXING,RANHOULIANJIE。

      (4)BooleanBUER

        YONGLAIBIAOSHItrueHUOZHEfalseDEZHILEIXING,0HUOZHEnilDOUSHIfalse,QIYUDEWEIZHEN。

    荣耀彩票代理  (5)FunctionHANSHU

        HEC/C++BUTONG,ZAILuaZHONG,HANSHUYESHIYIZHONGLEIXING,YEKEYIFUZHIJIYIGEBIANLIANG。YINCITONGGUOSHIYONGNEIGEBIANLIANGDEMINGZI,NIJIUKEYIDIAOYONGNEIGEHANSHU。YINWEILuaSHIRUOLEIXINGDEYUYAN,CANSHULIEBIAOHEFANHUIZHIDOUBUXUYAOZHIDINGLEIXING。XIAMIANSHIYIGEJIANDANDELIZI,QIULIANGGESHUZHIHE。ZAILuaZHONG,HANSHUKUAITONGGUOendGUANJIANZILAIJIESHU。

      

    add=function(a,b)
    return a+b
    end

      RUGUONIXIGUANYONGDESHIjavaHUOZHEC++YILEIDEYUYANDEHUA,KENENGHUIDUIZHEIGEYUFAGANJUEYOUDIANMOSHENG。LuaTIGONGLELINGWAIYIGEDINGYIYIGEHANSHUDEFANGSHI,ZHEIYANGKANQILAIGENGXIANGC++/javaLE:

      

    function add(a,b)
        return a+b
    end

      HEC++BUTONGDESHI,luaDEHANSHUKEYIYICIFANHUIDUOGEBIANLIANG,BIRUXIAMIANZHEIYANGXIESHIWANQUANMEIYOUWENTIDE:

      

    function Increse(a,b)
        return a+1,b+1
    end
    
    a=2,b=4;
    a,b=Increase(a,b)
    print(a,b)

    荣耀彩票代理  (5)TableBIAO

    荣耀彩票代理    tableSHILuaZHONGYIZHONGFEICHANGZHONGYAOYEFEICHANGQIANGDADESHUJULEIXING,NIKEYIBABIAOKANCHENGYIZHONGGUANLIANSHUZUHUOZHEHAXIBIAO。ZHEIYIWEIZHENIBUDANKEYIYONGZHENGSHULAISUOYINYIGEBIAO,YEKEYISHIYONGRENHELEIXINGDEJIANZHILAISUOYINYIGEBIAO,ERQIEluaDEBIAOSHIHUNHELEIXINGDE,TAMENKEYIBAOHANBUTONGDESHUJULEIXING。

      

    --创建一个table
    my_table={}
    --添加一些数据到表中
    my_table[1]=2333
    my_table[2]=666
    my_table[3]='HelloWorld'
    
    --或者你也可以一次性的初始化一个表
    my_table={2333,666,'HelloWorld'}
    
    --关联索引
    my_table['one']=2333
    my_table[6]='Study'
    
    --除了使用[]以外,我们还可以使用.来存取一个值
    my_table.one =2333
    
    --函数也可以赋值给表
    function add(a,b)
        return a+b
    end
    my_table={}
    my_table['add']=add 
    print(my_table.add(2,3))

      (6)UserDataYONGHUSHUJU

        userdataLEIXINGYUNXULuaBIANLIANGCUNCHUDINGZHIDEC/C++SHUJU。YIGEuserdataLEIXINGDEBIANLIANGBUNENGZAILuaZHONGCHUANGJIANHEXIUGAI,ZHIYOUTONGGUOC/C++JIEKOUCAIKEYI(XIAPIANWOMENHUIJIESHAOZHEIZHONGFANGFA)。

      (7)ThreadXIANCHENG

        TONGGUOZHEIZHONGLEIXING,WOMENKEYICHANSHENGBINGYUNXINGXINDEXIANCHENG

      3.逻辑操作符

      Lua中有三种逻辑操作符,分别是:and,or,和not。他们和C++中的&&、||和!很像,并且和C++一样,只有在必要的情况下,他们才会检查第二个条件。nil和false为假,其他都为真。

      4.条件结构

      荣耀彩票代理Lua中提供了ifwhile epeat or几种条件结构。lua的几个版本的if和while控制结构和C++很类似,只不过lua中条件不需要写在小括号中。

      

    if a==4 then
        print('four')
    else 
        print('other')
    end
    
    --while循环
    while a<=100 then
        a=a+1
    end
    print(a)
    
    --while和if语句都已end关键字结束
    --repeat和until关键字一起联用,就像这样:
    repeat
        a=a+1
        print(a)
        until a==100
    
    --for结构有两个版本,一个用于数字,一个用于表
    --用于数字的for循环的语法:
    for var=low_value,high_value,step do
    xxxxxx
    end
    
    --举例
    for a=10,20,2 do
    print(a)
    end

      HEC/C++YIYANG,NIYEKEYISHIYONGbreakLAITUICHUXUNHUAN。

      LINGWAIHAIYOUYIZHONGforXUNHUANYONGLAIBIANLIBIAO

    --语法
    for key,value in table do
    xxxxx
    end

      keyHEvalueFENBIEDAIBIAOJIANHEZHI,tableJIUSHIWOMENYAOBIANLIDEBIAO。

      

    --举例
    my_table={a=1,b=2,c=3}
    for k,v in my_table do
        print(k,v)
    end

    荣耀彩票代理  DAOXIANZAI,WOMENYIJINGSHAOWEIDUILuaYOULEYIXIELEJIE,DANSHIWOMENHAIBUNENGYONGTALAIWEIWOMENDEYOUXICHENGXUFUWU。XIAPIANWOMENJIANGGONGTONGXUEXILuaHEC/C++DEJIEKOU,YIJILuaRUHEYINGYONGZAICocos2d-XHEUnityYINQINGZHONG。

    作者:马三小伙儿
    出处:http://www.cnblogs.com/msxh/p/6033679.html 
    请尊重别人的劳动成果,让分享成为一种美德,欢迎转载。另外,文章在表述和代码方面如有不妥之处,欢迎批评指正。留下你的脚印,欢迎评论! 

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规