love2d 1. 入门
1、写个贪吃蛇
以下是main.lua的代码
snake = {}
speed = 50
function init()
snake = {}
local head = {x=love.graphics.getWidth()/2,y=love.graphics.getHeight()/2,direction='up'}
table.insert(snake,head)
local tail = {x=love.graphics.getWidth()/2,y=love.graphics.getHeight()/4*3,direction='up'}
table.insert(snake,tail)
end
function love.load(arg)
init()
end
function line2line(line1_x1,line1_y1,line1_x2,line1_y2,line2_x1,line2_y1,line2_x2,line2_y2)
return (not((math.max(line1_x1,line1_x2)<math.min(line2_x1,line2_x2)) or (math.min(line1_x1,line1_x2)>math.max(line2_x1,line2_x2)))) and (not((math.max(line1_y1,line1_y2)<math.min(line2_y1,line2_y2)) or (math.min(line1_y1,line1_y2)>math.max(line2_y1,line2_y2))))
end
function love.update(dt)
--head------------------------------------------------
if snake[1].direction == 'up' then
snake[1].y = snake[1].y - dt*speed
elseif snake[1].direction == 'down' then
snake[1].y = snake[1].y + dt*speed
elseif snake[1].direction == 'left' then
snake[1].x = snake[1].x - dt*speed
elseif snake[1].direction == 'right' then
snake[1].x = snake[1].x + dt*speed
end
--head------------------------------------------------
--tail------------------------------------------------
local tail_index = #snake
if snake[tail_index].direction == 'up' then
snake[tail_index].y = snake[tail_index].y - dt*speed
if snake[tail_index].y <= snake[tail_index-1].y then
table.remove(snake)
end
elseif snake[tail_index].direction == 'down' then
snake[tail_index].y = snake[tail_index].y + dt*speed
if snake[tail_index].y >= snake[tail_index-1].y then
table.remove(snake)
end
elseif snake[tail_index].direction == 'left' then
snake[tail_index].x = snake[tail_index].x - dt*speed
if snake[tail_index].x <= snake[tail_index-1].x then
table.remove(snake)
end
elseif snake[tail_index].direction == 'right' then
snake[tail_index].x = snake[tail_index].x + dt*speed
if snake[tail_index].x >= snake[tail_index-1].x then
table.remove(snake)
end
end
--tail------------------------------------------------
--out of range------------------------------------------------
if (snake[1].x<0 or snake[1].x>love.graphics.getWidth()) or (snake[1].y<0 or snake[1].y>love.graphics.getHeight()) then
init()
return
end
--out of range------------------------------------------------
--碰撞------------------------------------------------
for i=3,#snake-1,1 do --第3个点+第4个点开始,与头(第一个点)+第二个点,是否相交
--print('ppp')
if line2line(snake[1].x,snake[1].y,snake[2].x,snake[2].y,snake[i].x,snake[i].y,snake[i+1].x,snake[i+1].y) then
init()
return
end
end
--碰撞------------------------------------------------
end
function love.draw(dt)
for i=1,#snake-1,1 do
love.graphics.line(snake[i].x,snake[i].y,snake[i+1].x,snake[i+1].y)
end
end
function love.keypressed(key) --键盘检测回调函数,当键盘事件触发是调用
if key == 'up' or key == 'down' then
if snake[1].direction ~= 'up' and snake[1].direction ~= 'down' then
snake[1].direction = key
local head = {x=snake[1].x, y=snake[1].y, direction=snake[1].direction} --new head
table.insert(snake,1,head)
end
elseif key == 'left' or key == 'right' then
if snake[1].direction ~= 'left' and snake[1].direction ~= 'right' then
snake[1].direction = key
local head = {x=snake[1].x, y=snake[1].y, direction=snake[1].direction} --new head
table.insert(snake,1,head)
end
end
end
2、打包成exe
把main.lua等同级目录的所有文件打包成xxx.zip,改名为xxx.love
放到love的文件中(love-11.3-win64)
新建build.bat,内容为
copy /b love.exe+xxx.love xxx.exe
把xxx.exe,以及love的文件中(love-11.3-win64)所有的dll,打包,发给朋友,就能愉快玩耍了!!
参考:
别人做的一个游戏
http://osmstudios.com/tutorials/your-first-love2d-game-in-200-lines-part-1-of-3
一些讨论,怎么把dll打包进exe中,结论是,不推荐
https://love2d.org/forums/viewtopic.php?t=84533
https://love2d.org/forums/viewtopic.php?t=84358
https://love2d.org/forums/viewtopic.php?t=83614
https://love2d.org/forums/viewtopic.php?t=78157
https://love2d.org/forums/viewtopic.php?t=83614
https://love2d.org/forums/viewtopic.php?t=82569
https://love2d.org/forums/viewtopic.php?t=78157
半山无极
https://www.cnblogs.com/xdao/archive/2012/12/07/2806500.html