Lua+love2d made 2048 games _lua

Source: Internet
Author: User
Tags abs lua pow

PC version 2048 games with LUA and love2d are available for Linux and Windows platforms. Depending on the love2d game engine, love2d needs to be 0.9 and above.

Core.lua

Copy Code code as follows:

Core = {}
Core.block = {}
Core.score = 0
Core.best = 0
Love.filesystem.setIdentity ("2048")
Local function Get_best ()
If not love.filesystem.exists ("best") Then
Core.best = 0
Return
End
Core.best = Love.filesystem.read ("Best")
Core.best = Tonumber (core.best)
End
function core.initial ()
Core.block = {}
Local POS1 = Love.math.random (1, 16)
Local Pos2
While True
Pos2 = Love.math.random (1, 16)
If Pos2 ~= pos1 then break end
End
Local Val
val = Love.math.random ()
If Val < 0.8 then val = 2 Else val = 4 End
CORE.BLOCK[POS1] = val
val = Love.math.random ()
If Val < 0.8 then val = 2 Else val = 4 End
CORE.BLOCK[POS2] = val
Core.score = 0
End
function Core.set_best ()
If Core.score > Core.best Then
Core.best = Core.score
Local ret, err = Love.filesystem.write ("Best", core.best)
End
End
function core.tblclone (t1, num)
Local t2 = {}
For i = 1, num do
T2[i] = T1[i]
End
Return T2
End
function Core.isfull (TESTTBL)
Local block
If TESTTBL then block = testtbl else blocks = Core.block End
For i = 1, do
If not Block[i] then return False end
End
return True
End
Local function combine (Lstart, lend, Lstep, Rstart, rend, rstep, flag, TESTTBL)
Local index
Local Tflag, block
If Testtbl Then
Tflag = True
block = Testtbl
Else
block = Core.block
End
Local Cflag = False
For i = Lstart, lend, Lstep do
For j = Rstart, rend, rstep do
If flag = = "Up" then index = (i-1) * 4 + J
ElseIf flag = = "Down" then index = (i + 1) * 4 + J
ElseIf flag = = "Left" then index = i * 4 + j-1
else index = i * 4 + j + 1 End
If Block[index] and Block[i * 4 + j] and
Block[index] = = Block[i * 4 + j] and
Block[index] < 2048 Then
Cflag = True
If Tflag then return Cflag end
Block[index] = 2 * block[i * 4 + j]
Block[i * 4 + j] = Nil
Core.score = Core.score + Block[index]
End
End
End
Return Cflag
End
Local function Move (Lstart, lend, Lstep, Rstart, rend, rstep, flag)
Local Mflag = False
Local index, Kstart, Kend, Kstep
For i = Lstart, lend, Lstep do
For j = Rstart, rend, rstep do
If flag = = "Up" Then
Kstart = 0
Kend = i-1
Kstep = 1
ElseIf flag = = "Down" Then
Kstart = 3
Kend = i + 1
Kstep =-1
ElseIf flag = = "Left" then
Kstart = 1
Kend = j-1
Kstep = 1
Else
Kstart = 4
Kend = j + 1
Kstep =-1
End
For k = Kstart, kend, Kstep do
If flag = = "Up" or flag = "Down" then index = k * 4 + J
else index = i * 4 + K End
If not Core.block[index] and Core.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
Return Mflag
End
Local function Do_tsk (Lstart, lend, Lstep, Rstart, rend, rstep, flag, TESTTBL)
If Testtbl then return combine (Lstart, lend, Lstep, Rstart, rend, rstep, flag, TESTTBL) end
Local Mret = Move (Lstart, lend, Lstep, Rstart, rend, rstep, flag)
Local CRET = Combine (Lstart, lend, Lstep, Rstart, rend, rstep, flag)
If not Mret and is Cret then return False end
Core.score = Core.score + 1
Move (Lstart, lend, Lstep, Rstart, rend, rstep, flag)
return True
End
function Core.up_move (TESTTBL)
Return Do_tsk (1, 3, 1, 1, 4, 1, "Up", TESTTBL)
End
function Core.down_move (TESTTBL)
Return Do_tsk (2, 0,-1, 1, 4, 1, ' Down ', TESTTBL)
End
function Core.left_move (TESTTBL)
Return Do_tsk (0, 3, 1, 2, 4, 1, "left", Testtbl)
End
function Core.right_move (TESTTBL)
Return Do_tsk (0, 3, 1, 3, 1,-1, ' right ', testtbl)
End
function Core.new_block ()
Local val = Love.math.random ()
If Val < 0.8 then val = 2 Else val = 4 End
Local empty_tbl = {}
For i = 1, do
If not core.block[i] Then
Table.insert (EMPTY_TBL, i)
End
End
if #empty_tbl = = 1 Then
return {index = empty_tbl[1], value = val}
End
Local pos = love.math.random (1, #empty_tbl)
return {index = Empty_tbl[pos], value = val}
End
Get_best ()
return core

Main.lua

Copy Code code as follows:

Local core = require ("core")
Local block_pic = {}
Local BK
Local Over_flag = False
Local New_block = {flag = false}
Local WH--window Height
Local WW--window Weight
Local BW--block width
Local startpos = {}
Local delay = 0
function Love.load ()
Love.window.setFullscreen ()
WH = Love.window.getHeight ()
WW = Love.window.getWidth ()
BW = 0.8 * WH/4
BK = Love.graphics.newImage ("src/bk.jpg")
For i = 1, one do
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
Local function Draw_block (index, value)
Local line = MATH.MODF ((index-1)/4)
Local row = (index-1)% 4
Local Pic_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
function Love.draw ()
Local SCORESTR = "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)
For i = 1, do
If Core.block[i] Then
Draw_block (i, core.block[i])
End
End
If New_block.flag Then
If delay < then delay = 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)
If Over_flag Then
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
function love.mousepressed (x, Y, button)
if button = = ' L ' Then
Startpos.x = X
Startpos.y = y
End
End
function love.mousereleased (x, Y, button)
if button = = ' L ' Then
If Over_flag Then
Over_flag = False
Core.initial ()
Return
End
Local X_dis = x-startpos.x
Local Y_dis = Y-startpos.y
Local RET
If Y_dis < 0 and Math.Abs (y_dis) > Math.Abs (X_dis) Then
ret = Core.up_move ()
ElseIf y_dis > 0 and Math.Abs (y_dis) > Math.Abs (X_dis) Then
ret = Core.down_move ()
ElseIf X_dis < 0 and Math.Abs (x_dis) > Math.Abs (Y_dis) Then
ret = Core.left_move ()
ElseIf x_dis > 0 and Math.Abs (x_dis) > Math.Abs (Y_dis) Then
ret = Core.right_move ()
End
If not RET then return end
New_block = Core.new_block ()
If not New_block then return end
New_block.flag = True
Local TESTTBL = Core.tblclone (Core.block, 16)
Testtbl[new_block.index] = New_block.value
If Core.isfull (TESTTBL) Then
If Core.up_move (TESTTBL) or Core.down_move (TESTTBL) or Core.left_move (TESTTBL) or Core.right_move (TESTTBL) Then
Return
End
Core.set_best ()
Over_flag = True
End
End
End

The above is the entire content of this article, I hope you can enjoy. Also hope that through these 2048 small game code, can give you some help

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.