Here, try this for an example code.
{
double gravity=0.5;//change this to increase or decrease the gravity. Must be nonnegative. It's not changed by the function, so you could use a constant if you wanted.
if (collision(x,y+vspeed+gravity))
{
vspeed=0;
if (vspeed>=0)
while (! collision(x,y+gravity))
{y+=gravity;}
else
while (!collision(x,y-gravity))
{y-=gravity;}
}
else
vspeed+=gravity;
y+=vspeed;
}
You'll need to write the "collision" function yourself. It accepts two arguments (an x-coordinate and a y-coordinate, both of type double) and returns whether or not the object, when placed at those coordinates, intersects a wall or not.
To add user input, use this function in addition:
{
double hspeed=4;
double jumpspeed=6;
double gravity=0.5;//These three variables could also be constants. hspeed is the speed at which the player moves left or right when the arrow keys are held down, jumpspeed is the speed with which he jumps.
if (dbLeftKey() && ! collision(x-hspeed,y))
x-=hspeed;
if (dbRightKey() && ! collision(x+hspeed,y))
x+=hspeed;
if (dbUpKey() && ! collision(x,y-jumpspeed) && collision(x,y+gravity))
vspeed=-jumpspeed; //This bit is quite self-explanatory.
}
It uses the same collision function. Hope that helps!