Ok, Here is the sample program. I should probably clean it up a bit, but here it is for now.
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
//////////////////
while ( LoopGDK ( ) )
{
jump();
leftright();
makeblock();
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=100;
float y=0;
float ground=400;
float grav=.05;
float height=-400;
float move=3;
int start=0;
int end=0;
int nextx=0;
int nextsprite=3;
int count=0;
int blocks[200]={0};
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);
count=start;
while(count!=end){
if(count>199){
count=0;
}
if(dbSpriteCollision(1,blocks[count])){
if(y<dbSpriteY(blocks[count])){
y=dbSpriteY(blocks[count])-dbSpriteHeight(1)-1;
jumpok=true;
}else{
y=dbSpriteY(blocks[count])+dbSpriteHeight(blocks[count])+1;
}
vely=0;
}
count++;
}
dbSprite(1,x,y,1);
}
void leftright(void);
void leftright(void){
if(dbLeftKey()){
x-=move;
if(x<100){
nextx+=100-x;
count=start;
while(count!=end){
if(count>199){
count=0;
}
dbSprite(blocks[count],dbSpriteX(blocks[count])+100-x,dbSpriteY(blocks[count]),dbSpriteImage(blocks[count]));
count++;
}
x=100;
}
dbSprite(1,x,y,1);
}
if(dbRightKey()){
x+=move;
if(x>500){
nextx+=500-x;
count=start;
while(count!=end){
if(count>199){
count=0;
}
dbSprite(blocks[count],dbSpriteX(blocks[count])+500-x,dbSpriteY(blocks[count]),dbSpriteImage(blocks[count]));
count++;
}
x=500;
}
dbSprite(1,x,y,1);
}
///////////
count=start;
while(count!=end){
if(count>199){
count=0;
}
if(dbSpriteCollision(1,blocks[count])){
if(x>dbSpriteX(blocks[count])){
x=dbSpriteX(blocks[count])+dbSpriteWidth(blocks[count])+1;
}else{
x=dbSpriteX(blocks[count])-dbSpriteWidth(1)-1;
}
}
count++;
}
dbSprite(1,x,y,1);
}
void makeblock( void );
void makeblock(void){
if( dbReturnKey() && end<200){
//dbDeleteSprite(blocks[end]);
dbSprite(nextsprite,nextx,200,1);
blocks[end]=nextsprite;
end++;
nextsprite++;
nextx+=100;
}
dbText(0,0,dbStr(end));
}
I had a bug for a while where I thought that the blocks would stop being generated, but instead they were being placed off the screen.
The text in the corner of the screen tells you how many blocks you have made.
Oh, and it has a side scroller.
Edit:
Here is a small alteration of makeblock that causes the objects to be generated in random locations.
void makeblock( void );
void makeblock(void){
if( dbReturnKey() && end<200){
//dbDeleteSprite(blocks[end]);
dbRandomize ( dbTimer());
dbSprite(nextsprite,nextx,dbRND(200)+100,1);
blocks[end]=nextsprite;
end++;
nextsprite++;
nextx+=100;
}
dbText(0,0,dbStr(end));
}