function addBuffer1() -- 循环检测和补充buffer1
while true do -- 主循环
if getColor(100, 100) ~= 0x000000 then -- 如果没有buffer1
touchDown(0, 100, 100); -- 释放buffer1
touchUp(0);
end
mSleep(500);
coroutine.yield(); -- 主动挂起自己
end
end
function addBuffer2() -- 循环检测和补充buffer2
while true do -- 主循环
if getColor(200, 200) ~= 0x000000 then -- 如果没有buffer2
touchDown(0, 200, 200); -- 释放buffer2
touchUp(0);
end
mSleep(500);
coroutine.yield(); -- 主动挂起自己
end
end
function addBuffer3() -- 循环检测和补充buffer3
while true do -- 主循环
if getColor(300, 300) ~= 0x000000 then -- 如果没有buffer3
touchDown(0, 300, 300); -- 释放buffer3
touchUp(0);
end
mSleep(500);
coroutine.yield(); -- 主动挂起自己
end
end
function addBuffer4() -- 循环检测和补充buffer4
while true do -- 主循环
if getColor(400, 400) ~= 0x000000 then -- 如果没有buffer4
touchDown(0, 400, 400); -- 释放buffer4
touchUp(0);
end
mSleep(500);
coroutine.yield(); -- 主动挂起自己
end
end
function addBuffer5() -- 循环检测和补充buffer5
while true do -- 主循环
if getColor(500, 500) ~= 0x000000 then -- 如果没有buffer5
touchDown(0, 500, 500); -- 释放buffer5
touchUp(0);
end
mSleep(500);
coroutine.yield(); -- 主动挂起自己
end
end
function kill() -- 循环打怪一直到怪物死亡
while true do
if getColor(600, 600) ~= 0x000000 then -- 如果怪物没死
touchDown(0, 1000, 1000); -- 释放一次技能
touchUp(0);
end
mSleep(500);
coroutine.yield(); -- 主动挂起自己
end
end
function main()
co1 = coroutine.create(addBuffer1);
co2 = coroutine.create(addBuffer2);
co3 = coroutine.create(addBuffer3);
co4 = coroutine.create(addBuffer4);
co5 = coroutine.create(addBuffer5);
co6 = coroutine.create(kill);
while true do
coroutine.resume(co1);
coroutine.resume(co2);
coroutine.resume(co3);
coroutine.resume(co4);
coroutine.resume(co5);
coroutine.resume(co6);
end
while true do -- 主循环
if getColor(100, 100) ~= 0x000000 then -- 如果没有buffer1
touchDown(0, 100, 100); -- 释放buffer1
touchUp(0);
end
mSleep(500);
coroutine.yield(); -- 主动挂起自己
end
end
function addBuffer2() -- 循环检测和补充buffer2
while true do -- 主循环
if getColor(200, 200) ~= 0x000000 then -- 如果没有buffer2
touchDown(0, 200, 200); -- 释放buffer2
touchUp(0);
end
mSleep(500);
coroutine.yield(); -- 主动挂起自己
end
end
function addBuffer3() -- 循环检测和补充buffer3
while true do -- 主循环
if getColor(300, 300) ~= 0x000000 then -- 如果没有buffer3
touchDown(0, 300, 300); -- 释放buffer3
touchUp(0);
end
mSleep(500);
coroutine.yield(); -- 主动挂起自己
end
end
function addBuffer4() -- 循环检测和补充buffer4
while true do -- 主循环
if getColor(400, 400) ~= 0x000000 then -- 如果没有buffer4
touchDown(0, 400, 400); -- 释放buffer4
touchUp(0);
end
mSleep(500);
coroutine.yield(); -- 主动挂起自己
end
end
function addBuffer5() -- 循环检测和补充buffer5
while true do -- 主循环
if getColor(500, 500) ~= 0x000000 then -- 如果没有buffer5
touchDown(0, 500, 500); -- 释放buffer5
touchUp(0);
end
mSleep(500);
coroutine.yield(); -- 主动挂起自己
end
end
function kill() -- 循环打怪一直到怪物死亡
while true do
if getColor(600, 600) ~= 0x000000 then -- 如果怪物没死
touchDown(0, 1000, 1000); -- 释放一次技能
touchUp(0);
end
mSleep(500);
coroutine.yield(); -- 主动挂起自己
end
end
function main()
co1 = coroutine.create(addBuffer1);
co2 = coroutine.create(addBuffer2);
co3 = coroutine.create(addBuffer3);
co4 = coroutine.create(addBuffer4);
co5 = coroutine.create(addBuffer5);
co6 = coroutine.create(kill);
while true do
coroutine.resume(co1);
coroutine.resume(co2);
coroutine.resume(co3);
coroutine.resume(co4);
coroutine.resume(co5);
coroutine.resume(co6);
end
end
LuaSocket和Lua Loop
Lua Loop: http://loop.luaforge.net/
LuaSocket: http://www.tecgraf.puc-rio.br/~diego/professional/luasocket/
感觉用这个写个简单的聊天服务器,或者写个文字Mud的网络小游戏挺方便
运行很简单,只要将LuaSocket放在下面代码说在的路径,然后用lua5.0运行。
--
server端Lua代码
socket = require( " socket " );
host = host or " localhost " ;
port = port or " 8383 " ;
server = assert(socket.bind(host, port, 1000 ))
server:settimeout( 0 )
local client = {}
print( " server: waiting for client connection " )
local clientcount = 0
while 1 do
control = server:accept()
if control ~= nil then
client[control] = control
clientcount = clientcount + 1
print( " 有新客户端连入链接总数为: " .. clientcount .. " \n " )
end
for user in pairs( client ) do
command = user:receive();
if command ~= nil then
print( command )
end
end
end
socket = require( " socket " );
host = host or " localhost " ;
port = port or " 8383 " ;
server = assert(socket.bind(host, port, 1000 ))
server:settimeout( 0 )
local client = {}
print( " server: waiting for client connection " )
local clientcount = 0
while 1 do
control = server:accept()
if control ~= nil then
client[control] = control
clientcount = clientcount + 1
print( " 有新客户端连入链接总数为: " .. clientcount .. " \n " )
end
for user in pairs( client ) do
command = user:receive();
if command ~= nil then
print( command )
end
end
end
--
Client端Lua代码
local MaxLink = 220
local socket = require( " socket " )
local c = {}
host = host or " localhost "
port = port or 8383
if arg then
host = arg[ 1 ] or host
port = arg[ 2 ] or port
end
print( " Attempting connection to host ' " ..host.. " ' and port " ..port.. " " )
for i = 1 , MaxLink do
c[i] = assert(socket.connect(host, port))
c[i]:settimeout( 0 )
end
print( " Connected! Please type stuff (empty line to stop): " )
l = io.read()
for i = 1 , MaxLink do
assert( c[i]:send(l .. " \n " ) )
assert( c[i]:send( " test " .. i .. " \n " ) )
print( " test " .. i .. " \n " )
end
while 1 do
for i = 1 , MaxLink do
command = c[i]:receive()
if command ~= nil then
print(command)
end
end
end
local MaxLink = 220
local socket = require( " socket " )
local c = {}
host = host or " localhost "
port = port or 8383
if arg then
host = arg[ 1 ] or host
port = arg[ 2 ] or port
end
print( " Attempting connection to host ' " ..host.. " ' and port " ..port.. " " )
for i = 1 , MaxLink do
c[i] = assert(socket.connect(host, port))
c[i]:settimeout( 0 )
end
print( " Connected! Please type stuff (empty line to stop): " )
l = io.read()
for i = 1 , MaxLink do
assert( c[i]:send(l .. " \n " ) )
assert( c[i]:send( " test " .. i .. " \n " ) )
print( " test " .. i .. " \n " )
end
while 1 do
for i = 1 , MaxLink do
command = c[i]:receive()
if command ~= nil then
print(command)
end
end
end