Programación Micro Lua DS
Estructura básica El código de un videojuego en Lua involucra la carga de contenido y la definición de variables. Ejemplo: sprite = Sprite.new("Heroine.png", 16, 18, VRAM) background = Image.load("BackgroundMC.png", VRAM) score = 0 lifes = 3
También en esta primera parte del código se recomienda agregar todas las funciones a utilizar en el videojuego. Por ejemplo: function printScore(score) printScore(score) screen.print(SCREEN_DOWN screen.print(SCREEN_DOWN, , 15, 20, "Score: " .. score) end
Al tener todas estas declaraciones necesarias para el buen funcionamiento del juego se procede a implementar el ciclo de vida del videojuego, el cual básicamente es un ciclo while hasta que ocurra un evento, por ejemplo presionar el botón Start : while not Keys.newPress.Start Keys.newPress.Start do Controls.read() -- Código del videojuego render() end
La función Controls.read() permite conocer el estado de los botones o la pantalla táctil, mientras que la función render() nos permite dibujar o pintar los elementos en pantalla. Dentro de este ciclo que define al juego se encuentran localizadas implícitamente las funciones de actualización (Update) y pintado (Draw ). ).
Hola mundo en Lua Lo único que se requiere hacer es escribir el siguiente código en cualquier editor de texto y
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
guardarlo con extensión .lua. while true while true do Controls.read() screen.print(SCREEN_UP, screen.print(SCREEN_UP, 10, 10, "Hello world.") render() end
En seguida hay que ejecutar Micro Lua Simulator1, elegir la opción File > Open y Open y elegir el script anterior con extensión .lua. El resultado es el siguiente:
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Ejemplos de código 2 El propósito de los siguientes ejemplos radica en observar las similitudes que guardan la estructura de un juego en Lua con la vista en el framework XNA.
Cambiar color del background color1 = Color.new(10, 10, 20) color2 = Color.new(31, 31, 31) -- color blanco while not Keys.newPress.Start Keys.newPress.Start do Controls.read() -- Pantalla superior screen.drawFillRect(SCRE screen.drawFillRect(SCREEN_UP, EN_UP, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, color1) -- Pantalla inferior screen.drawFillRect(SCRE screen.drawFillRect(SCREEN_DOWN, EN_DOWN, 0, 0, SCREEN_WIDTH, SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_HEIGHT, color2) screen.print(SCREEN_UP, screen.print(SCREEN_UP, 10, 10, "Hello world.") world.") render() end
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Dibujar imágenes color = Color.new(10, 10, 20) imagen = Image.load("Imagenes/Ayanami.png" Image.load("Imagenes/Ayanami.png", VRAM) while not Keys.newPress.Start Keys.newPress.Start do Controls.read() -- Background screen.drawFillRect(SCRE screen.drawFillRect(SCREEN_UP, EN_UP, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, color) -- Dibujar imagen screen.blit(SCREEN_UP, 30, 20, imagen)
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Aplicar efectos a imágenes Las siguientes líneas de código agregan los efectos nombrados: -- Escalar imagen Image.scale(imagen, Image.scale(imagen, 72, 246) -- nuevo ancho y largo de la imagen -- Rotar imagen Image.rotate(imagen, Image.rotate(imagen, 470) -- radianes (0 a 511) Image.rotateDegree(image Image.rotateDegree(imagen, n, 330) -- grados (0 a 360) -- Efecto espejo Image.mirrorH(imagen, Image.mirrorH(imagen, true) -- booleano para activado/desactivado Image.mirrorV(imagen, Image.mirrorV(imagen, true)
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Posicionar imágenes Sobre cómo alinear o colocar imágenes en la posición correcta de la pantalla. imagen = Image.load("Imagenes/Ayanami.png" Image.load("Imagenes/Ayanami.png", VRAM)
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
-- Dibujar imagen en esquina inferior derecha screen.blit(SCREEN_UP, screen.blit(SCREEN_UP, SCREEN_WIDTH-ancho, SCREEN_WIDTH-ancho, SCREEN_HEIGHT-largo, SCREEN_HEIGHT-largo, imagen) -- Dibujar imagen en centro de pantalla screen.blit(SCREEN_UP, screen.blit(SCREEN_UP, (SCREEN_WIDTH/2)-(ancho/ (SCREEN_WIDTH/2)-(ancho/2), 2), (SCREEN_HEIGHT/2)-(largo (SCREEN_HEIGHT/2)-(largo/2), /2), imagen) render() end
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
x = 0 y = 0 velocidad = 1 while not Keys.newPress.Start do Controls.read() -- Actualizar coordenada x x = x + velocidad -- Mantener dentro del espacio de pantalla if x if x >= SCREEN_WIDTH-ancho or or x x <= 0 then velocidad = velocidad * (-1) end -- Background screen.drawFillRect(SCRE screen.drawFillRect(SCREEN_UP, EN_UP, 0, 0, SCREEN_WIDTH, SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_HEIGHT, Color.new(10, Color.new(10, 10, 20)) -- Dibujar imagen screen.blit(SCREEN_UP, screen.blit(SCREEN_UP, x, y, imagen) render() end
Entrada del usuario Cómo interpretar cuando el usuario presiona los botones o la pantalla táctil para mover la imagen en el espacio de la pantalla. imagen = Image.load("Imagenes/Ayanami.png" Image.load("Imagenes/Ayanami.png", VRAM) x = 0
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
y = y + velocidad end if if Keys.held.Right then x = x + velocidad end if Keys.held.Left Keys.held.Left then x = x - velocidad end -- Usando el stylus para mover la imagen if Stylus.held then x = Stylus.X y = Stylus.Y end -- "DIBUJAR" -- Background screen.drawFillRect(SCRE screen.drawFillRect(SCREEN_DOWN, EN_DOWN, 0, 0, SCREEN_WIDTH, SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_HEIGHT, Color.new(10, Color.new(10, 10, 20)) -- Dibujar imagen screen.blit(SCREEN_DOWN, screen.blit(SCREEN_DOWN, x, y, imagen) render() end
Animación El algoritmo de animación se basa en tener un sprite sheet en el cual se dibuja un frame diferente en cada momento para crear la ilusión de movimiento. El sprite sheet dentro del código no es tratado como un tipo Image sino como un tipo Sprite.
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Controls.read() -- Background screen.drawFillRect(SCRE screen.drawFillRect(SCREEN_UP, EN_UP, 0, 0, SCREEN_WIDTH, SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_HEIGHT, Color.new(10, Color.new(10, 10, 20)) -- Mostrar animacion 1 sprite:playAnimation(SCREEN_UP, 10, 10, 1) -- Mostrar animacion 2 sprite:playAnimation(SCREEN_UP, 60, 10, 2) -- Mostrar animacion 3 sprite:playAnimation(SCR sprite:playAnimation(SCREEN_UP, EEN_UP, 110, 10, 3) -- Mostrar animacion 4 sprite:playAnimation(SCR sprite:playAnimation(SCREEN_UP, EEN_UP, 160, 10, 4) render() end
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Colisiones El manejo de colisiones en Micro Lua DS no está implementado explícitamente, así que se debe implementar dentro de una función. A continuación se muestra el pseudocódigo para colisiones basadas en cajas (bounding box): bool DoBoxesIntersect(Box a, Box b) { return (abs(a.x - b.x) * 2 < (a.width + b.width)) && (abs(a.y - b.y) * 2 < (a.height + b.height)); }
Supondremos que utilizaremos dos imágenes y queremos detectar cuándo hay colisión entre ellas. También diremos que una de las imágenes se encuentra estática mientras que la otra se puede controlar con los botones o stylus (como el código “Entrada del usuario” mostrado mostrado anteriormente) y en caso de haber colisión mostraremos un mensaje de texto. imagen = Image.load("Imagenes/Ayanami.png" Image.load("Imagenes/Ayanami.png", VRAM) imagen2 = Image.load("Imagenes/Kaworu.png" Image.load("Imagenes/Kaworu.png", VRAM) ancho = Image.width(imagen) Image.width(imagen) largo = Image.height(imagen) Image.height(imagen) x = 0 y = 0
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
10, 20)) -- Dibujar imagenes screen.blit(SCREEN_DOWN, screen.blit(SCREEN_DOWN, x, y, imagen) screen.blit(SCREEN_DOWN, screen.blit(SCREEN_DOWN, x2, y2, imagen2) if collisionDetection() collisionDetection() then screen.print(SCREEN_DOWN, 10, 10, "Colision detectada!") detectada!") end render() end