Love2d新手入門歷程之貪吃蛇(附源碼)

p_w_picpath-B681_53F0D3AB.jpgp_w_picpath-0B47_53F0D3AB.jpg



-----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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章