Ahora que hemos añadido una escalera a nuestro juego, creado un sprite para nuestro personaje cuando sube y baja por la escalera, y configurado las teclas de Arriba y Abajo, necesitamos escribir el código que controlará sus movimientos de subida y bajada por las escaleras.
Para ello, abramos el evento Player (Player Event), que debería verse así:
EVENT PLAYER
IF KEY LEFT
LET DIRECTION = LEFT
LET IMAGE = 1
IF X <= LEFTEDGE
SCREENLEFT
LET X = RIGHTEDGE
EXIT
ELSE
IF CANGOLEFT
SPRITELEFT
ANIMATE
ENDIF
ENDIF
ENDIF
IF KEY RIGHT
LET DIRECTION = RIGHT
LET IMAGE = 0
IF X >= RIGHTEDGE
SCREENRIGHT
LET X = LEFTEDGE
EXIT
ELSE
IF CANGORIGHT
SPRITERIGHT
ANIMATE
ENDIF
ENDIF
ENDIF
IF KEY FIRE
JUMP 7
ENDIF
FALL
IF DEADLY
KILL
ENDIF
GETBLOCKSVeremos este script en detalle en la próxima Parte, pero por ahora fíjate en que el script está dividido en varias cláusulas:
Qué hacer si se está pulsando la tecla izquierda
Qué hacer si se está pulsando la tecla derecha
Qué hacer si se está pulsando la tecla de disparo
Hacer que el jugador CAIGA (FALL) si no hay ningún bloque debajo en el que apoyarse
Qué hacer si el jugador toca un bloque mortal
y nuestro comando GETBLOCKS que añadimos antes al crear nuestros huesos coleccionables
Así que aquí no hay nada que controle qué debe pasar si el jugador pulsa ARRIBA o Abajo para subir o bajar una escalera.
Vamos a añadir algo de código para ARRIBA: corta y pega este código justo antes de la cláusula IF KEY FIRE...
IF KEY UP
ANIMATE SLOW
IF LADDERABOVE
IF CANGOUP
LET IMAGE = 3
SPRITEUP
LET DIRECTION = UP
ELSE
ADD 1 TO Y
ENDIF
ELSE
SPRITELEFT
IF LADDERABOVE
ELSE
SPRITERIGHT
SPRITERIGHT
IF LADDERABOVE
ELSE
SPRITELEFT
ENDIF
ENDIF
ENDIF
ENDIF
El código para bajar por la escalera es bastante parecido...
IF KEY DOWN
ANIMATE SLOW
IF LADDERBELOW
LET IMAGE = 3
SPRITEDOWN
LET DIRECTION = DOWN
ELSE
SPRITELEFT
IF LADDERBELOW
ANIMATE
ELSE
SPRITERIGHT
SPRITERIGHT
IF LADDERBELOW
ELSE
SPRITELEFT
ENDIF
ENDIF
ENDIF
ENDIFDe nuevo, pega este código justo antes de la cláusula IF KEY FIRE. Guarda el script, y luego compila (Build) y prueba tu juego. El perro ya puede subir y bajar por las escaleras.
En MPAGD cada sprite tiene una serie de parámetros que podemos aprovechar en nuestro código. Son estos:
X: la coordenada en el eje x de la pantalla del píxel superior izquierdo del sprite
Y: la coordenada en el eje y de la pantalla del píxel superior izquierdo del sprite
IMAGE: el número de imagen del sprite
FRAME: el número de fotograma de la IMAGE del sprite
TYPE: el número de tipo de evento en el que está el código del sprite (el jugador es TYPE = 0)
Además, cada sprite tiene tres parámetros definibles por el usuario, que podemos usar para lo que queramos. Son:
DIRECTION: se suele usar para guardar la dirección en la que se desplaza el jugador, aunque puedes usarlo con otro fin si lo necesitas
SETTINGA
SETTINGB
Hay un par de parámetros más del sprite que guardan información sobre él cuando está en el aire (saltando o cayendo, por ejemplo) ...los veremos en otro momento.
Siempre es buena idea comentar nuestro código, poniendo junto a cada línea una explicación de lo que creemos que hace...cuando empiezas a experimentar y probar cosas nuevas, comentar se vuelve imprescindible. Recuerda que intentamos meter todo lo posible en unos 40K de memoria; si eres como yo, pronto empezarás a escribir tu propio código para hacer cosas que van más allá de las capacidades de los scripts de serie de MPAGD...si no comentas tu código, enseguida olvidarás qué *crees* que intenta hacer, ¡y depurarlo será aún más difícil!
Comentamos el código añadiendo un punto y coma y luego el comentario. El motor de MPAGD sabe que debe ignorar tus comentarios al compilar el código.
Ahora es probablemente un buen momento para comentar nuestro script del jugador; aquí tienes el script completo tal como está hasta ahora, con mis comentarios:
EVENT PLAYER
IF KEY LEFT ; Is the left key pressed?
LET DIRECTION = LEFT ; if so, remember I am going LEFT
LET IMAGE = 1 ; display the left facing image
IF X <= LEFTEDGE ; are we at the left edge of the screen?
SCREENLEFT ; if so, go to the screen to left on the map
LET X = RIGHTEDGE ; and put me at the right edge of that screen
EXIT ; and stop processing this event
ELSE ; if I'm not at the left edge
IF CANGOLEFT ; is there space to the left of me?
SPRITELEFT ; if so move me left
ANIMATE ; and animate me
ENDIF
ENDIF
ENDIF
IF KEY RIGHT ; Is the right key pressed?
LET DIRECTION = RIGHT ; if so, remember I am going RIGHT
LET IMAGE = 0 ; display the right facing image
IF X >= RIGHTEDGE ; Am I at the right edge of the screen?
SCREENRIGHT ; if so go to the screen to right on the map
LET X = LEFTEDGE ; and put me at the left edge
EXIT ; and stop processing this event
ELSE ; if I'm not at the right edge
IF CANGORIGHT ; is there space to the right of me?
SPRITERIGHT ; if so, move me right
ANIMATE ; and animate me?
ENDIF
ENDIF
ENDIF
IF KEY UP ; is the UP key pressed?
ANIMATE SLOW ; if so, animate me slowly
IF LADDERABOVE ; is there a ladder above me?
IF CANGOUP ; can I go up?
LET IMAGE = 3 ; display the image of me climbing
SPRITEUP ; move me up
LET DIRECTION = UP ; remember I am going up
ELSE ; if there is a ladder above me, but I cant go up
ADD 1 TO Y ; nudge me 1 pixel to the right
ENDIF
ELSE ; if there isn't a ladder above me
SPRITELEFT ; move me left
IF LADDERABOVE ; now is there a ladder above me?
ELSE ; if not
SPRITERIGHT ; move me back where I was
SPRITERIGHT ; then a bit more to the right
IF LADDERABOVE ; now is there a ladder above me?
ELSE ; if not
SPRITELEFT ; move me back where I was
ENDIF
ENDIF
ENDIF
ENDIF
IF KEY DOWN ; is the DOWN key pressed?
ANIMATE SLOW ; if so, animate me slowly
IF LADDERBELOW ; is there a ladder below me?
LET IMAGE = 3 ; if so, set my image to the climbing one
SPRITEDOWN ; move me down
LET DIRECTION = DOWN ; remember I am going down
ELSE ; if there isn't a ladder below me
SPRITELEFT ; move me left
IF LADDERBELOW ; is there a ladder below me now?
ELSE ; if not
SPRITERIGHT ; move me back where I was
SPRITERIGHT ; then a bit more to the right
IF LADDERBELOW ; now is there a ladder below me?
ELSE ; if not
SPRITELEFT ; move me back where I was
ENDIF
ENDIF
ENDIF
ENDIF
IF KEY FIRE ; is the Fire key being pressed?
JUMP 7 ; if so, make me Jump to height 7
ENDIF
FALL ; make me fall if there is nothing below me
IF DEADLY ; am I touching something deadly?
KILL ; if so, kill me
ENDIF
GETBLOCKS ; if I touch any collectible blocks, run the Collected Block scriptAsí que lo importante que hay que entender en este script es:
Las instrucciones LET DIRECTION = son solo una forma de recordar en qué dirección vamos por si necesitamos consultarlo en otra parte de nuestro código (lo haremos más adelante)
El código de la ESCALERA (LADDER) parece un poco enrevesado, con un montón de instrucciones IF adicionales: lo único que hacen es facilitar subir por una escalera; es decir, el jugador no necesita estar al 100% debajo de la escalera para subirla, el código lo desplaza un par de píxeles para ayudarle a colocarse en posición.
Con suerte, leyendo los comentarios, entenderás bien cómo se gestionan el movimiento básico y los controles en un script de MPAGD.
