好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁(yè) > 知識(shí)庫(kù) > lua+love2d制作的2048游戲

lua+love2d制作的2048游戲

熱門標(biāo)簽:新岸線智能電銷機(jī)器人 漳州智云呼電話機(jī)器人 武漢外呼防封系統(tǒng)多少錢 個(gè)人怎么在地圖標(biāo)注需要的店鋪 百度地圖標(biāo)注早餐區(qū)域 怎么去除地圖標(biāo)注 清朝地圖標(biāo)注哈爾濱 地圖標(biāo)注大廈 冀州市地圖標(biāo)注

使用lua和love2d編寫的pc版2048游戲,適用于linux和windows平臺(tái)。依賴love2d游戲引擎,love2d需0.9及以上版本。

core.lua

復(fù)制代碼 代碼如下:

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

main.lua

復(fù)制代碼 代碼如下:

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, 11 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, 16 do
        if core.block[i] then
            draw_block(i, core.block[i])
        end
    end
    if new_block.flag then
        if delay 10 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

以上便是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。也希望通過(guò)這幾個(gè)2048小游戲的代碼,能給到大家一些幫助

您可能感興趣的文章:
  • lua實(shí)現(xiàn)的2048小游戲
  • Lua腳本語(yǔ)言簡(jiǎn)明入門教程
  • Lua游戲開發(fā)教程之時(shí)區(qū)問(wèn)題詳解

標(biāo)簽:金昌 宣城 儋州 臺(tái)灣 天門 濰坊 德宏 天門

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《lua+love2d制作的2048游戲》,本文關(guān)鍵詞  lua+love2d,制作,的,2048,游戲,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《lua+love2d制作的2048游戲》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于lua+love2d制作的2048游戲的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 午夜在线精品偷拍| 山村艳妇全肉乱妇TXT| パイパン熟女に中出しPorn| 都市激色第一页| 舌吻视频| 扒開腿灌牛奶??調教双楠| 911精品国自产在线偷拍| 无遮挡中文毛片免费观看| 国产未成女年一区二区| 总受调教骚浪受扇烂H| 欧美午夜一艳片欧美精品| 吉泽新婚上司出差7天| 天堂黃色A片免费蜜月Av| 秋芬小丹你就再给我一次吧| 亚洲性电影| 337P粉嫩大胆色噜噜嚕动态图| 隔壁的女孩在线观看| 亚洲国产系列一区二区三区| 日美女逼视频| 91视频毛片| 男女啪啪免费软件下载| 被拖进小树林C了好爽H动图| 欧美h肉| 爱插美女| 91资源在线| 久久夜色精品亚洲AV三区| 国产99999久久久久精品小说| 久久国产精品露脸精品国产蜜桃| 日韩激情电影在线观看| 爽?好大?快?深点H樱花视频| 一级黄色绿像片| 成人精品一区二区三区尤物仙踪林| 中文字幕av人妻一本二本| 最新国产精品亚洲二区| 我把寡妇日出水好爽视频| 被室友cao翻了H双性| 亚洲VA欧美va国产va妖精| 日本不卡在线视频高清免费| bl小黄文| 五十六十老熟女HD60| 免费Av鲁丝片|