Tutorial De Programacion En LUA - Capitulo 10

Todo sobre la primera portátil de Sony

Moderators: Webmaster, Administrador, Moderador

Post Reply
the_marioga
Colaborador
Colaborador
Posts: 21
Joined: Wednesday, 17 February 2010, 14:56

Tutorial De Programacion En LUA - Capitulo 10

Post by the_marioga »

Capitulo 10: Tablas y Gestion de archivos

Tablas

Recordemos lo que dije en el tutorial anterior

table o tabla
Este tipo de variable lo es todo. Una tabla no es más que una variable que contiene un grupo de variables de cualquier tipo. Una tabla puede contener varios "numbers", varios "string", incluso una tabla puede contener otra tabla. Es mas puede contener diferentes tipos de variables. Ejemplo:

se formula asi: nombredetabla = { contenido de las subvariables de la tabla }

Code: Select all

Ej. numeros = {6, 9, 3}
Cada numero en la tabla tiene su variable. En esta tabla el 6 pertenece a la variable numeros[1], el 9 pertenece a la variable numeros[2] y el 3 pertenece a numeros[3]

Code: Select all

Ej2. imagen = { Image.load("fondo.png"),
Image.load("fondo2.png"),
Image.load("fondo3.png")}
Cada imagen de la tabla tiene una variable al igual k los numeros en la otra. En esta tabla fondo.png pertenece a imagen[1], fondo2 a imagen[2] y la fondo3 pues a imagen[3]

Code: Select all

Ej3. string = { "hola", "que tal", "adios"}
Y Aqui igual que en la otra. hola es string[1], que tal es string[2] y adios es string[3]

Todo claro no? Aprendamos a destruirlas

Para acabar con una tabla es tan sencillo como

Ej1.

Code: Select all

numeros = nil
collectgarbage()
Ej2.

Code: Select all

imagen = nil
collectgarbage()
Ej3.

Code: Select all

string = nil
collectgarbage()

Gestion de archivos

Podemos hacer que la PSP lea o escriba en .txt

Ej1. Escribir

Code: Select all

txt = io.open("ms0:/PSP/GAME/homebrew/texto.txt","w")
txt:write("escrito")
txt:close()
Dentro del TXT quedaria asi: escrito

Ej2. Leer

Code: Select all

texto = io.open("ms0:/PSP/GAME/homebrew/texto.txt","r")
leido = texto:read()
texto:close()
y la variable leido contendria: "escrito"

Para bajar de linea al escribir hay k usar txt:write("\n")
Para bajar de linea al leer simplemente habria k hacer asi

Ej3.

Code: Select all

texto = io.open("ms0:/PSP/GAME/homebrew/texto.txt","r")
leido = texto:read()
leido2 = texto:read()
texto:close()
leido contendria la primera linea, leido2 la segunda linea

Ejemplo de uso de ambos

Un trozo de mi PSP==Wii

Code: Select all

negro = Color.new(0,0,0)
blanco = Color.new(255, 255, 255)
verde = Color.new(0, 180, 0)
txt = io.open("ms0:/PSP/GAME/PspWii/script.txt","r")
seleccionado = txt:read()
txt:close()
txt = nil
collectgarbage()
--Imagenes
fondo = Image.load("ms0:/PSP/GAME/PspWii/Imagenes/fondowii.png")
cursor = Image.load("ms0:/PSP/GAME/PspWii/Imagenes/cursor.png")
icono = { Image.load("ms0:/PSP/GAME/PspWii/Imagenes/menuumd.png"),
Image.load("ms0:/PSP/GAME/PspWii/Imagenes/menufotos.png"),
Image.load("ms0:/PSP/GAME/PspWii/Imagenes/menumii.png")}
if seleccionado == "0" then 
icono4 = Image.load("ms0:/PSP/GAME/PspWii/Imagenes/menuempty.png")
icono4sel = Image.load("ms0:/PSP/GAME/PspWii/Imagenes/menuempty.png")
icono5 = Image.load("ms0:/PSP/GAME/PspWii/Imagenes/menuempty.png")
icono5sel = Image.load("ms0:/PSP/GAME/PspWii/Imagenes/menuempty.png")
end
if seleccionado == "1" then
txt3 = io.open("ms0:/PSP/GAME/PspWii/seleccion.txt","r")
canal4 = txt3:read()
icono4 = Image.load("ms0:/PSP/GAME/PspWii/Imagenes/menu"..canal4..".png")
icono4sel = Image.load("ms0:/PSP/GAME/PspWii/Imagenes/menu"..canal4.."sel.png")
canal5 = txt3:read()
icono5 = Image.load("ms0:/PSP/GAME/PspWii/Imagenes/menu"..canal5..".png")
icono5sel = Image.load("ms0:/PSP/GAME/PspWii/Imagenes/menu"..canal5.."sel.png")
txt3 = nil 
collectgarbage()
end
function LeerPulsacion()
pad = Controls.read()
x = 0
x0 = 235
y0 = 150
x1 = 0
y1 = 0
if x0 >= 22 and x0 <= 150 and y0 >= 6 and y0 <= 80 and pad:cross() then
fondo = nil
cursor = nil
icono = nil
icono4 = nil
icono4sel = nil
icono5 = nil
icono5sel = nil
icono6 = nil
icono6sel = nil
collectgarbage()
System.memclean()
dofile("ms0:/PSP/GAME/PspWii/Canales/presencanalumd.lua")
end
if x0 >= 172 and x0 <= 300 and y0 >= 6 and y0 <= 80 and pad:cross() then
fondo = nil
cursor = nil
icono = nil
icono4 = nil
icono4sel = nil
icono5 = nil
icono5sel = nil
icono6 = nil
icono6sel = nil
collectgarbage()
System.memclean()
dofile("ms0:/PSP/GAME/PspWii/Canales/presencanalfotos.lua")
end
if seleccionado if x0 >= 322 and x0 <= 450 and y0 >= 6 and y0 <= 80 and pad:cross() then
fondo = nil
cursor = nil
icono = nil
icono4 = nil
icono4sel = nil
icono5 = nil
icono5sel = nil
icono6 = nil
icono6sel = nil
collectgarbage()
System.memclean()
dofile("ms0:/PSP/GAME/PspWii/Canales/presencanalpspii.lua")
end== "1" then
if x0 >= 25 and x0 <= 150 and y0 >= 105 and y0 <= 180 and pad:cross() then
fondo = nil
cursor = nil
icono = nil
icono4 = nil
icono4sel = nil
icono5 = nil
icono5sel = nil
icono6 = nil
icono6sel = nil
collectgarbage()
System.memclean()
dofile("ms0:/PSP/GAME/PspWii/Canales/presencanal"..canal4..".lua")
end
if x0 >= 172 and x0 <= 300 and y0 >= 105 and y0 <= 180 and pad:cross() then
fondo = nil
cursor = nil
icono = nil
icono4 = nil
icono4sel = nil
icono5 = nil
icono5sel = nil
icono6 = nil
icono6sel = nil
collectgarbage()
System.memclean()
dofile("ms0:/PSP/GAME/PspWii/Canales/presencanal"..canal5..".lua")
end
end
if pad:start() and seleccionado == "0" then
screen:clear()
screen:print(1, 1, "Pon los canales en tu orden preferido, si algun canal no lo", verde)
screen:print(1, 10, "quieres escribe empty, y el nombre del canal para ponerlo", verde)
screen.flip() 
screen.waitVblankStart(200)
txt4 = io.open("ms0:/PSP/GAME/PspWii/script.txt","w")
uno = 1
txt4:write(uno)
txt4:close()
txt2 = io.open("ms0:/PSP/GAME/PspWii/seleccion.txt","w")
canal4 = System.startOSK("empty","Introduce un nombre")
txt2:write(canal4)
txt2:write("\n")
canal5 = System.startOSK("empty","Introduce un nombre")
txt2:write(canal5)
txt2:write("\n")
canal6 = System.startOSK("empty","Introduce un nombre")
txt2:write(canal6)
txt2:write("\n")
txt2:close()
tobreak = 1
end
end
while true do
if x0<0 then 
x0=0 
end
if x0>478 then 
x0=478 
end
if y0<0 then 
y0=0 
end
if y0>270 then 
y0=270 
end
-- Menu --
screen:blit(1,0,fondo)
screen:blit(22,6,icono[1])
screen:blit(172,6,icono[2])
screen:blit(322,6,icono[3])
screen:blit(25,105,icono4)
screen:blit(172,105,icono5)
if seleccionado == "0" then
screen:print(50, 100, "PULSE START PARA ELEJIR CANALES COMPLEMENTARIOS", negro)
end

if x0 >= 22 and x0 <= 150 and y0 >= 6 and y0 <= 80 then
iconosel1 = Image.load("ms0:/PSP/GAME/PspWii/Imagenes/menuumdsel.png")
  screen:blit(22,6,iconosel1)
  iconosel1 = nil
  collectgarbage()
end
if x0 >= 172 and x0 <= 300 and y0 >= 6 and y0 <= 80 then
iconosel2 = Image.load("ms0:/PSP/GAME/PspWii/Imagenes/menufotossel.png")
  screen:blit(172,6,iconosel2)
  iconosel2 = nil
  collectgarbage()
end
if x0 >= 322 and x0 <= 450 and y0 >= 6 and y0 <= 80 then
iconosel3 = Image.load("ms0:/PSP/GAME/PspWii/Imagenes/menumiisel.png")
  screen:blit(322,6,iconosel3)
  iconosel3 = nil
  collectgarbage()
end
if x0 >= 25 and x0 <= 150 and y0 >= 105 and y0 <= 180 then
  screen:blit(25,105,icono4sel)
end
if x0 >= 172 and x0 <= 300 and y0 >= 105 and y0 <= 180 then
  screen:blit(172,105,icono5sel)
end
screen:blit(x1,y1,cursor)
screen.waitVblankStart()
screen.flip()
LeerPulsacion()
end
Post Reply