首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

JavierShare的笔记04——基础Lua语言

这是JavierShare的第4篇原创文章

这一期笔记写一下Lua语言的基础语法。有许多接触过其他C类型的语言的人,会发现其实大部分语言都和C差不多,Lua也一样,只是小部分不同罢了,所以对我来说,那些想要去了解语言的人,自然会去学习和深入了解,水平高的人,也大有人在,所以我的知识会不吝啬得分享出来,所以且行且珍惜吧~( 看的人少:) )

开始。

01

Print("Hello World") 代码间无分号。

age = 100 变量直接申明,不用类型。

name="Javier" 尽量避免_下划线开头

isMan=True

-- 这是单行注释 单行注释

--[[中间代码内容]]-- 多行注释

nil 表示空数据,等同于null

myTable= 数组 索引是从1开始,C是0开始

myTable=[3] 数组第三项

type() 可以获取类型

Tips:最默认定义得变量都是全局的,定义局部变量需要加一个local

02

算数运算符+ - * / % Lua中没有++ --自加运算符

关系运算符 >= ==

逻辑运算符and(与) or(或) not(非)

03

if循环

local hp = 0

if hp

print("player is die")

end

local hp = 100

if hp

print("player is die")

elseifhp>=50 then

print("player is good")

end

local hp = 50

if hp

print("player is die")

elseifhp>=50 then

print("player is good")

else

print("player is bad")

end

Tips:这里elseif 一定要连写,只要有if 后面一定要跟then。

04

while和repeat和for循环

index=1

while index

print(index)

index=index+1

end

sum=0

index=1

while index

sum = sum +index

index=index+1

end

sum=0

index=1

while index

if index%2==1 then

sum = sum +index

end

index=index+1

end

repeat循环(相当于C#里的do)

index=1

repeat

print(index)

index = index+1

until index>100

sum=0

index=1

repeat

sum=sum+index

index = index+1

until index>100

print(sum)

sum=0

index=1

repeat

if index%2==1 then

sum=sum+index

end

index = index+1

until index>100

print(sum)

for循环

for index=1,100 do

print(index)

end

sum=0

for index=1,100 do

sun=sum+index

end

sum=0

for index =1,100 do

if index%2==1 then

sum=sum+index

end

end

Tips:break 可以终止循环,没有continue语法。

05

方法

function Plus(num1,num2)

return num1+num2

end

res = Plus(1,2)

print(res)

Lua标准库

tostring() 把一个数字转化成字符串

tonumber() 字符串转数字

myTable[]={}

myTable["name"]="apple"

myTable.name="apple" 会覆盖上面

myTable =

myTable=

遍历

1.如果只有数字键,并且是连续的。

Scores=

for index=1,table.getn(scores) do

prit(scores[index])

end

2.所有表都可以输出

for(index,value in pairs(Scores) do

print(index,value)

end

06

面向对象

Enemy={}

local this = Enemy

Enemy.hp=100

Enemy.spend=10

Enemy.Move=function()

print("移动")

end

function Enemy.Attack()

print(this.hp,"attack")

this.Move()

end

Enemy.Attack() --调用

Keep Active

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20180518G1W1TP00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券
http://www.vxiaotou.com