How do I make a 2d side-scrolling platform game?

Started by
8 comments, last by s_cloudx 22 years, 2 months ago
I'm trying to make a 2d side-scrolling platform game (a la castlevania). How do they make the character jump? stand on the platform? I am having trouble implementing them. Can someone here, teach me how? or maybe point me to a tutorial. I would be very grateful to any advice. Thanks in advance! Edited by - s_cloudx on February 5, 2002 9:21:16 PM
--------------------------Sony "Mr. Valdez" ValdezPinoyforum.net Technical EditorPinoyforum.net - the breeding ground of the Filipino Computer Talents
Advertisement
HAHA
Ignore the many assholes who frequent this board.

If your main character has a "world y-coordinate" (preferrably at his feet) and every surface/floor has one of its own, then for a character to be standing on a surface he should be located at the given surface''s y-coordinate. Jumping is a question of temporarily incrementing and then decrementing the character''s y-coordinate.

A good approximation is to use a function like y = b - x2 over the interval [-sqrt(b), +sqrt(b)]. By that I mean choosing, say, to start the jump time at -3 and incrementing it until it gets to 3. In that case, the jump function would be jump_y = 9 - (jump_time * jump_time).
jump_time | jump_y    -3    |   0    -2    |   5    -1    |   8     0    |   9     1    |   8     2    |   5     3    |   0 

As you can see, adding jump_y to your character''s y-coordinate would give you a nice non-linear jump, realistically slowing down near the top and then speeding up again on the way down.

If this isn''t clear enough, post back and point out any areas that may be confusing.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
There''a a book that talks about sidescrollers and it comes with a small demo. It''s all free to download right here.
Check out the latest contest. It''s about making a side-scrolling game using the Game Development Kit.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

One problem, Oluseyi

one slight push of my button and my character jumps high. I want to emulate the way games like mario that has a variety of jumps height. How do I do that?
--------------------------Sony "Mr. Valdez" ValdezPinoyforum.net Technical EditorPinoyforum.net - the breeding ground of the Filipino Computer Talents
Vary the height on how long you hold the button down. Use a timer to figure out how many milliseconds the user keeps the jump button pressed, and then adjust his vertical based on that.
The way I did jumping was like this:
if Key[KEY_JUMP] is pressed then Player.Speed.y = -10; 

and each frame I do this:
Player.Speed.y += Gravity;Player.y += Player.Speed.y; 

That''s all there is to it.

~CGameProgrammer( );

~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
Actually, to do it the Mario way, this is probably a good way to do it... do this each frame:
if( Key[KEY_JUMP] is pressed ){    Player.Speed.y = -10;    Jumping = true;} if( Jumping ){    Player.JumpDist += 10;    if( Player.JumpDist >= 80 ) // max jump height        Jumping = false;}else // Falling{    Player.Speed.y += Gravity;    Player.y += Player.Speed.y;} 


~CGameProgrammer( );

~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
At last! I was able to get my character to jump. But I did it in a different way. You guys gave me the idea. Anyways, from memory, this is my code:

//Possible actions the player can do
enum PlayerAction = {WALKING, RUNNING, JUMPING, FALLING ...}

struct
{
//location of character
int x,y;

//jump related valuables
int Gravity;
int maxJumpHeight;

PlayerAction PA;

} Player;

//Default gravity. change this to make an anti-gravity room or
//heavy gravity room.
int Default_Gravity = 2;

void Init_Player()
{

//initialize the current gravity to the default
Player.Gravity = Default_Gravity;

//the maximum height in pixel the character can jump
Player.maxJumpHeight = -10;

}

void Controls()
{

//if player pushes Jump button
if (Key(JUMP_BUTTON))

//make sure we don't double jump when we reach our maximum jump
if (Player.PA != FALLING)
{

//set the player to jumping
Player.PA = JUMPING;

//If the Player is still starting his jump, decrease by 2
//else decrease by one. this will slow his jump.
if (Player.Gravity > 0)
Player.Gravity -= 2;
else
Player.Gravity --;

//skip the rest of the function to speed things up
return;
}

//if player releases Jump button while the player is jumping
if (!Key(JUMP_BUTTON) && Player.PA == JUMPING)
{

//he falls
Player.PA == FALLING;
}

//the player is falling
if (Player.PA == FALLING)
{

// if the player's gravity is negative, fall slowly
// otherwise fall fast

if (Player.Gravity < 0)
Player.Gravity ++;
else
Player.Gravity += 2;

//skip the rest of the function to speed things up
return;
}

//the player isn't jumping or falling
if (Player.PA != JUMPING || Player.PA != FALLING)

//reset the gravity
Player.Gravity = Default_Gravity;
}

void Apply_Gravity()
{

// if the gravity is positive, the player will fall.
// otherwise he will rise.
Player.x -= Player.Gravity;

// if the player has reached the highest point he can jump
if (Player.Gravity < Player.maxJumpHeight)
{
Player.PA = FALLING;
}

}

//this is called every frame
void UpdateFrame()
{

Controls();

ApplyGravity();

//this function will check the AI, logic, etc.
Logic();

//this function will draw the sprites, background, character, etc.
Render();

}

--------

what do you guys think?


P.s. Maraming salamat sa tulong nyo (Thank you very much for all your help!)


Edited: Forgoten UpdateFrame. Added UpdateFrame()

Edited by - s_cloudx on February 9, 2002 8:11:08 PM
--------------------------Sony "Mr. Valdez" ValdezPinoyforum.net Technical EditorPinoyforum.net - the breeding ground of the Filipino Computer Talents

This topic is closed to new replies.

Advertisement