加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

lua+love2d 2048游戏

发布时间:2020-12-14 22:55:49 所属栏目:大数据 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 core = {}core.block = {}core.score = 0core.best = 0love.filesystem.setIdentity("2048")local function get_best() if not love.filesystem.exist

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

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 do
        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 block = core.block end
    for i = 1,16 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,lstep do
        for j = rstart,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,flag)
    local mflag = false
    local index,kstart,kend,kstep
    for i = lstart,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,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,testtbl)
    if testtbl then return combine(lstart,testtbl) end
    local mret = move(lstart,flag)
    local cret = combine(lstart,flag)
    if not mret and not cret then return false end
    core.score = core.score + 1
    move(lstart,flag)
    return true
end

function core.up_move(testtbl)
    return do_tsk(1,3,1,4,"up",testtbl)
end

function core.down_move(testtbl)
    return do_tsk(2,-1,"down",testtbl)
end

function core.left_move(testtbl)
    return do_tsk(0,2,"left",testtbl)
end

function core.right_move(testtbl)
    return do_tsk(0,"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,16 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

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读