lua 基础知识点



--nil表示空数据 等同于null


--字符串可以"",也可以用''


--Lua没有整数类型,都是小数类型


--Lua中所有用到索引的都是从1开始的


print("Hello Workd!") --输出hello world




local age = 100 --变量前加local 表示局部变量,否则为全局变量


--[[
print(age)
--]]


name = "wwp"


isMan = false


print(name)


print(type(name)) --type() 取变量的类型




------------------if elseif 用法----------------------
local hp = 100
if hp <= 0 then
print("role die")
elseif hp >= 50 then
print ("role good")
end


------------------while 循环---------------------------


sum = 0
index = 1
while index <= 100 do
sum = sum + index
index = index + 1
end
print(sum)


------------------repeat 循环--------------------------
sum = 0
index = 1
repeat
if index % 2 == 1 then
sum = sum + index
end
index = index + 1
until index > 100
print(sum)


------------------for 循环------------------------------
index = 1
sum = 0
for index = 1,100 do
sum = sum + index
end
print(sum)




--------------------函数---------------------------------


function plus(num1,num2)
return num1+num2
end
sum = plus(100,200)
print(sum)


--------------------数学运算函数-----------------------


print(math.abs(-90))
print(math.max(23,43,4,2))
print(math.random())
print(math.random())
print(math.random())
print(math.random())




-------------------字符串相关函数----------------------


name = "wwPpkkdldlgl"
print(string.lower(name))
print(string.upper(name))
print(string.sub(name,1,3))
print("http://"..name)
print(string.byte(name))


-------------------table 表-----------------------------


--类似于c#里的字典 key,value
mytable = {}
mytable[1] = 34
mytable[3] = "kjgk"
mytable["name"] = "wwp"
mytable.name = "WWP1"
print(mytable[1],mytable[3],mytable["name"],mytable.name)


myTable = {name = "wwp",age = 18,isMan = false}
print(myTable.isMan)


--没有key的
scores = {23,43,5,5,46,57}


for index = 1,table.getn(scores) do
print(scores[index])
end


for index,value in pairs(scores) do
print(index,value)
end


for index,value in pairs(myTable) do
print(index,value)
end


------------------表相关的函数---------------------------


print(table.concat(scores)) --把表中数据连成一个字符串
table.insert(scores,2,2)
print(scores[2])
table.remove(scores,3)
table.sort(scores)


-------------------通过表实现面向对象-------------------
enemy={}
local this = enemy
enemy.hp = 100
enemy.speed = 12.3
enemy.move = function()
print("敌人在移动")
end


function enemy.Attack()
print(this.hp,this.speed)
end


enemy.Attack()















































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值