【Lua】【1】Lua游戏脚本语言入门之杂码

本文介绍了Lua语言的基础语法,包括for循环、repeat-until结构、逻辑运算符and、or、not的使用,以及类型检查、条件判断、表格操作、函数定义等内容,适合初学者入门。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

--//============= for  循环
--[[
for a=1, 5, 2
do
print("Hello  ")
end
]]
--//===========repeat  util
--[[
a=1
repeat
    print("Hello")
    a=a+1
until  a==5
]]
--//=============and  or  not
--[[
    and 和 or的运算结果不是true和false,而是和它的两个操作数相关。
a and b:如果a为false,则返回a;否则返回b
a or b:如果 a 为true,则返回a;否则返回b
]]
--[[
print(4  and  5)        -- 5
print(4  or  5 )        --4
print(nil  and  13) --nil
print(false   and  13)  --false
print(nil  or  15)      --15
print(false  or  9)    --9
a= true
x=   true  and  false or  7
print(x)
  y= a  and  print("----true")  or  print("----false")
print(y)
]]
-- //============type  获得变量的类型
--[[
a=1
print(type(a))
b="Hello"
print(type(b))
c=true
print(type(c))
d=nil
print(type(d))
e={1,2,3}
print(type(e))
e="nihao"
print(type(e))
]]
--//============
--[[
a=12
b=12
if  a==b then
print("a==b")
elseif  a~=b  then
print("a ~=b")
end
A=false
print(A)
c=
print(type(c))
 A={12,23}
--print("A="..A)  --   A=nil
]]
--[[
e={1,2,3}
print(e[1]..e[2]..e[3])
for i=1 , 3 ,1  do
    print(e[i])
end
print("----------------")
for i =1, table.maxn(e) ,1  do
    print(e[i])
end
]]
--  //===============
--[[
a="HHh "
--print(type(a))
if  a  then
print("true")
print(a.."\n".."\tmm")
else
    print("false")
end
]]
-- //==============
--print("one line\nnext  line\n\" in  quotes\" ,\' in \'")
--[[
page = [\[
<HTML>
<HEAD>
<TITLE>An HTML Page</TITLE>
</HEAD>
<BODY>
<A HREF="http://www.lua.org">Lua</A>
            </BODY>
</HTML>
]\]
print(page)
]]
--  //================table
--[[
t={}
t[1]=10
t[2]=23
t[3]="MM"
t[5]=true
for  i=1 , table.maxn(t) ,1 do
    print(t[i])
end
]]
--[[
T={}
T[1]=10
T[2]="Hello"
T["person"]={ ["name"]="lisong"  ,["sex"]="man"}
T["person2"]={ name="lisong2", sex="man2"}
T.person3={ name= "lisong3", sex="man3"}
T.person4={ ["name"]="lisong4"  ,["sex"]="man4"}
T[3]=12
print(T.person.name) ;
print(T.person.sex)
print()
print(T.person2.name) ;
print(T.person2.sex)
print()
print(T.person3.name) ;
print(T.person3.sex)
print()
print(T.person4.name) ;
print(T.person4.sex)
print()
for   i=1,  table.maxn(T) ,  1
do
    print(i.." "..T[i])
end
]]
--//==========================
--[[
T =
{
    10,
    12.3,
    "Hello",
    [4]=23,
    [5]=9,
    [6]=67,
    person1={name="lisong", sex="man"},
    person2= { ["name"]= "lisong2", sex="man2"},
    person3= { ["name"]= 89, sex=true},
}
print(T.person1.name); print(T.person1.sex)
print(T.person2.name); print(T.person2.sex)
print(T.person3.name); print(T.person3.sex)
for   i=1,  table.maxn(T) ,  1
do
    print(i.." "..T[i])
end
]]
--[[
function  add(a,b)
    return  a+b
end
function  ji(a,b)
    return   a*b
end
print(add(4,5))
print(ji(4,5))
]]
--[[
shang= function   (a,b)  return   a/b  end
print(shang(10,3))
function   digui(num)
    if  num==0  then
        return   0;
    else
        return  digui(num-1)+num
    end
end
print(digui(100))
shang="Hello"
print(shang)
]]
--[[
function  s()
    return   1,2,3,"Hello"
end
a,b,c,d,e= s()
print(a)
print(b)
print(c)
print(d)
print(e)
x=2
y=3
function  s()
local   x=1
local   y=2
    print("局部变量x="..x)
    print("局部变量y="..y)
    return  x+y
end
print("求函数的值"..s())
print("x="..x)
print("y="..y)
]]
--[[
t=
{
    age=27,
    add=function(a,n)  a.age=a.age+n  end
}
print(t.age)
t.add(t,10)
t:add(10)
print(t.age)
print()
]]
--[[
function   sui()
    t=
    {
        name="lisong",
        sex="man",
        age=24
    }
    return  t.name,t.sex,t.age
end
a,b,c=sui()
print(a)
print(b)
print(c)
]]
--[[
function   pyth(a,b)
    local  c=a^2+b^2+1
    return  math.sqrt(c)
end
print(pyth(3,4))
]]
--[[
for  i=1,5,2   do
    print("i  is  now "..i)
end
for  i=5,-5,-1  do
    print("i  现在是"..i)
end
]]
--[[
function   str(i,a)
    print("i is  now "..i.."  "..a)
end
for  i=1,10,1 do
    if   i<4  then
        str(i,"small")
    elseif  i<8   then
        str(i,"mudium")
    else
        str(i,"big")
    end
end
]]
--[[
function  size(a,b,c)
    if   c=="+"  then
        return  a+b
    elseif   c=="-"  then
        return   a-b
    elseif  c=="*"  then
        return  a*b
    elseif  c=="/"   then
        return  a/b
    else
        error("false")
    end
end
str="/"
print(size(3,4,str))
]]
--[[
mydata={}
mydata[1]="Hello"
mydata[2]=12
mydata[3]=true
mydata["bar"]="baz"
mydata[5]=8
for  key , value  in  pairs(mydata)  do
    --print(key.." = "..value)
    print("key= "..key.."\t",value)
end
]]
--[[
a={}
a["x"]=10
b=a
print(a["x"].."   "..a.x);
print(b["x"].."   "..b.x);
b.x=20
print(a["x"].."   "..a.x);
print(b["x"].."   "..b.x);
a.x="Hello"
print(a["x"].."   "..a.x);
print(b["x"].."   "..b.x);
a.x=nil
print(a["x"].."   "..a.x);
print(b["x"].."   "..b.x);
]]
--[[
foride=
{
    color="red",
    age="24",
    [1]={x=12,y=2},
    [2]={x=-19,y=23},
    [0]={x=2,y=3}
}
--print(foride.color)
print(foride[0].x.."  "..foride[0].y)
print(foride[1].x.."  "..foride[1].y)
print(foride[2].x.."  "..foride[2].y)
for   key,value  in  pairs(foride)
do
    print(key,value)
end
]]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值