使用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")
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
复制代码 代码如下:
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
以上便是本文的全部内容了,希望大家能够喜欢。也希望通过这几个2048小游戏的代码,能给到大家一些帮助
lua,love2d,2048
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]