Gw Temp

Menu

Tutorial - 'Rpg Movement' by yugi

An item about RPGMaker 2000 posted on

Blurb

How to make the basic RPG movement in Game Maker.

Body

Rpg movement

Ok. First make 4 sprites of a character walking in each direction. Let's just call these sprites...

AlexU

AlexD

AlexR

AlexL

As you can see I'm using Alex sprites and the letter after Alex is just the direction the sprite is facing.

Ok, now we've got the sprites create an object and call it Alex. Once you've done that add a step event (if your using V4.3 or later) then in that step event add a code from under the code tab.

Ok, first we need to see if the keys are actually being pressed to do that add this piece of code...

if (keyboard_check(vk_right))

You can use vk_up, vk_down ETC. as well but you can't do vk_a ETC. You have to use ord('A') and the letter has to be a capital. Now we we know how to check what key is being pressed we have to do something. First we want Alex to move thats the whole point of this so add this piece of code...

if (keyboard_check(vk_right)){

if (place_free(x+4,y) x += 4;

}

Ok. what that does is simple you know about the keyboard_check function already. { and } are what start and finish a program (or a function that is executed) so if you are pressing the right arrow key andything inbetween { and } will be executed. place_free(x,y) speaks for it's self, it basically checks if there isn't a solid object at were ever (in this case 4 pixels to the right of Alex). Finally x += 4 moves Alex 4 pixels to the right. Now do this for each direction (up, down, left and right). That means you'll have to change the direction that is moved (you'll have to use y instead of x to move Alex up and down) and you'll have to chage the key that is pressed.

Ok now your code should look like this...

{

if (keyboard_check(vk_right)){

if (place_free(x+4,y) x += 4;

}

if (keyboard_check(vk_left)){

if (place_free(x-4,y) x -= 4;

}

if (keyboard_check(vk_up)){

if (place_free(x,y-4) y -= 4;

}

if (keyboard_check(vk_down)){

if (place_free(x,y+4) y += 4;

}

}

Ok, if your code doesn't look like that then just copy and paste that. Now you can test it if you want but you'll see nothing because Alex doesn't have a sprite. So now we're going to add a sprite to Alex. This is simple just add this under place_free(x,y)...
instance_sprite(AlexR);

that will just change the sprite to what evers in the brackets repeat this with each direction. Now your code should look like this...

{

if (keyboard_check(vk_right)){

if (place_free(x+4,y) x += 4;

instance_sprite(AlexR);

}

if (keyboard_check(vk_left)){

if (place_free(x-4,y) x -= 4;

instance_sprite(AlexL);

}

if (keyboard_check(vk_up)){

if (place_free(x,y-4) y -= 4;

instance_sprite(AlexU);

}

if (keyboard_check(vk_down)){

if (place_free(x,y+4) y += 4;

instance_sprite(AlexD);

}

}

If your code doesn't look like this then copy and paste this code. Now if you test it you'll see Alex start of with no sprite until you press an arrow key then he'll probably have feet of flames because of his animaion speed. Plus if you stop pressing the arrow key Alex will stop walking but his sprite will just continue.

To fix the first problem (with the image animation speed) just add this to the create event of Alex.

image speed = 0.4;

That simple slows down the animation speed of an object (1 is the normal speed and 0.5 is half the speed). To fix the second problem it isn't as simple but it can be done. All we have to do is check when any of the arrow keys are been pressed and when none of them are been pressed.

So first we'll check if the arrow keys are not been pressed with this piece of code.

if !keyboard_check(vk_right) && !keyboard_check(vk_left) && !keyboard_check(vk_up) && !keyboard_check(vk_down) image_single = 0;

It's pretty long for one piece of code but it works. You'll notice the ! before every keyboard_check this just means not (you can replace it with not but I don't). Add that piece of code to the step event of Alex.

Now test the game again. This time you'll see Alex walk but he wont have feet of flames and when you're not pressing an arrow key he wont look like he's moving as well. Now if you press 2 arrow keys at once you'll notice that Alex will move diagonal you can have this if you want but I don't so to fix that we need to do this.

Where you have if (keyboard_check(vk_right)) Etc. we need to change them to this.

if (keyboard_check(vk_right) && !keyboard_check(vk_left) && !keyboard_check(vk_up) && !keyboard_check(vk_down))

again, another long piece of code. This basically says if the right arrow key is been pressed and the other arrow keys are not been pressed do something.

Now your code should look like this.

{

if keyboard_check(vk_right) && !keyboard_check(vk_left) && !keyboard_check(vk_up) && !keyboard_check(vk_down)

{

instance_sprite(AlexR);

if place_free(x+4,y) x += 4;

}

if !keyboard_check(vk_right) && keyboard_check(vk_left) && !keyboard_check(vk_up) && !keyboard_check(vk_down)

{

instance_sprite(AlexL)

if place_free(x-4,y) x -= 4;

}

if !keyboard_check(vk_right) && !keyboard_check(vk_left) && keyboard_check(vk_up) && !keyboard_check(vk_down)

{

instance_sprite(AlexU);

if place_free(x,y-4) y -= 4;

}

if !keyboard_check(vk_right) && !keyboard_check(vk_left) && !keyboard_check(vk_up) && keyboard_check(vk_down)

{

instance_sprite(AlexD);

if place_free(x,y+4) y += 4;

}

//This stops the animation from playing

if !keyboard_check(vk_right) && !keyboard_check(vk_left) && !keyboard_check(vk_up) && !keyboard_check(vk_down) image_single = 0

else image_single = -1


}

Now retest it and avoila or almost firstly Alex still wont start with a sprite and if you press 2 arrow keys Alex will stop moving but his sprite will still play to fix this add this piece of code to the step event of Alex.

if keyboard_check(vk_right) && keyboard_check(vk_up) image_single = 0;

if keyboard_check(vk_right) && keyboard_check(vk_left) image_single = 0;

if keyboard_check(vk_right) && keyboard_check(vk_down) image_single = 0;

if keyboard_check(vk_left) && keyboard_check(vk_up) image_single = 0;

if keyboard_check(vk_left) && keyboard_check(vk_down) image_single = 0;

if keyboard_check(vk_up) && keyboard_check(vk_down) image_single = 0;

Well now your code should look like this...

{

if keyboard_check(vk_right) && !keyboard_check(vk_left) && !keyboard_check(vk_up) && !keyboard_check(vk_down)

{

instance_sprite(AlexR);

if place_free(x+4,y) x += 4;

}

if !keyboard_check(vk_right) && keyboard_check(vk_left) && !keyboard_check(vk_up) && !keyboard_check(vk_down)

{

instance_sprite(AlexL)

if place_free(x-4,y) x -= 4;

}

if !keyboard_check(vk_right) && !keyboard_check(vk_left) && keyboard_check(vk_up) && !keyboard_check(vk_down)

{

instance_sprite(AlexU);

if place_free(x,y-4) y -= 4;

}

if !keyboard_check(vk_right) && !keyboard_check(vk_left) && !keyboard_check(vk_up) && keyboard_check(vk_down)

{

instance_sprite(AlexD);

if place_free(x,y+4) y += 4;

}

//This stops the animation from playing

if !keyboard_check(vk_right) && !keyboard_check(vk_left) && !keyboard_check(vk_up) && !keyboard_check(vk_down) image_single = 0

else image_single = -1


if keyboard_check(vk_right) && keyboard_check(vk_up) image_single = 0;

if keyboard_check(vk_right) && keyboard_check(vk_left) image_single = 0;

if keyboard_check(vk_right) && keyboard_check(vk_down) image_single = 0;

if keyboard_check(vk_left) && keyboard_check(vk_up) image_single = 0;

if keyboard_check(vk_left) && keyboard_check(vk_down) image_single = 0;

if keyboard_check(vk_up) && keyboard_check(vk_down) image_single = 0;

}

Now retest it and again Alex wont start with a sprite but everything else is in order is it not?

Ok now to make Alex start with a sprite :D Add this piece of code to the create event of Alex.

instance_sprite(AlexR);

There Alex will start with a sprite and everything should be in working order.

Now create another object called Wall and make it look like a box (colour doesn't matter) then tick the box that says "Solid" then add this to the room (to make some walls) then test the game and then try and walk through the walls. Alex won't be able to.

If there are any bugs or there is something you don't understand then contact me and I'll see what I can do.


Phew. Thats all folks.