lua实现的2048小游戏

时间:2022-11-29 10:37:23 作者:無名好 综合材料 收藏本文 下载本文

“無名好”通过精心收集,向本站投稿了6篇lua实现的2048小游戏,下面是小编整理后的lua实现的2048小游戏,希望能帮助到大家!

篇1:lua实现的2048小游戏

之前给大家分享了好几个语言版本的2048了,今天再给大家分享一个lua脚本实现的2048小游戏,小伙伴们参考下吧,

lua实现的2048小游戏,只要可以运行lua脚本的环境下都可以玩。

代码如下:

--[[=============================================================================

#    FileName: 2048.lua

#        Desc: lua console 2048

#      Author: hanxi

#       Email: hanxi.info@gmail.com

#    HomePage: www.hanxi.info

#     Version: 0.0.1

#  LastChange: 2014-04-28 11:05:09

#     History:

=============================================================================]]

local function initGrid(m,n)

local grid = {}

for i=1,m do

if not grid[i] then

grid[i] = {}

end

for j=1,n do

grid[i][j] = 0

end

end

return grid

end

local function printGrid(grid)

local celllen = 8 -- 每个格子占用字符数

local gridStrLines = {}

table.insert(gridStrLines,“-------------------------------------”)

for i,row in ipairs(grid) do

local line = {}

for _,num in ipairs(row) do

if num==0 then

local pres = “”

for tmp=1,celllen do

pres = pres .. “ ”

end

local s = string.format(“%s”,pres)

table.insert(line,s)

else

local s = tostring(num)

local l = string.len(s)

local l = (celllen-l)/2

local prel = math.floor(l)

local sufl = math.ceil(l)

local pres = “”

for tmp=1,prel do

pres = pres .. “ ”

end

local sufs = pres

if sufl>prel then

sufs = pres.. “ ”

end

local s = string.format(“%s%s%s”,pres,s,sufs)

table.insert(line,s)

end

end

local line = table.concat(line,“|”)

line = “|” .. line .. “|”

table.insert(gridStrLines,line)

table.insert(gridStrLines,“-------------------------------------”)

end

local gridStr = table.concat(gridStrLines,“n”)

print(gridStr)

end

local function randomGrid(grid)

local m = #grid

local n = #grid[1]

for i=1,m do

for j=1,n do

local r = math.random(1,5)

local num = 2^r

grid[i][j] = num

end

end

end

local function getRandomZeroPos(grid)

local m = #grid

local n = #grid[1]

local zeros = {}

for i=1,m do

for j=1,n do

if grid[i][j]==0 then

table.insert(zeros,{i=i,j=j})

end

end

end

if #zeros>0 then

local r = math.random(1,#zeros)

return zeros[r].i,zeros[r].j

end

end

local function randomNum(grid)

local i,j = getRandomZeroPos(grid)

if i and j then

local r = math.random

if r<0.9 then

grid[i][j] = 2

else

grid[i][j] = 4

end

return i,j

end

end

local function moveLeft(grid)

print(“==============moveLeft===============”)

local m = #grid

local n = #grid[1]

for i=1,m do

local line = {}

for j=1,n do

if grid[i][j]~=0 then

table.insert(line,grid[i][j])

end

end

local k=#line

for j=1,n do

if j<=k then

grid[i][j] = line[j]

else

grid[i][j] = 0

end

end

for j=1,k-1 do

if grid[i][j]==grid[i][j+1] then

grid[i][j+1] = grid[i][j] + grid[i][j+1]

for x=j,n-1 do

grid[i][x] = grid[i][x+1]

end

grid[i][n] = 0

end

end

end

end

local function moveRight(grid)

print(“==============moveRight==============”)

local m = #grid

local n = #grid[1]

for i=1,m do

local line = {}

for j=n,1,-1 do

if grid[i][j]~=0 then

table.insert(line,grid[i][j])

end

end

local k = #line

for j=n,1,-1 do

if n-j+1<=k then

grid[i][j] = line[n-j+1]

else

grid[i][j] = 0

end

end

for j=n,n-k+2,-1 do

if grid[i][j]==grid[i][j-1] then

grid[i][j-1] = grid[i][j] + grid[i][j-1]

for x=j,2,-1 do

grid[i][x] = grid[i][x-1]

end

grid[i][1] = 0

end

end

end

end

local function moveUp(grid)

print(“===============moveUp================”)

local m = #grid

local n = #grid[1]

for j=1,n do

local line = {}

for i=1,m do

if grid[i][j]~=0 then

table.insert(line,grid[i][j])

end

end

local k = #line

for i=1,m do

if i<=k then

grid[i][j] = line[i]

else

grid[i][j] = 0

end

end

for i=1,k-1 do

if grid[i][j]==grid[i+1][j] then

grid[i+1][j] = grid[i][j] + grid[i+1][j]

for x=i,m-1 do

grid[x][j] = grid[x+1][j]

end

grid[m][j] = 0

end

end

end

end

local function moveDown(grid)

print(“==============moveDown===============”)

local m = #grid

local n = #grid[1]

for j=1,n do

local line = {}

for i=m,1,-1 do

if grid[i][j]~=0 then

table.insert(line,grid[i][j])

end

end

local k = #line

for i=m,1,-1 do

if m-i+1<=k then

grid[i][j] = line[m-i+1]

else

grid[i][j] = 0

end

end

for i=m,m-k+2,-1 do

if grid[i][j]==grid[i-1][j] then

grid[i-1][j] = grid[i][j] + grid[i-1][j]

for x=i,2,-1 do

grid[x][j] = grid[x-1][j]

end

grid[1][j] = 0

end

end

end

end

local function canMove(grid)

local m = #grid

local n = #grid[1]

for i=1,m do

for j=1,n do

if grid[i][j]==0 then

return true

end

if (i

and (grid[i][j]==grid[i][j+1]

or grid[i][j]==grid[i+1][j]) then

return true

end

end

end

return false

end

local function main()

local grid = initGrid(4,4)

randomNum(grid)

printGrid(grid)

io.write(“next step ‘a‘[←],‘w‘[↑],‘s‘[↓],‘d‘[→],‘q‘[exit] >>”)

local input = io.read()

while input~=“q” do

if input==“a” or input==“w” or input==“s” or input==“d” then

if input==“a” then

moveLeft(grid)

elseif input==“w” then

moveUp(grid)

elseif input==“s” then

moveDown(grid)

elseif input==“d” then

moveRight(grid)

end

randomNum(grid)

printGrid(grid)

else

print(“error input. please input ‘a‘[←] or ‘w‘[↑] or ‘s‘[↓] or ‘d‘[→] or ‘q‘[exit]”)

end

io.write(“next step ‘a‘[←],‘w‘[↑],‘s‘[↓],‘d‘[→],‘q‘[exit] >>”)

input = io.read()

end

end

main()

以上就是本文所述的全部内容了,希望大家能够喜欢,

篇2:Lua实现类继承

这里给大家演示的是一个使用lua实现类继承的示例,实现类继承其实有很多种写法,这里是本人常用的方法,推荐给大家,

mulInherit.lua

代码如下:

--[[

author:looyer@sina.com

date:2014/7/18

purpose:lua的继承演示

--]]

--- base class “Object”

local bject = {_t = “Object”}

function Object:new(id)

local o =

{

_id = id

}

setmetatable(o, self)

self.__index = self

return o

end

function Object:type()

return self._t

end

function Object:id()

return self._id

end

--- class “Widget” driver from “Object”

local Widget = {_t = “Widget”}

setmetatable(Widget, Object)

function Widget:new(id)

local o =

{

_id = id,

}

setmetatable(o, self)

self.__index = self

-- function (tb, key)

-- return self[key]

-- end

return o

end

-- function Widget:id()

-- return self._id

-- end

--- class “Drawor” driver from “Object”

local Drawor = {_t = “Drawor”}

setmetatable(Drawor, Object)

function Drawor:new(id, name)

local o =

{

_id = id,

_name = name,

}

setmetatable(o, self)

self.__index = self

return o

end

function Drawor:name()

return self._name

end

--- class “DrawPlane” driver from “Widget” and “Drawor”

local DrawPlane = {_t = “DrawPlane”, _father = {Widget, Drawor}}

function DrawPlane:new(id)

local o =

{

_id = id,

}

setmetatable(o, self)

self.__index =

function (tb, key)

if self[key] then return self[key] end

for _, v in pairs(self._father) do

if v._t == key then return v end

if v[key] then return v[key] end

end

end

return o

end

local varA = Object:new()

local varB = Widget:new(10)

local varC = Drawor:new(30, “dwr”)

local varD = DrawPlane:new(51)

print(“varA type:”, varA:type())

print(“varB type:”, varB:type())

print(“varB._id: ”, varB._id, varB:id())

print(“varC name: ”, varC:name())

print(“varD type: ”, varD:type())

print(“varD id: ”, varD:id())

print(“varD father Widget--”, varD.Widget:type())

以上就是本文所要分享的代码了,希望大家能够喜欢,

篇3:lua脚本实现自动生成APK包

作者:sails鸢 字体:[增加 减小] 类型:转载

可以根据需要自行扩展了。

使用前tool path 还有 target path的前两个还是需要自己设置下。

一些小的函数 jit_file copy_file 我就不贴了 比较简单,用来luajit 和 拷贝。

代码如下:

-- Authors: sails鸢@oschina

-- Date: 20th , August , 2014

-- Note:

-- This is used for Cocos2dx + Lua

-- This is a script. to making .APK file for android platform

-- Make sure you have installed java, ant, android sdk, ndk, svn, jit

-- Also plz check and rewrite following paths before you use this script

-- Remarks:

-- The script. will update your cocos engine directory and your Lua script. which probably is Resources

-- then it should jit your Lua files , use ASMaker to encrypt your Lua-jit files

-- all files and resources will move to this folder proj.android/assets

-- finally it will make a .APK package with ANT

require(‘support‘)

--tools paths

local JAVA_HOME = ‘C:\Program Files\Java\jdk1.8.0_05‘

local ANT_HOME = ‘D:\ProgramSoftware\apache-ant-1.9.4‘

local ANDROID_HOME = ‘“D:\ProgramSoftware\android sdk\sdk”‘

local NDK_HOME = ‘D:\ProgramSoftware\android-ndk-r9d-windows-x86_64\android-ndk-r9d‘

local SVN_HOME = ‘C:\Program Files\TortoiseSVN\bin\‘

--target paths

local ENGINE_DIR = ‘D:\engine‘

local WORK_DIR = ‘D:\engine\projects\XXXX\proj.android‘

local RESOURCES_DIR = WORK_DIR ..‘\..\Resources‘

local ASSETS_DIR = WORK_DIR ..‘\assets‘

--function detect directory

local function dir_exist(dir)

return os.execute(string.format(‘pushd “%s”>nul 2>nul && popd‘, dir))

end

--remove old assets

if dir_exist(ASSETS_DIR) then

rmdir(ASSETS_DIR)

end

--remove old APK

local old_apk , err = io.open(WORK_DIR..‘\bin\XXXX-release.apk‘)

if err == nil then

old_apk:close

delfile(WORK_DIR..‘\bin\XXXX-release.apk‘)

end

--svn update

--check

--svn_up(ENGINE_DIR)

--svn_up(WORK_DIR..‘\..‘)

--luajit

--iter directory

local cmd = string.format(“pushd %q &dir /b /s &popd” , RESOURCES_DIR)

local file_list = io.popen(cmd)

for line in file_list:lines() do

line_to = string.gsub(line, ‘Resources‘, ‘Resources_jit‘)

if dir_exist(line) then

check_mk_path(line_to)

else

if(string.find(line,‘.lua$‘)) then

jit_file(WORK_DIR, line , line_to)

else

copy_file(line , line_to)

end

end

end

file_list:close()

--encryption with ASmaker

local enc_cmd = WORK_DIR..‘\ASmaker.exe‘..‘ -i ‘..WORK_DIR..‘\..\Resources_jit‘..“ -o ”..ASSETS_DIR..‘ -f .lua -e .exe‘

local enc_re = run_one_cmd(enc_cmd)

if enc_re:find (“失败”) then

print(“ASmaker加密文件夹失败!”,enc_re)

os.exit(1)

end

--ndk build

local ndk_cmd = ‘call ‘..NDK_HOME..‘\ndk-build‘..‘ -C ‘..WORK_DIR..‘ ‘..‘NDK_MODULE_PATH=‘..ENGINE_DIR..‘;‘..ENGINE_DIR..‘\cocos2dx\platform\third_party\android\prebuilt‘

local ndk_re = run_one_cmd(ndk_cmd)

if ndk_re:find (“error”) then

print(“NDK build失败!”,ndk_re)

os.exit(1)

end

--android update

local and_cmd = ‘call ‘..ANDROID_HOME..‘\tools\android‘..‘ update project -p ‘..WORK_DIR

local and_re = run_one_cmd(and_cmd)

and_cmd = ‘call “‘..ANDROID_HOME..‘\tools\android”‘..‘ update lib-project -p ‘..ENGINE_DIR..‘\cocos2dx\platform\android\java‘

and_re = run_one_cmd(and_cmd)

--ant

local ant_cmd = ‘pushd ‘..WORK_DIR..‘&call ‘..ANT_HOME..‘\bin\ant release‘

local ant_re = run_one_cmd(ant_cmd)

if ant_re:find (“failed”) then

print(“生成APK失败!”,ant_re)

os.exit(1)

end

以上代码就是本文所要分享的全部内容了,希望大家能够喜欢,

篇4:c语言实现猜数字小游戏

#include#include#include void fun(int c ){ srand((unsigned)time(NULL)); int ret = rand() % 100 + 1; printf(“请输入一个数>”); while (1) { scanf(“%d”, &c); if (c == ret) { printf(“正确n”); break; } else if (c >ret) { printf(“猜大了n”); } else { printf(“猜小了n”); } }} void menu() { printf(“********************n”); printf(“****游戏开始********n”); printf(“********************n”); printf(“*****请选择*********n”); printf(“********************n”); printf(“****1 游戏开始******n”); printf(“********************n”); printf(“****2 游戏结束******n”); printf(“********************n”); printf(“********************n”); }int main(){ int c = 0; int num = 0; int input = 0; while (1) { menu(); printf(“请选择n”); scanf(“%d”, &input); switch (input) { case 1: printf(“游戏开始n”); fun( num); break; { case 2: printf(“游戏结束n”); break; } } //break; } printf(“%d”, c); system(“pause”); return 0;

篇5:使用lua实现php的vardump函数功能

习惯了php中的var_dump函数,而如今写lua的时候总习惯使用var_dump()函数,于是就自己动手写了一个类似功能的var_dump()函数,

代码如下:

function var_dump(data, max_level, prefix)

if type(prefix) ~= “string” then

prefix = “”

end

if type(data) ~= “table” then

print(prefix .. tostring(data))

else

print(data)

if max_level ~= 0 then

local prefix_next = prefix .. “   ”

print(prefix .. “{”)

for k,v in pairs(data) do

io.stdout:write(prefix_next .. k .. “ = ”)

if type(v) ~= “table” or (type(max_level) == “number” and max_level <= 1) then

print(v)

else

if max_level == nil then

var_dump(v, nil, prefix_next)

else

var_dump(v, max_level - 1, prefix_next)

end

end

end

print(prefix .. “}”)

end

end

end

篇6:Lua 学习笔记之C API 遍历 Table实现代码

作者:TimothyQiu 字体:[增加 减小] 类型:转载

这篇文章主要介绍了Lua 学习笔记之C API 遍历 Table实现代码,需要的朋友可以参考下

Lua 通过一个虚拟栈与 C 的交互,正数索引自底向上取值,负数索引自顶向下取值,

Lua 中的 Table(表)结构可以使用任何数据作为 key 进行取值。使用 C API 访问 Table 中的元素有两种方法:

代码如下:

lua_getglobal(L, t);

lua_pushinteger(L, k); -- 这里可以换成其它类型的 lua_pushXXXX(L, k) 压数据到栈顶作key

lua_gettable(L, -2);

lua_getglobal(L, t);

lua_getfield(L, -1, k);

在结束时,栈上的情况均为:栈顶为 t[k],次顶元素为 Table 类型的 t。第二种方法其实是第一种方法在「key 为字符串」时的特殊写法。

C API 遍历 Table

代码如下:

lua_getglobal(L, t);

lua_pushnil(L);

while (lua_next(L, -2)) {

/* 此时栈上 -1 处为 value, -2 处为 key */

lua_pop(L, 1);

}

lua_next 函数针对 -2 处(参数指定)的 Table 进行遍历,

弹出 -1 处(栈顶)的值作为上一个 key(为 nil 时视为请求首个 key),压入 Table 中的下一个 key 和 value。返回值表示是否存在下一个 key。

另外在循环中处理值时要记得随时清理栈,否则 Table 就不在 -2 了。(也可以考虑在 lua_getglobal 后用 lua_gettop 存下 Table 的正数索引。)

虽然这是手册中记载的遍历方法,但这种方法在遍历时并没有一定的遍历顺序,于是便又有了下面的方法。

用整数 Key 进行并不那么完美的遍历

代码如下:

lua_getglobal(L, t);

len = lua_objlen(L, -1);

for (i = 1; i <= len; i++) {

lua_pushinteger(L, i);

lua_gettable(L, -2);

/* 此时栈顶即为 t[i] 元素 */

lua_pop(L, 1);

}

这种方法无视了非整数 key,但可以保证遍历顺序。如果只关注整数 key,可以考虑用这种遍历方法 :)

Lua函数用法研究

早会经典小游戏

班会小游戏

经典成语猜谜小游戏

早会小游戏:大风吹

娱乐小游戏脑筋急转弯

春节聚会小游戏

幼儿园国旗下小游戏

的猜成语小游戏

猜迷经典小游戏成语

lua实现的2048小游戏(集锦6篇)

欢迎下载DOC格式的lua实现的2048小游戏,但愿能给您带来参考作用!
推荐度: 推荐 推荐 推荐 推荐 推荐
点击下载文档 文档为doc格式
点击下载本文文档