lua+love2d制作的2048游戏
使用lua和love2d编写的pc版2048游戏,适用于linux和windows平台。依赖love2d游戏引擎,love2d需0.9及以上版本。
core.lua
core={}
core.block={}
core.score=0
core.best=0
love.filesystem.setIdentity("2048")
localfunctionget_best()
ifnotlove.filesystem.exists("best")then
core.best=0
return
end
core.best=love.filesystem.read("best")
core.best=tonumber(core.best)
end
functioncore.initial()
core.block={}
localpos1=love.math.random(1,16)
localpos2
whiletruedo
pos2=love.math.random(1,16)
ifpos2~=pos1thenbreakend
end
localval
val=love.math.random()
ifval<0.8thenval=2elseval=4end
core.block[pos1]=val
val=love.math.random()
ifval<0.8thenval=2elseval=4end
core.block[pos2]=val
core.score=0
end
functioncore.set_best()
ifcore.score>core.bestthen
core.best=core.score
localret,err=love.filesystem.write("best",core.best)
end
end
functioncore.tblclone(t1,num)
localt2={}
fori=1,numdo
t2[i]=t1[i]
end
returnt2
end
functioncore.isfull(testtbl)
localblock
iftesttblthenblock=testtblelseblock=core.blockend
fori=1,16do
ifnotblock[i]thenreturnfalseend
end
returntrue
end
localfunctioncombine(lstart,lend,lstep,rstart,rend,rstep,flag,testtbl)
localindex
localtflag,block
iftesttblthen
tflag=true
block=testtbl
else
block=core.block
end
localcflag=false
fori=lstart,lend,lstepdo
forj=rstart,rend,rstepdo
ifflag=="up"thenindex=(i-1)*4+j
elseifflag=="down"thenindex=(i+1)*4+j
elseifflag=="left"thenindex=i*4+j-1
elseindex=i*4+j+1end
ifblock[index]andblock[i*4+j]and
block[index]==block[i*4+j]and
block[index]<2048then
cflag=true
iftflagthenreturncflagend
block[index]=2*block[i*4+j]
block[i*4+j]=nil
core.score=core.score+block[index]
end
end
end
returncflag
end
localfunctionmove(lstart,lend,lstep,rstart,rend,rstep,flag)
localmflag=false
localindex,kstart,kend,kstep
fori=lstart,lend,lstepdo
forj=rstart,rend,rstepdo
ifflag=="up"then
kstart=0
kend=i-1
kstep=1
elseifflag=="down"then
kstart=3
kend=i+1
kstep=-1
elseifflag=="left"then
kstart=1
kend=j-1
kstep=1
else
kstart=4
kend=j+1
kstep=-1
end
fork=kstart,kend,kstepdo
ifflag=="up"orflag=="down"thenindex=k*4+j
elseindex=i*4+kend
ifnotcore.block[index]andcore.block[i*4+j]then
core.block[index]=core.block[i*4+j]
core.block[i*4+j]=nil
mflag=true
break
end
end
end
end
returnmflag
end
localfunctiondo_tsk(lstart,lend,lstep,rstart,rend,rstep,flag,testtbl)
iftesttblthenreturncombine(lstart,lend,lstep,rstart,rend,rstep,flag,testtbl)end
localmret=move(lstart,lend,lstep,rstart,rend,rstep,flag)
localcret=combine(lstart,lend,lstep,rstart,rend,rstep,flag)
ifnotmretandnotcretthenreturnfalseend
core.score=core.score+1
move(lstart,lend,lstep,rstart,rend,rstep,flag)
returntrue
end
functioncore.up_move(testtbl)
returndo_tsk(1,3,1,1,4,1,"up",testtbl)
end
functioncore.down_move(testtbl)
returndo_tsk(2,0,-1,1,4,1,"down",testtbl)
end
functioncore.left_move(testtbl)
returndo_tsk(0,3,1,2,4,1,"left",testtbl)
end
functioncore.right_move(testtbl)
returndo_tsk(0,3,1,3,1,-1,"right",testtbl)
end
functioncore.new_block()
localval=love.math.random()
ifval<0.8thenval=2elseval=4end
localempty_tbl={}
fori=1,16do
ifnotcore.block[i]then
table.insert(empty_tbl,i)
end
end
if#empty_tbl==1then
return{index=empty_tbl[1],value=val}
end
localpos=love.math.random(1,#empty_tbl)
return{index=empty_tbl[pos],value=val}
end
get_best()
returncore
main.lua
localcore=require("core")
localblock_pic={}
localbk
localover_flag=false
localnew_block={flag=false}
localwH --windowheight
localwW --windowweight
localbW --blockwidth
localstartpos={}
localdelay=0
functionlove.load()
love.window.setFullscreen()
wH=love.window.getHeight()
wW=love.window.getWidth()
bW=0.8*wH/4
bk=love.graphics.newImage("src/bk.jpg")
fori=1,11do
block_pic[tostring(math.pow(2,i))]=love.graphics.newImage("src/"..tostring(math.pow(2,i))..".PNG")
end
love.graphics.setBackgroundColor(255,255,255)
love.graphics.setNewFont(24)
love.graphics.setColor(255,255,255)
core.initial()
end
localfunctiondraw_block(index,value)
localline=math.modf((index-1)/4)
localrow=(index-1)%4
localpic_index=tostring(value)
love.graphics.draw(block_pic[pic_index],0.1*wH+row*bW,0.1*wH+line*bW,0,bW/block_pic[pic_index]:getWidth(),bW/block_pic[pic_index]:getHeight())
end
functionlove.draw()
localscorestr="SCORE:\n"..core.score.."\nBEST:\n"..core.best
love.graphics.draw(bk,0,0,0,wW/bk:getWidth(),wH/bk:getHeight())
love.graphics.setColor(255,255,255)
love.graphics.rectangle("line",0.1*wH,0.1*wH,0.8*wH,0.8*wH)
fori=1,16do
ifcore.block[i]then
draw_block(i,core.block[i])
end
end
ifnew_block.flagthen
ifdelay<10thendelay=delay+1
else
draw_block(new_block.index,new_block.value)
core.block[new_block.index]=new_block.value
new_block.flag=false
delay=0
end
end
love.graphics.print(scorestr,wH,wH*0.1)
ifover_flagthen
love.graphics.setColor(0,0,255)
love.graphics.rectangle("fill",0.25*wW,0.25*wH,0.5*wW,0.5*wH)
love.graphics.setColor(255,255,255)
love.graphics.print(scorestr,0.45*wW,0.45*wH)
end
end
functionlove.mousepressed(x,y,button)
ifbutton=='l'then
startpos.x=x
startpos.y=y
end
end
functionlove.mousereleased(x,y,button)
ifbutton=='l'then
ifover_flagthen
over_flag=false
core.initial()
return
end
localx_dis=x-startpos.x
localy_dis=y-startpos.y
localret
ify_dis<0andmath.abs(y_dis)>math.abs(x_dis)then
ret=core.up_move()
elseify_dis>0andmath.abs(y_dis)>math.abs(x_dis)then
ret=core.down_move()
elseifx_dis<0andmath.abs(x_dis)>math.abs(y_dis)then
ret=core.left_move()
elseifx_dis>0andmath.abs(x_dis)>math.abs(y_dis)then
ret=core.right_move()
end
ifnotretthenreturnend
new_block=core.new_block()
ifnotnew_blockthenreturnend
new_block.flag=true
localtesttbl=core.tblclone(core.block,16)
testtbl[new_block.index]=new_block.value
ifcore.isfull(testtbl)then
ifcore.up_move(testtbl)orcore.down_move(testtbl)orcore.left_move(testtbl)orcore.right_move(testtbl)then
return
end
core.set_best()
over_flag=true
end
end
end
以上便是本文的全部内容了,希望大家能够喜欢。也希望通过这几个2048小游戏的代码,能给到大家一些帮助