Sign in to follow this  
GROUDON

DarkGDK- im trying to get mario to jump!

Recommended Posts

ok, so im making a Mario Bros. remake. so far i got the background image and the sprites. so far im only using mario, floor tiles that are 20x20pixels, and the background. i stretch the image itself to 640x480, so now it fits the window. and mario can now move left and right. but now it seems i cant get him to jump correctly and interact with the floor tiling correctly. ive attempted to do so, but its now just a jumbled mess. i kinda need some help here unjumbling it and making it work. Im asking for someone to give me coding help, not nessesarily doing it for me tho. Here's the code: (with the problem area commented out)
#include <DarkGDK.h>

void ResInit(void);
void FloorInit(void);
void MoveMarioLeft(void);
void MoveMarioRight(void);
#define MOVEX 3

void DarkGDK(void)
{
	float UpVelocity=10.5f;
	bool HeldUp=0;
	dbSyncOn();
	dbSyncRate(60);
	ResInit();
	dbSetWindowTitle("Mario Bros");
	dbSetSpritePriority(1,2);
	while(LoopGDK())
	{
		int collision;
		if(dbLeftKey())
		{
			MoveMarioLeft();
		}
		if(dbRightKey())
		{
			MoveMarioRight();
		}
		/*if(dbUpKey())
		{
			if(dbSpriteCollision(1,0)>=100&&dbSpriteCollision(1,0)<233)
			{
				collision=dbSpriteCollision(1,0);
				if(dbSpriteY(1)+dbSpriteHeight(1)>=dbSpriteY(collision)&&dbSpriteY(1)+dbSpriteHeight(1)-5<=dbSpriteY(collision))
				{
					if(!HeldUp)
					{
						UpVelocity=10.5f;
						dbSprite(1,dbSpriteX(1),dbSpriteY(1)-(int)(UpVelocity),1);
						UpVelocity-=0.1f;
					}
					HeldUp=1;
				}
				if(dbSpriteY(1)>dbSpriteY(collision))
				{
					UpVelocity=0.0f;
					dbSprite(1,dbSpriteX(1),dbSpriteY(1)+5,1);
				}
			}
		}
		else
		{
			if(dbSpriteCollision(1,0)>=100&&dbSpriteCollision(1,0)<233)
			{
				collision=dbSpriteCollision(1,0);
				if(dbSpriteY(1)+dbSpriteHeight(1)>=dbSpriteY(collision)&&dbSpriteY(1)+dbSpriteHeight(1)-5<=dbSpriteY(collision))
				{
					HeldUp=0;
				}
			}
		}
		if(UpVelocity>0)
		{
			dbSprite(1,dbSpriteX(1),dbSpriteY(1)-(int)(UpVelocity),1);
			UpVelocity-=0.1f;
		}*/
		if(!(dbSpriteCollision(1,0)>=100&&dbSpriteCollision(1,0)<233))
		{
			dbSprite(1,dbSpriteX(1),dbSpriteY(1)+5,1);
		}
		dbSync();
	}
	return;
}

void ResInit(void)
{
	dbCreateAnimatedSprite(1,"mario.bmp",4,1,1);//mario=1; 1
	//dbCreateAnimatedSprite(2,"crab2.bmp",2,1,2);//crab=2; 2-12// dont need yet
	//dbCreateAnimatedSprite(13,"spiny.bmp",2,1,3);//spiny=3; 13-23
	//dbCreateAnimatedSprite(24,"buzzbuzz.bmp",2,1,4);//buzz=4; 24-34
        dbCreateAnimatedSprite(35,"floor.bmp",2,1,5);//floor=5; 35 
	dbLoadImage("map.bmp",6);
	FloorInit();
	dbSprite(36,0,0,6);//map size 640x480 map=6; 36
	dbSprite(1,180,380,1);
}
void FloorInit(void)
{
	dbLoadImage("floortile.bmp",7);
	int posx,posy,i;
	posx=-40;
	posy=120;
	for(i=100;i<116;i++)
	{
		dbSprite(i,posx,posy,7);
		posx+=20;
	}
	posx=360;
	for(i=116;i<132;i++)
	{
		dbSprite(i,posx,posy,7);
		posx+=20;
	}
	posx=-50;
	posy=253;
	for(i=132;i<139;i++)
	{
		dbSprite(i,posx,posy,7);
		posx+=20;
	}
	posx=550;
	for(i=139;i<145;i++)
	{
		dbSprite(i,posx,posy,7);
		posx+=20;
	}
	posx=167;
	posy=233;
	for(i=145;i<160;i++)
	{
		dbSprite(i,posx,posy,7);
		posx+=20;
	}
	posx=-40;
	posy=347;
	for(i=160;i<174;i++)
	{
		dbSprite(i,posx,posy,7);
		posx+=20;
	}
	posx=395;
	for(i=174;i<188;i++)
	{
		dbSprite(i,posx,posy,7);
		posx+=20;
	}
	posx=-40;
	posy=460;
	for(i=188;i<233;i++)
	{
		dbSprite(i,posx,posy,7);
		posx+=20;
	}
	for(i=100;i<233;i++)
	{
		dbSetSpritePriority(i,1);
	}
}

void MoveMarioLeft(void)
{
	int collide;
	dbSprite(1,dbSpriteX(1)-MOVEX,dbSpriteY(1),1);
	if(dbSpriteMirrored(1))
	{
		dbMirrorSprite(1);
	}
	if(dbSpriteCollision(1,0)>=100&&dbSpriteCollision(1,0)<233)
	{
		collide=dbSpriteCollision(1,0);
		if(dbSpriteX(1)-dbSpriteWidth(1)>dbSpriteX(collide))
		{
			dbSprite(1,dbSpriteX(1)+MOVEX,dbSpriteY(1),1);
		}
	}
	if(dbSpriteX(1)<=-40)
	{
		dbSprite(1,640,dbSpriteY(1),1);
	}
}

void MoveMarioRight(void)
{
	int collide;
	dbSprite(1,dbSpriteX(1)+MOVEX,dbSpriteY(1),1);
	if(!dbSpriteMirrored(1))
	{
		dbMirrorSprite(1);
	}
	if(dbSpriteCollision(1,0)>=100&&dbSpriteCollision(1,0)<233)
	{
		collide=dbSpriteCollision(1,0);
		if(dbSpriteX(1)<dbSpriteX(collide))
		{
			dbSprite(1,dbSpriteX(1)-MOVEX,dbSpriteY(1),1);
		}
	}
	if(dbSpriteX(1)>=640)
	{
		dbSprite(1,-38,dbSpriteY(1),1);
	}
}
dont get me wrong the program runs. the game is what doesnt run right. if u want to have the resources on hand to compile it urself. just make an image 20x20 as the floor tiles, a 640x480 to act as the background, and a 152x57 to act as mario. be sure to color the boxes u make different colors. Checklist of what i need: 1) Mario needs to jump up so you can see him go up, not just fall. 2)he needs to fall unless there is floor beneath him 3)he cant get stuck in between the floor by hitting the left or right side of the block. 4)when he jumps, he cant jump through the floors. 5)when he hits his head on the bottom of a floor tile, he needs to stop going up, but stay at the same y coordinate for an amount of time where if he hits his head he would hit the ground at the same time as if he didnt hit his head. 6)when the game starts, he cant be jumping up, he must fall to the ground. Thanx for any coding hints and help!

Share this post


Link to post
Share on other sites
implement a physics part wherever you call his "update" function.. i.e.



/* Falling physics */
if(mario is in air)
{
myAcceleration -= 9.8 * (currentTime - lastUpdateTime) / 1000;
/* the above means it will reduce it by 9.8 a second */

myYspeed += myAcceleration;
mariosY += myYspeed;
}





/* in the key press for jump*/

if(keypress == JUMP_KEY)
myYspeed += 110;


Im tired so hopefully that makes sense. the numbers used are just pulled out of my butt and you'd have to find a ratio of gravity to jump speed.

also, for the stopping at the top, well thats just collision detection where you dont actually increase is y, but the acceleration keeps ticking and it will keep trying to move mario's Y higher and higher till he finally starts to free fall.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this