MarkHoo
文章43
标签36
分类11
Lua快速入门

Lua快速入门

Lua是一门快速小巧的脚本语言,这篇文章让你快速入门Lua,只需一个蹲坑的时间

打印/输出

1
print("Hello World")

输出

1
2
3
4
--this is a comment
print("hello") --this is another comment
-- the next line will not do anything because it is commented out
--print("world")

变量

1
2
3
4
5
-- Different types
local x = 10 --number
local name = "john doe" --string
local isAlive = false -- boolean
local a = nil --no value or invalid value

Numbers

operators

  • + addition
  • - minus
  • * multiply
  • / divide
  • ^ power
  • % modulus
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
-- examples
local a = 1
local b = 2
local c = a + b
print(c) -- 3

local d = b - a
print(d) -- 1

local x = 1 * 3 * 4 -- 12
print(x)

local y = (1+3) * 2 -- 8
print(y)


print(10/2) -- 5
print (2^2) -- 4
print(5%2) -- 1

print(-b) -- -2
1
2
3
4
-- increment
local level = 1
level = level + 1
print(level) -- 2

字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
-- concatenate strings
local phrase = "My name is "
local name = "John Doe"
print(phase .. name) --My name is John Doe

-- strings and numbers
local age = 12
local name = "Billy"
print(name .. " is " .. age .. " years old")
````

**布尔**
```lua
local isAlive = true
print(isAlive) --true
isAlive = false
print(isAlive) --false

条件语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
--number comparisions
local age = 10
if age < 18 then
print("over 18") --this will not be executed
end

--elseif and else
age = 20
if age > 18 then
print("dog")
elseif age == 18 then
print("cat")
else
print("mouse")
end

比较运算符

  • == 相等
  • < 小于
  • > 大于
  • <= 小于等于
  • >= 大于等于
  • ~= 不等于
1
2
3
4
5
6
7
8
9
10
11
12
13
14
--boolean comparision
local isAlive = true
if isAlive then
print("dog")
end

--string comparisions
local name = "billy"
if name == "Billy" then --false
print("Billy")
elseif name == "billy" then --true
print("billy")
end

组合语句

1
2
3
4
5
6
7
local x = 10
if x == 10 and x < 0 then --both are true
print("dog")
elseif x == 100 or x < 0 then --1 or more are true
print("cat")
end
--result: cat

嵌套语句

1
2
3
4
5
6
7
8
9
local x = 10
local isAlive = true
if x==10 then
if isAlive == true then
print("dog")
else
print("cat")
end
end

反转值

您还可以使用 not 关键字取反

1
2
3
4
local x = 10
if not x == 10 then
print("here")
end

函数

1
2
3
4
5
6
function printTax(price)
local tax = price * 0.21
print("tax:" .. tax)
end

printTax(200)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
--function that returns a value
function calculateTax(price)
return price * 0.21
end

local result = calculateTax(100)
print(result)

--reusing the function but this time using variables
local bread = 130
local milk = 110

local breadTax = calculateTax(bread) --27.3
local milkTax = calculateTax(milk) --23.1

print("Bread Tax = " .. breadTax)
print("Milk Tax = " .. milkTax)
1
2
3
4
5
6
--multiple parameters
function displayInfo(name, age, country)
print(name .. " is " .. age .. " years old and is from " .. country)
end

displayInfo("Billy", 12, "Jupiter")

作用域

变量有不同的作用域。一旦到达范围的末尾,该范围中的值将不再可访问

1
2
3
4
5
function foo()
local a = 10
end

print(a) --nil
1
2
3
4
5
6
local isAlive = true
if isAlive then
local a = 10
end

print(a) --nil

全局变量

1
2
local _G.myValue = 69
--doing this can sometimes be bad practice

循环

有几种不同的方法可以在 lua 中执行循环

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
--while loop
local i = 0
local count = 0

while i <= 10 do
count = count + 1
end

print("count is " .. count) --count is 7


--for loop
count = 0
for i=1, 5 do
count = count + 1
end
print("count is " .. count)

无限循环(死循环)

1
2
3
4
5
6
--infinite loop will never end
local i = 0
while i >= 0 do
i = i + 1
print(i)
end

嵌套循环

1
2
3
4
5
6
7
local count = 0
for a=1, 10 do
for b=1, 10 do
count = count + 1
end
end
print(count) -- 100

1
2
3
4
5
6
7
8
9
10
11
--basic table
local colors = { "red", "green", "blue" }

print(colors[1]) --red
print(colors[2]) --green
print(colors[3]) --blue

--using a loop to iterate though your table
for i=1, #colors do
print(colors[i])
end

表操作

1
2
3
4
5
--insert
local colors = { "red", "green", "blue" }
table.insert(colors, "orange")
local index = #colors --4 (this is the last index in the table)
print(colors[index]) --orange
1
2
3
4
5
6
7
--insert at index
local colors = { "red", "green", "blue" }
table.insert(colors, 2, "pink")
for i=1, #colors do
print(colors[i])
end
--red, pink, green, blue
1
2
3
4
5
6
7
--remove 
local colors = { "red", "green", "blue" }
table.remove(colors, 1)
for i=1, #colors do
print(colors[i])
end
-- "green", "blue"

2 尺寸表

1
2
3
4
5
6
7
8
9
10
--tables within tables
local data = {
{ "billy", 12 },
{ "john", 20 },
{ "andy", 65 }
}

for a=1, #data do
print(data[a][1] .. " is " .. data[a][2] .. " years old")
end

键表

二维表不适合不同类型的数据,而是使用表的键

1
2
3
4
5
6
7
8
9
10
local teams = {
["teamA"] = 12,
["teamB"] = 15
}

print(teams["teamA"]) -- 12

for key,value in pairs(teams) do
print(key .. ":" .. value)
end
1
2
--insert into key table
teams["teamC"] = 1
1
2
--remove key from table
teams["teamA"] = nil

从函数返回表

这可用于从函数返回多个值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function getTeamScores()
local scores = {
["teamA"] = 12,
["teamB"] = 15
}
return scores
end

local scores = getTeamScores()
local total = 0
for key, val in pairs(scores) do
total += val
end
print("Total score of all teams:" .. total)

Math

数学类有许多处理数字的函数。您可能不需要它们,但这里有一些更有用的功能:

更多: Wiki

  • abs (绝对值)
    1
    2
    3
    4
    local x = -10
    print(math.abs(x)) --result: 10
    local a = 10
    print(math.abs(a)) --result: 10
  • ceil (四舍五入的十进制值)
    1
    2
    local x = 1.2
    print(math.ceil(x)) --result: 2
  • deg (将值从弧度转换为度)
    1
    print(math.deg(math.pi)) -- result: 180
  • floor (向下取整十进制值)
    1
    2
    local x = 1.2
    print(math.floor(x)) --result: 1
  • pi (pi 的常数值)
    1
    2
    print(math.pi) --3.1415926535898
    3.1415926535898
  • rad (将值从度数转换为弧度)
    1
    print(math.rad(180)) --result: 3.1415926535898
  • random (随机数生成)
    1
    2
    3
    4
    5
    6
    7
    8
    --random value between 0 tand 1
    print(math.random()) --result: 0.0012512588885159

    --random integer value from 1 to 100 (both inclusive)
    print(math.random(100)) --result: 20

    --random integer value from 20 to 100 (both inclusive)
    print(math.random(20, 100)) --result: 54
  • sqrt (数字的平方根)
    1
    print(math.sqrt(100)) --result: 10

模块

Include code other files

1
require("otherfile")
本文作者:MarkHoo
本文链接:https://blog.markhoo.com/posts/LearnLua/
版权声明:本文采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可
×