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

Author Topic: Gravity and Jumping Mario  (Read 2310 times)

0 Members and 1 Guest are viewing this topic.

sptroyer

  • Newbie
  • *
  • Posts: 3
    • View Profile
Gravity and Jumping Mario
« on: May 21, 2014, 03:48:47 am »
How can I make my Mario jump and then fall back down using this code:

#include<SFML/Graphics.hpp>
#include<iostream>
 
int main()
{
    enum Direction { Right };
 
    sf::RenderWindow Window(sf::VideoMode(800, 600, 32), "SpriteSheet Animation");
 
    sf::Texture texture;
   sf::Texture tex;
   sf::Texture up;
    sf::Sprite sprite;
   sf::Sprite still;
   sf::Sprite jump;
 
    sf::Vector2i source(1, Right);
 
    if(!texture.loadFromFile("Data/Running.gif"))
        std::cout << "could not locate the specified file" << std::endl;
    else
        sprite.setTexture(texture);

   if(!tex.loadFromFile("Data/Standing.gif"))
        std::cout << "could not locate the specified file" << std::endl;
    else
        still.setTexture(tex);

   if(!up.loadFromFile("Data/Jumping.gif"))
        std::cout << "could not locate the specified file" << std::endl;
    else
        jump.setTexture(up);

   sprite.setPosition(0,400);
   jump.setPosition(0,-400);
   still.setPosition(0,400);

 
    while(Window.isOpen())
    {
        sf::Event Event;
        while(Window.pollEvent(Event))
        {
            if(Event.type == sf::Event::Closed || Event.key.code == sf::Keyboard::Escape)
                Window.close();
      }

            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
            sprite.move(.1,0);
             sprite.setTextureRect(sf::IntRect(source.x * 15, source.y * 20, 15, 20));
             still.setPosition(-50,-50);
             jump.setPosition(-50,-50);
         
         }

         if (Event.type == sf::Event::KeyReleased && Event.key.code == sf::Keyboard::Right) {
            still.setPosition(sprite.getPosition().x,sprite.getPosition().y);
             sprite.setTextureRect(sf::IntRect(source.x * 0, source.y * 0, 0, 0));
   
             jump.setPosition(-50,-50);
            
         }

         if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
            sprite.setTextureRect(sf::IntRect(source.x * 0, source.y * 0, 0, 0));
            still.setPosition(-50,-50);
            jump.setPosition(sprite.getPosition().x,sprite.getPosition().y);

            
         }

         if (sprite.getPosition().x < 0 )
      {
         sprite.setPosition(0, 400);
      }

         if (sprite.getPosition().x > 786)
      {
         sprite.setPosition(0, 400);
      }
 
        source.x++;
 
        if(source.x * 15 >= texture.getSize().x)
            source.x = 0;   
 
        Window.clear();

      Window.draw(still);

      if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
        Window.draw(sprite);
      }
      
      Window.draw(jump);
      
        Window.display();
    }
    return 0;
}

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Re: Gravity and Jumping Mario
« Reply #1 on: May 21, 2014, 07:35:36 am »
No need for creating the same post twice?
And use the code tag for your code, and maybe a spoiler too.
Everything event related should be inside the event loop, and real time input should be outside...
Use the time to move your sprite, otherwise it does run differently on different PC`s.

To the question: With "this code" you obviously can't make your Mario jump :p
How jumping works is adding gravity to a "fallVelocity" which affects Mario all the time. If he hits some sort of ground or a block above his head you set this value to 0. If you jump you just subtract a certain amount (experiment a bit with the values)
Failing to succeed does not mean failing to progress!

sptroyer

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Gravity and Jumping Mario
« Reply #2 on: May 21, 2014, 09:06:19 pm »
Thank you

 

anything