Let's create an MPAGD game - Part 14: Editing the Player Script
Now that we've added a ladder to our game, created a sprite for our character when he goes up and down the ladder, and set the keys for Up and Down, we need to write the code that will control him moving up and down the ladders.
To do that, let's open up the Player Event, it should look like this:
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 GETBLOCKS
We'll cover this script in detail in the next Part, but for now just note that the script is broken into a number of clauses:
What to do if the left key is being pressed
What to do if the right key is being pressed
What to do if the fire key is being pressed
Make the player FALL if there is no block beneath them to stand on
What to do if the player touches a deadly block
and our GETBLOCKS command that we added early when creating our collectible Bones
So there is nothing here that will control what should happen if the player presses UP or Down to ascend/descend a ladder.
Let's add some code for UP, cut and paste this code directly before the IF KEY FIRE clause...
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
The code for going down the ladder is fairly similar...
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 ENDIF
Again, paste this code directly before the IF KEY FIRE clause. Save the script, then Build and test your game. The dog can now go up and down the ladders.
In MPAGD each Sprite has a number of parameters, we can take advantage of these in our code. These are:
X - The coordinate on the x-axis of the screen of the top left pixel of the sprite
Y - The coordinate on the y-axis of the screen of the top left pixel of the sprite
IMAGE - The image number of the sprite
FRAME - The Frame number of the IMAGE of the sprite
TYPE - The event type number that the sprites code is in (The player is TYPE = 0)
In addition each Sprite has three, user-definable parameters, we can use these for whatever purpose we like, these are:
DIRECTION - usually used to store the direction that the player is travelling, although you can use it for a different purpose if you need to
There are a couple of other sprite parameters that hold information about the sprite if it is in the air (jumping or falling for example) ...we'll look at those another time.
It's always a good idea to comment our code - by putting an explanation next to each line, explaining what we think its doing...when you start to experiment and try new things, commenting becomes invaluable. Remember, we are trying to squeeze as much as possible into just 40K or so of memory, if you are anything like me you will soon start trying to write your own code to do things beyond the capabilities of MPAGDs stock scripts...if you don't comment your code you'll soon forget what you *think* it is trying to do, making debugging even more difficult!.
We comment our code by adding a semicolon and then adding a comment. The MPAGD engine knows to ignore your comments when compiling your code.
Now is probably a good time to comment our player script, here's the whole script as it exists so far, with my comments:
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 script
So the important things to understand in this script are:
The LET DIRECTION = statements are just a way of remembering which direction we are going in case we need to refer back to this elsewhere in our code (we will do later)
The LADDER code looks a bit convoluted, with a bunch of additional IF statements - all these are doing are making it easier to go up a ladder - in other words the player doesn't need to be 100% below the ladder to go up it, the code shifts him a couple of pixels to help get him into position.
Hopefully, by reading the comments, you'll get a good understanding of how basic movement and controls are handled in MPAGD script.