【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】
前面我们用对easyx做了lua的适配,也就是说,对于easyx的基本接口做好封装之后,就可以用lua进行进行业务开发了。这样,就不需要安装任何其他的软件,也不需要安装第三方库什么的,直接拿起编辑器就可以开发界面了。开发的时候,修改也不需要编译,因为自身的解析器和虚拟机已经集成在里面了。今天,我们正好可以看看,怎么用lua设计一个太阳、地球、月亮的旋转演示效果图。

1、准备一个基本框架
首先我们准备一个基本框架,也就是基本图形,
initgraph(640,480)
_getch()
closegraph()
2、添加第一个太阳
太阳的添加比较简单,设置好颜色之后,fill一个圆形即可,
setfillcolor(red) -- red color
local x0 = 320
local y0 = 240
fillcircle(x0, y0,50)
3、添加地球
地球本身是围绕着太阳转的,那么这个时候就会涉及到旋转角度。默认当前的旋转角度是angle,并且默认太阳和地球距离180个pixel,
local radian1 = math.rad(angle) -- angle to randians
local x1 = x0 + math.floor(180*math.cos(radian1))
local y1 = y0 + math.floor(180*math.sin(radian1))
setfillcolor(blue) -- blue color
fillcircle(x1, y1,20)
开发的过程中使用到了math库,这是lua自带的函数库,可以直接使用。不过对lua来说,它最大的优势还是适配的那些API,这样才能体现自身的价值。
4、继续添加月球
月球的旋转式围绕着地球进行的,同样,也会涉及到一个旋转角度问题,为了简单说明。我们默认旋转角度,是地球围绕太阳旋转角度的两倍,
local radian2 = math.rad(angle * 2)
local x2 = x1 + math.floor(50*math.cos(radian2))
local y2 = y1 + math.floor(50*math.sin(radian2))
setfillcolor(green) -- green color
fillcircle(x2, y2, 10)
5、更新角度
角度本身是需要定时更新的。并且更新的时候,需要判断是否越界,
angle = angle + speed -- increase angle here
if angle == 360 then
angle = 0
end
6、让速度变成可配的情形
细心的同学可能发现了,这里的speed不是一个固定数值。原因就在于我们把它变成了可以配置的形式。即单击空白格,就可以弹出配置窗口,
if _kbhit() == 1 then -- check input box
str = _getch();
speed = inputbox()
end
7、完整流程
最后我们给出完整的流程,大家会发现,它和c语言其实差不多。但是好处也是很多的,因为它不需要编译,不需要任何编译环境,入门门槛不高,所以可以适合所有人都拿来看一看、学一学。
-- file: demo.lua
-- author: feixiaoxing
-- email: feixiaoxing@163.com
-- date: 2025-10-19
initgraph(640,480)
angle = 0
red = 0x0000aa
blue = 0xaa0000
green = 0x00aa00
speed = 1
BeginBatchDraw()
while true do
cleardevice()
if _kbhit() == 1 then -- check input box
str = _getch();
speed = inputbox()
end
angle = angle + speed -- increase angle here
if angle == 360 then
angle = 0
end
setfillcolor(red) -- red color
local x0 = 320
local y0 = 240
fillcircle(x0, y0,50)
local radian1 = math.rad(angle) -- angle to randians
local x1 = x0 + math.floor(180*math.cos(radian1))
local y1 = y0 + math.floor(180*math.sin(radian1))
setfillcolor(blue) -- blue color
fillcircle(x1, y1,20)
local radian2 = math.rad(angle * 2)
local x2 = x1 + math.floor(50*math.cos(radian2))
local y2 = y1 + math.floor(50*math.sin(radian2))
setfillcolor(green) -- green color
fillcircle(x2, y2, 10)
FlushBatchDraw()
sleep(50)
end
EndBatachDraw()
closegraph()
注1:
另外一个有趣的旋转demo,之前是c语言,现在转成lua语言,
-- file: demo.lua
-- author: feixiaoxing
-- email: feixiaoxing@163.com
-- date: 2025-10-19
initgraph(640,480)
angle = 0
red = 0x0000aa
blue = 0xaa0000
green = 0x00aa00
setfillcolor(red)
solidcircle(320, 240, 50)
setfillcolor(blue)
solidcircle(320, 210, 8)
img1 = getimage(270, 190, 100, 100)
img2 = getimage(270, 190, 100, 100)
BeginBatchDraw()
while true do
cleardevice()
angle = angle + 2
if angle == 360 then
angle = 0
end
rotateimage(img2, img1, angle)
putimage(270, 190, img2)
FlushBatchDraw()
sleep(50)
end
EndBatachDraw()
closegraph()
注2:
以及碰撞的小球,
-- file: demo.lua
-- author: feixiaoxing
-- email: feixiaoxing@163.com
-- date: 2025-10-19
initgraph(640,480)
angle = 0
red = 0x0000aa
blue = 0xaa0000
green = 0x00aa00
x = 120
y = 120
direction_x = 0
direction_y = 0
speed = 3
BeginBatchDraw()
while true do
cleardevice()
if x > 590 then
direction_x = 1
end
if x < 50 then
direction_x = 0
end
if y > 430 then
direction_y = 1
end
if y < 50 then
direction_y = 0
end
if direction_x == 0 then
x = x + speed
else
x = x - speed
end
if direction_y == 0 then
y = y + speed
else
y = y - speed
end
setfillcolor(red)
solidcircle(x,y, 50)
FlushBatchDraw()
sleep(50)
end
EndBatachDraw()
closegraph()