--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()