1.基础语法
print("hello world")
--[[ssssss
ss --]]
--变量申明可以直接写名字
--一共有五种类型 根据数据去确定数据类型
mytablename = {12,122,1}
print(mytablename[1])
-- lua 默认变量都是全局的变量
-- string 可以单引号
-- 数组第一位索引是1
hp =59;
if(hp<90) then
print('不及格')
end
if hp>90 then
print("niubi")
elseif hp>60 and hp < 80 then
print("及格")
else
print("bujige")
print("nige shabi")
end
--lua 没有自增自减运算符号
index = 1
while index < 3 do index = index + 1 end
print (index)
repeat index = index +1
until index >=3
print (index) --4
sum = 0
for index1 = 1,100 do
sum = sum + index1
if(sum > 100) then
sum = 1000
break
end
end
print(sum)
function Plus ( num1 , num2)
return num1 + num2
end
resoult = Plus(11,11)
print(resoult)
print(math.abs(-1))
print(math.max(1,2,3,4))
str1 = "heh"
str2 = "bab"
print(string.sub(str1,1,2)..str2)
matable1 = {}
matable1[1] = "mmmm"
matable1["n"] = "nnnn"
print(matable1.n)
print(matable1['n'])
myt = { name = "heheh",age = 10, sex = "men",h="ke"}
print(myt[1]) --nil
print(myt[name])--nil
print(myt['name'])--"heheh"
print(myt.name)--"heheh"
myt1 = { "a","b","c"}
--print( myt1[1])
--[[for i = 1,3 do
print(myt1[i])
end
]]--
--[[i=1
while i<4 do
print(myt1[i])
i = i+1
end
]]--
--[[for i = 1,table.getn(myt1) do
--i = i+2
print (myt1[i])
end
]]--
-- 所有都能遍历的数组
for index ,value in pairs(myt) do
print(index,value)
end
--也可以
for index1 ,value1 in pairs(myt1) do
print(index1 ,value1)
end
t = { name = "fei",age = 13,high = 120}
--table.insert(t,2,2)
--print(t[2])
table.insert(t,"hehheh")
---位置是n+1 但是默认的key 是 1 开始
for key,value in pairs(t) do
print( key ,value)
end
2,面向对象
Enemy = {}
-- 关键字 可以索引当前对象
local this = Enemy
Enemy.hp = 10
Enemy.speed = 100
Enemy.hurt = 1
--方法可以放在变量中
Enemy.move = function ()
print("敌人在移动",this.speed)
end
--Enemy.move() --调用
--this.move() --调用
Player = {}
Player.hp = 100
Player.speed = 10
Player.hurt =111
Player.name ="shabi"
Player.Move = function (i,j)
Player.hp = i
Player.speed =j
print(Player.hp,Player.speed)
end
--Player.Move(1,1)
function Player.Attack (j)
Player.hurt = j
print(Player.hurt)
Enemy.hp = Enemy.hp -j
print(Enemy.hp)
end
---自动改变赋值类型
Player.Attack("1") --1 9
Player.Attack(2) -- 2 7