您的位置:首页 > 其它

Love2d新手入门教程之贪吃蛇(附源码)

2014-08-18 00:35 501 查看
最近一直在研究Love2D这款游戏引擎, 晚上花了点时间写了个贪吃蛇,大家可以看看下面两个图





这是main.lua 中的代码

max=0				                                     --这个变量控制蛇向前进的速度
gameover=false
math.randomseed(tostring(os.time()):reverse():sub(1, 6)) --随机数种子
function love.load()
p_x=math.random(5,35)*20
p_y=math.random(4,26)*20
pos={[1]={x=p_x,y=p_y},[2]={x=p_x+20,y=p_y},[3]={x=p_x+40,y=p_y}}
snake={[1]={fx="left"}}
bean={[1]={x=math.random(1,40-1)*20,y=math.random(1,30-1)*20}}
end

function love.update(dt)
max=max+dt

if(pos[1].x==bean[1].x and pos[1].y==bean[1].y)then
bean[1].x=-100
bean[1].y=100

--蛇多一节
if(pos[table.getn(pos)-1].x>pos[table.getn(pos)].x)then
table.insert(pos,table.getn(pos)+1,{x=pos[table.getn(pos)].x-20,y=pos[table.getn(pos)].y})

elseif(pos[table.getn(pos)-1].x<pos[table.getn(pos)].x)then
table.insert(pos,table.getn(pos)+1,{x=pos[table.getn(pos)].x+20,y=pos[table.getn(pos)].y})

elseif(pos[table.getn(pos)-1].y>pos[table.getn(pos)].y)then
table.insert(pos,table.getn(pos)+1,{x=pos[table.getn(pos)].x,y=pos[table.getn(pos)].y-20})

elseif(pos[table.getn(pos)-1].y<pos[table.getn(pos)].y)then
table.insert(pos,table.getn(pos)+1,{x=pos[table.getn(pos)].x,y=pos[table.getn(pos)].y+20})
end
--生成新的豆子
newbean()
--love.update(60)
end
if(max>0.5) then
for i=table.getn(pos),2,-1 do
pos[i].x=pos[i-1].x
pos[i].y=pos[i-1].y
end
if(snake[1].fx=="left")then
pos[1].x=pos[1].x-20

elseif(snake[1].fx=="down")then
pos[1].y=pos[1].y+20

elseif(snake[1].fx=="right")then
pos[1].x=pos[1].x+20

elseif(snake[1].fx=="up")then
pos[1].y=pos[1].y-20
end
max=0
end

if(pos[1].x<0 or pos[1].x>780 or pos[1].y<0 or pos[1].y>580) then
gameover=true
end
for i=2,table.getn(pos) do
if(pos[1].x==pos[i].x and pos[1].y==pos[i].y)then
gameover=true
end
end

end

function love.draw()

if(gameover) then
love.graphics.setColor(255,255,255)
love.graphics.print("gameover!click the screen to begin a new game",250,300)
else
love.graphics.setColor(255,255,255)
love.graphics.print("length of the snake:" .. table.getn(pos),5,5)
for i=1,table.getn(pos) do

love.graphics.rectangle("fill",pos[i].x,pos[i].y,20,20)
end

love.graphics.setColor(0,0,0)
love.graphics.polygon('fill', pos[1].x+10, pos[1].y+12, pos[1].x+7, pos[1].y+15, pos[1].x+13, pos[1].y+15)
love.graphics.circle("fill",pos[1].x+5,pos[1].y+5,2,100)
love.graphics.circle("fill",pos[1].x+15,pos[1].y+5,2,100)

love.graphics.setColor(255,255,255)
love.graphics.rectangle("fill",bean[1].x,bean[1].y,20,20)
love.graphics.setColor(0,0,0)
love.graphics.print(table.getn(pos),pos[table.getn(pos)].x,pos[table.getn(pos)].y)
end

end
function love.keypressed(key)
if (key=="down")then
if(snake[1].fx~="up")then
snake[1].fx="down"
love.update(60)
end

elseif (key=="up")then
if(snake[1].fx~="down")then
snake[1].fx="up"
love.update(60)
end

elseif (key=="left")then
if(snake[1].fx~="right")then
snake[1].fx="left"
love.update(60)
end

elseif (key=="right")then
if(snake[1].fx~="left")then
snake[1].fx="right"
love.update(60)
end

end

end
function love.mousepressed(x,y,button)
if(gameover)then
if(button=="l")then
gameover=false
love.load()
end
end
end
function newbean()
b_x=math.random(1,40-1)*20
b_y=math.random(1,30-1)*20

for i=1,table.getn(pos) do

if(b_x==pos[i].x and b_y==pos[i].y)then
newbean()
elseif(math.abs(b_x-pos[i].x)<60 and math.abs(b_y-pos[i].y)<60 )then

else
bean[1].x=b_x
bean[1].y=b_y
end
end
end


这是 conf.lua 中的代码

function love.conf(t)
t.title = "贪吃蛇"
t.screen.width = 800
t.screen.height = 600
end



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐