OK! I got it! I think it is even better now than it was.
I integrated it into the code I had originally.
Now you may want to pay attention to the comments in the main.cpp.
main.cpp:
#include "methjump.h"
void DarkGDK ( void )
{
dbSyncOn ( );
dbSyncRate ( 60 );
dbLoadImage("sprite.bmp",1);
dbSprite(1,0,0,1);//collision
dbSprite(2,0,0,1);//visual
dbHideSprite(1);
dbSizeSprite(1,dbSpriteWidth(2)-2,dbSpriteHeight(2)-2);//give a one pixel margin
dbSprite(3,0,200,1);//////////////////
while ( LoopGDK ( ) )
{
jump();
leftright();
dbSprite(2,x-1,y-1,1);//sync the images
if ( dbEscapeKey ( ) )
break;
dbSync ( );
}
return;
}
methjump.h:
#include "DarkGDK.h"
float vely=0;
float x=0;
float y=0;
float ground=400;
float grav=.05;
float height=-400;
float move=1;
bool jumpok=true;
void jump(void);
void jump(void){
vely+=grav;
if(jumpok==true && dbSpaceKey()){
vely=-dbSqrt(-2.*grav*(height));
}
y+=vely;
if(y>=ground){
vely=0;
jumpok=true;
y=ground;
}else{
jumpok=false;
}
dbSprite(1,x,y,1);
if(dbSpriteCollision(1,3)){
if(vely>0){
y=dbSpriteY(3)-dbSpriteHeight(1)-1;
jumpok=true;
}else{
y=dbSpriteY(3)+dbSpriteHeight(3)+1;
}
vely=0;
}
dbSprite(1,x,y,1);
}
void leftright(void);
void leftright(void){
if(dbLeftKey()){
x-=move;
dbSprite(1,x,y,1);
}
if(dbRightKey()){
x+=move;
dbSprite(1,x,y,1);
}
///////////
if(dbSpriteCollision(1,3)){
if(x>dbSpriteX(3)){
x=dbSpriteX(3)+dbSpriteWidth(3)+1;
}else{
x=dbSpriteX(3)-dbSpriteWidth(1)-1;
}
}
dbSprite(1,x,y,1);
}
This is a great Collision set up for any 2D game, but you may what to loop through a bunch of objects, some of which could be baddies. So, I would suggest using arrays with 2 or more dimensions to keep track of all the world objects and baddies along with their attributes.
Also, If you want the screen to scroll to follow the character you will need to move all other objects to in the opposite direction of travel while keeping the character still. Arrays will prove to be helpful here as well.
I hope you find this helpful!
I'll get a new signature someday.