//Keyboard constants
//We use these so we can just type the variables
//instead of "keyboard_check(*)"
KEY_RIGHT=keyboard_check(vk_right);
KEY_LEFT=keyboard_check(vk_left);
KEY_UP=keyboard_check(vk_up);
KEY_DOWN=keyboard_check(vk_down)
KEY_MEW=keyboard_check(ord('Z'))
//This one's for variable jump height:
//Now check if the player pressed left or right and move
if (KEY_RIGHT)
{
hsp=walksp; //Walk right
image_xscale=1; //Face right
}
if (KEY_LEFT)
{
hsp=-walksp; //Walk left
image_xscale=-1; //Face left
}
//Face the proper way when falling
if (KEY_LEFT and ground=0)
{
hsp=-walksp; //jump left
image_xscale=-1; //face left
}
if (KEY_RIGHT and ground=0)
{
hsp=walksp; //jump right
image_xscale=1; //face right
}
//Climb up a sprout
//First, we need to verify that we're in "ladder mode"
if (KEY_UP || KEY_DOWN) //if you press up or down
{
if place_meeting (x, y, obj_sprout) ladder=1 //and if the player object is touching
//the ladder object, then ladder mode is activated
}
//Now, we need to write out the physics for "ladder mode"
if (ladder) //if ladder mode is activated
{
vsp=0 //this should negate the effects of gravity
//you could also type here "hsp=0" to prevent you from moving your avatar left/right
if (KEY_UP) vsp=-climbsp //up makes you go up
if (KEY_DOWN) vsp=climbsp //down makes you go down
if !place_meeting (x, y, obj_sprout) ladder=0 //if you move off of the ladder
//then "ladder mode" is turned off
}
if (ladder)
{
sprite_index = spr_cucumber_climb
}
//Stop moving when no keys are pressed
if (!KEY_RIGHT and !KEY_LEFT) hsp=0;
//Keep player facing last direction moved when no keys are pressed
if (!KEY_RIGHT and !KEY_LEFT)
{
draw_sprite(spr_cucumber_idle, 0, x, y); //change sprite_idle with whatever direction you are going.
}
//Make sure we don't hit a wall
if (place_meeting(x+hsp,y,obj_ground))
{
//Move until contact with the wall
if (hsp!=0)
while (!place_meeting(x+sign(hsp),y,obj_ground))
x+=sign(hsp);
hsp=0;
}
//Because we don't use hspeed, we got to move ourselves
x+=hsp;
//Now for vertical motion (jumping and falling)
//Is the player in the air?
if (place_meeting(x,y+1,obj_ground)) ground=1;
else ground=0;
//Fall with gravity
if (!ground and !ladder) vsp+=grav;
//Now it's more complicated.
//When hitting the ceiling, vertical speed must stop.
//The if statement says, "if we hit the ceiling and are moving up"
if (place_meeting(x,y+vsp,obj_ground) && vsp<0)
{
//We must move up until contact with the ceiling
while (!place_meeting(x,y+sign(vsp),obj_ground)) y+=sign(vsp);
vsp=0;
}
//But if we are moving down and hit the floor, we have to land
if (place_meeting(x,y+vsp,obj_ground) and vsp>0)
{
//Move so we hit the ground
var cc;
cc=vsp+1; //A counter, so we don't get an infinite loop
//Move down until we hit the floor
while (!place_meeting(x,y+1,obj_ground) and cc>=0) y+=1;
//Now ground the player
grounded=1;
vsp=0;
}
//Again, we're not using vspeed, so we have to move ourselves
y+=vsp;
//Animation - check what our sprite's state is and set accordingly
if (ground) //If we're grounded
{
if (hsp==0) //Then if we're not moving, change to stand sprite
{
if(KEY_MEW)
{
sprite_index=spr_cucumberMew;
image_speed=0.5;
}
else
sprite_index=spr_cucumber_idle;
image_speed=0.5; //animated at half the rate
}
else //But if we are moving, change to walk sprite
{
//Reset image index if just now switching to walk sprite
if (sprite_index!=spr_cucumberWalk) image_index=0;
sprite_index=spr_cucumberWalk;
image_speed=0.5; //Change this to whatever works for you
}
}
//If we're not grounded, but on a ladder, change to ladder sprite
if (ladder) sprite_index=spr_cucumber_climb
//If we're not grounded, and in the air, change to air sprite
if (!ground && !ladder)
{
sprite_index=spr_cucumberLeap;
image_speed=0; //Don't animate
}
if KEY_MEW
var n;
with obj_trunk
{
if grow == -1 //Make sure the vine isn't growing or fully grown
n = x - other.x;
if sign(n) == other.image_xscale //check if the player is facing the sprout
if x < 8 //make sure the sprout is close enough, you can change the 8 to adjust the distance
{
//The following conditional should be set by yourself. I just wrote it out as code for the sake of completeness.
if image_speed != 0 then image_speed *= 2; //If you set an image_speed for its normal sprite, just speed up the sprite
else
image_speed = 1/8; //If you didn't set an image_speed, set one now.
alarm[0] = image_number / image_speed; //The alarm won't count down until you've set it, so here it's set.
break; //Tell the game to stop looking for sprouts.
}
}