Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Is there an SFML Mario tutorial?  (Read 4077 times)

0 Members and 1 Guest are viewing this topic.

bladelock

  • Newbie
  • *
  • Posts: 20
    • View Profile
Is there an SFML Mario tutorial?
« on: September 29, 2011, 04:47:02 pm »
Hello, I was just wondering if there was an SFML Mario Clone tutorial with some source code.

I tried google for mario sfml game tutorials, but I havn't really found anything relevant.

I think that reading some Mario clone source code will help me understand some key aspects of the game, such as the jump sequence (Which, i'm still working on), and a nice and smooth running sequence.

I guess this is how I learned programming when I was an absolute beginner. I just need some code in action to give me some push.

Cheers.

Disch

  • Full Member
  • ***
  • Posts: 220
    • View Profile
Is there an SFML Mario tutorial?
« Reply #1 on: September 29, 2011, 05:01:53 pm »
Motion is very simple.   Just keep an X and Y velocity which add to the object's position every frame.

Code: [Select]

void Mario::Update()
{
  // assume velocity is a sf::Vector2f

  // jump
  if( PlayerPressedJumpButton() && MarioIsOnTheGround() )
  {
    velocity.y -= jump_speed;  // higher value = higher jump
  }

  // move left/right
  if(PlayerWantsToMoveLeft())
  {
    velocity.x -= move_speed;  // higher value = faster accelleration
      // but not higher top-speed.  Note you can have
      // different values here for when mario is on the ground vs. on ice
      // vs. in the air.  Example the accelleration might be low when in the
      //  air vs. high when on the ground

    if(velocity.x < -topspeed)  // higher value = higher top speed
      velocity.x = -topspeed;
  }
  if(PlayerWantsToMoveRight())
  {
    velocity.x += move_speed;
    if(velocity.x > topspeed)
      velocity.x = topspeed;
  }

  // gravity
  if(!MarioIsOnTheGround())
  {
    velocity.y += gravity;  // higher value = more gravity
  }

  // slow the player down if they are not moving
  if(!PlayerWantsToMoveLeft() && !PlayerWantsToMoveRight())
  {
    if(velocity.x < 0)
    {
      velocity.x += slowdown;  // higher value = quicker slowdown
         // again, air, ice, ground can have different values.  You could
         //  also just use move_speed for this I suppose
      if(velocity.x > 0)  // stop
        velocity.x = 0;
    }
    else
    {
      velocity.x -= slowdown;
      if(velocity.x < 0)
        velocity.x = 0;
    }
  }

  // terminal velocity
  if(velocity.y > terminalvelocity)
    velocity.y = terminalvelocity;  // the highest speed at which you can fall

  // move Mario
  MoveMario( velocity );

  DoCollisionDetectionAndStuff();

  // note you might want the collision detection to alter the velocity.
  //  for example set velocity.y to zero if you hit a floor/ceiling, or
  //  velocity.x to zero if you move into a wall.  Or multiply them by
  //  a negative constant to "bounce off" a rubber wall or something.
}