Tutorial AdHoc LUA -------------------------------------------
-- TUTO HECHO POR PIPAGERARDO --
-------------------------------------------
--------------------------------------------------------------------------------------
-- PRIMERO DEFINIMOS LAS VARIABLES DEL TUTORIAL ADHOC --------------------------------
--------------------------------------------------------------------------------------
blanco = Color.new( 255, 255, 255 )
negro = Color.new( 0, 0, 0 )
pad = Controls.read() -- Variable que lee el teclado.
oldpad = pad -- Teclado estado anterior.
SA = 60 -- Sensibilidad del Anal�gico.
fin_espera = true
fin_juego = false
fin_partida = false
nickname = System.nickName()
nickname = tostring(nickname)
macaddress = Adhoc.getMac()
macaddress = tostring(macaddress)
Adhoc_state = 0
mensaje_recibido = ""
mensaje_enviado = ""
ultimo_mensaje_recibido = ""
ultimo_mensaje_enviado = ""
mensaje = 65
caracter = "A"
jugador = {}
jugador[1] = {
sprite = img_sprite_1,
vida = 99, -- 100 Vida m�xima / 0 = Destruido.
p_fuego = 5, -- Da�o inflingido.
combustible = 99, -- Litros de Combustible.
municion = 99, -- Numero de Proyectiles.
x = 50, -- Posicion X.
y = 136, -- Posicion Y.
direccion = 0, -- Direccion del tanque en grados 0 - 360.
accion = true, -- True = Operativo / False = Destruido.
en_juego = true -- Si juega o no.
}
jugador[2] = {
sprite = img_sprite_2,
vida = 99, -- 100 Vida m�xima / 0 = Destruido.
p_fuego = 5, -- Da�o inflingido.
combustible = 99, -- Litros de Combustible.
municion = 99, -- Numero de Proyectiles.
x = 430, -- Posicion X.
y = 136, -- Posicion Y.
direccion = 0, -- Direccion del tanque en grados 0 - 360.
accion = true, -- True = Operativo / False = Destruido.
en_juego = true -- Si juega o no.
}
--------------------------------------------------------------------------------------
-- DEFINIMOS LAS FUNCIONES DEL TUTORIAL ADHOC ----------------------------------------
--------------------------------------------------------------------------------------
function reiniciar_nivel() -- TU CODIGO -- end
function pintar_fondo_juego() -- TU CODIGO -- end
function mueve_sprite_arriba( numero ) -- TU CODIGO -- end
function mueve_sprite_abajo( numero ) -- TU CODIGO -- end
function mueve_sprite_derecha( numero ) -- TU CODIGO -- end
function mueve_sprite_izquierda( numero ) -- TU CODIGO -- end
function accion_sprite( numero ) -- TU CODIGO -- end
function dibujar_sprites() -- TU CODIGO -- end
function mostrar_marcadores() -- TU CODIGO -- end
function muestra_pantalla() -- TU CODIGO -- end
function determina_fin_partida_y_ganador() -- TU CODIGO -- end
--------------------------------------------------------------------------------------
-- AVISO DE INTERRUPTOR WLAN ---------------------------------------------------------
--------------------------------------------------------------------------------------
while pcall ( Adhoc.init ) == false do
screen:print( 130, 116, "CONECTA EL INTERRUPTOR WLAN" , blanco )
screen:print( 130, 126, " PULSA X " , blanco )
screen.flip()
fin_espera = true
while fin_espera do
pad = Controls.read()
System.sleep(50)
if pad:cross() then fin_espera = false end
end
end
screen:print( 140, 86, "nickname = " .. nickname , blanco )
screen:print( 140, 96, "macaddress = " .. macaddress , blanco )
screen:print( 130,116, " ESPERANDO ADHOC " , blanco )
screen.flip()
System.sleep(50)
Adhoc.connect()
System.sleep(50)
repeat
Adhoc_state = Adhoc.getState()
System.sleep(50)
until Adhoc_state == 1
--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
-- La idea es transmitir via Adhoc un "String" o cadena de cuatro caracteres: --
-- El primer car�cter sera uno de control que variar� desde el n�mero 65 que es la --
-- letra "A" hasta el n�mero 122 que es la letra "z". Esto es para evitar que falte --
-- un mensaje, por ejemplo: Si nos ha llegado el mensaje n�mero 75 y despu�s nos --
-- llega el mensaje 77, pues est� claro que el 76 no ha llegado. Ahora tenemos que --
-- pedirle a la otra consola que nos repita el mensaje 76. --
-- El segundo car�cter es el de arriba o abajo, como no se pueden pulsar las dos --
-- teclas a la vez si es arriba "U", abajo "D" y no pulsadas "F". --
-- El tercer car�cter es de derecha, izquierda igual que el anterior. Derecha "R", --
-- Izquierda "L" y no pulsadas "F". --
-- Y el cuarto car�cter es de pulsar el bot�n cruz. Pulsado "C", no pulsado "F". --
--------------------------------------------------------------------------------------
fin_juego = false
repeat
reiniciar_nivel()
mensaje_recibido = ""
mensaje_enviado = ""
ultimo_mensaje_recibido = ""
ultimo_mensaje_enviado = ""
mensaje = 65
fin_partida = false
repeat
pintar_fondo_juego()
pad = Controls.read()
if jugador[1].accion then -- Jugador que controlas tu. --
mensaje_enviado = string.char(mensaje) -- Cabecera de mensaje. --
--------------------------------------------------------------------------------
-- SI PULSAS ARRIBA O ABAJO (No creo que puedas pulsar los dos a la vez) -------
--------------------------------------------------------------------------------
if pad:up() or pad:analogY() < -SA then
mensaje_enviado = mensaje_enviado .. "U"
mueve_sprite_arriba( 1 )
elseif pad:down() or pad:analogY() > SA then
mensaje_enviado = mensaje_enviado .. "D"
mueve_sprite_abajo( 1 )
else
mensaje_enviado = mensaje_enviado .. "F"
-- no hay movimiento --
end
--------------------------------------------------------------------------------
-- SI PULSAS DERECHA O IZQUIERDA (No creo que puedas pulsar los dos a la vez) --
--------------------------------------------------------------------------------
if pad:right() or pad:analogX() > SA then
mensaje_enviado = mensaje_enviado .. "R"
mueve_sprite_derecha( 1 )
elseif pad:left() or pad:analogX() < -SA then
mensaje_enviado = mensaje_enviado .. "L"
mueve_sprite_izquierda( 1 )
else
mensaje_enviado = mensaje_enviado .. "F"
-- no hay movimiento --
end
--------------------------------------------------------------------------------
-- SI PULSAS CRUZ --------------------------------------------------------------
--------------------------------------------------------------------------------
if pad:cross() and oldpad:cross() ~= pad:cross() then
mensaje_enviado = mensaje_enviado .. "C"
accion_sprite( 1 )
else
mensaje_enviado = mensaje_enviado .. "F"
-- no hay bot�n pulsado --
end
--------------------------------------------------------------------------------
else
--------------------------------------------------------------------------------
-- Si te han matado pero durante alg�n tiempo sigue la partida -----------------
--------------------------------------------------------------------------------
mensaje_enviado = string.char(mensaje) .. "FFF"
--------------------------------------------------------------------------------
end
-----------------------------------------------------------------------------------
-- Trasmitimos el mensaje a prueba de fallos de transmisi�n -----------------------
-----------------------------------------------------------------------------------
fin_espera = false
repeat
Adhoc.send(mensaje_enviado)
System.sleep(10)
for espera = 1, 5 do
mensaje_recibido = Adhoc.recv()
if string.sub( mensaje_recibido, 1, 1) == string.char(mensaje) then
fin_espera = true
break
end
if mensaje_recibido == ultimo_mensaje_recibido then
Adhoc.send(ultimo_mensaje_enviado)
System.sleep(10)
end
System.sleep(10)
end
until fin_espera
ultimo_mensaje_recibido = mensaje_recibido
ultimo_mensaje_enviado = mensaje_enviado
mensaje = mensaje + 1
if mensaje > 122 then mensaje = 65 end
-----------------------------------------------------------------------------------
oldpad = pad
if jugador[2].accion then -- Jugador contrario u remoto. --
--------------------------------------------------------------------------------
-- SI EL CONTRARIO PULSA ARRIBA O ABAJO ----------------------------------------
--------------------------------------------------------------------------------
caracter = string.sub( mensaje_recibido, 2, 2 )
if caracter == "U" then
mueve_sprite_arriba( 2 )
elseif caracter == "D" then
mueve_sprite_abajo( 2 )
else
-- no hay movimiento --
end
--------------------------------------------------------------------------------
-- SI EL CONTRARIO PULSA DERECHA O IZQUIERDA -----------------------------------
--------------------------------------------------------------------------------
caracter = string.sub( mensaje_recibido, 3, 3 )
if caracter == "R" then
mueve_sprite_derecha( 2 )
elseif caracter == "L" then
mueve_sprite_izquierda( 2 )
else
-- no hay movimiento --
end
--------------------------------------------------------------------------------
-- SI EL CONTRARIO PULSA CRUZ --------------------------------------------------
--------------------------------------------------------------------------------
caracter = string.sub( mensaje_recibido, 4, 4 )
if caracter == "C" then
accion_sprite( 2 )
else
-- no hay bot�n pulsado --
end
--------------------------------------------------------------------------------
else
--------------------------------------------------------------------------------
-- Si has matado al jugador remoto pero durante alg�n tiempo sigue la partida --
--------------------------------------------------------------------------------
end
dibujar_sprites()
mostrar_marcadores()
muestra_pantalla()
determina_fin_partida_y_ganador()
until fin_partida
-----------------------------------------------------------------------------------
-- Trasmitimos el �ltimo mensaje a prueba de fallos de transmisi�n pero esta vez --
-- para evitar que una consola se quede pillada en un bucle cerrado, solo hacemos--
-- cinco bucles de prueba y salimos. ----------------------------------------------
-----------------------------------------------------------------------------------
fin_espera = false
for esperando = 1, 5 do
Adhoc.send(mensaje_enviado)
System.sleep(10)
for espera = 1, 5 do
mensaje_recibido = Adhoc.recv()
if string.sub( mensaje_recibido, 1, 1) == string.char(mensaje) then
fin_espera = true
break
end
if mensaje_recibido == ultimo_mensaje_recibido then
Adhoc.send(ultimo_mensaje_enviado)
System.sleep(10)
end
System.sleep(10)
end
if fin_espera then break end
end
ultimo_mensaje_recibido = mensaje_recibido
ultimo_mensaje_enviado = mensaje_enviado
mensaje = mensaje + 1
if mensaje > 122 then mensaje = 65 end
-----------------------------------------------------------------------------------
until fin_juego
Adhoc.term() -- Cerramos el Adhoc -----
System.sleep(500)
System.memclean() -- Limpiamos la Memoria --
System.sleep(500)
System.Quit() -- Salida al XMB ---------
Por PIPAGERARDO