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

Author Topic: Mario Jumping  (Read 1916 times)

0 Members and 1 Guest are viewing this topic.

sptroyer

  • Newbie
  • *
  • Posts: 3
    • View Profile
Mario Jumping
« on: May 23, 2014, 02:38:55 am »
Can someone make the WASD Mario jump smoothly? Please.

#include<SFML/Graphics.hpp>
#include<iostream>
 
int main()
{

   int windowHeight = 400;
   int windowWidth = 512;
   const float gravity = .1;
   int groundHeight = windowHeight - 64;
   sf::Vector2f velocity(sf::Vector2f(0, 0));



    enum Direction { Right };
 
    sf::RenderWindow Window(sf::VideoMode(windowWidth, windowHeight, 32), "SpriteSheet Animation");
 
    sf::Texture texture;
   sf::Texture tex;
   sf::Texture up;
    sf::Sprite sprite;
   sf::Sprite still;
   sf::Sprite jump;
   sf::Texture blue_back;
   sf::Texture sky_img;
   sf::Texture gro;
   sf::Texture test;
   sf::Sprite test2;


   sf::Texture texture2;
   sf::Texture tex2;
   sf::Texture up2;
    sf::Sprite sprite2;
   sf::Sprite still2;
   sf::Sprite jump2;


   if(blue_back.loadFromFile("Data/blue.jpg") == false) {
      return -1;
   }

   if(sky_img.loadFromFile("Data/sky.gif") == false) {
      return -1;
   }

   if(gro.loadFromFile("Data/ground.png") == false) {
      return -1;
   }




   sf::RectangleShape blue;
   blue.setSize(sf::Vector2f(windowWidth,windowHeight));
   blue.setPosition(0,0);
   blue.setTexture(&blue_back);
 
   sf::RectangleShape back;
   back.setSize(sf::Vector2f(windowWidth,windowHeight));
   back.setPosition(0,0);
   back.setTexture(&sky_img);

   sf::RectangleShape ground;
   ground.setSize(sf::Vector2f((windowWidth + 20),64));
   ground.setPosition(0,(windowHeight-44));
   ground.setTexture(&gro);

   sf::RectangleShape rect;
   rect.setSize(sf::Vector2f(20,20));
   rect.setPosition(0,0);
   rect.setFillColor(sf::Color::Black);


    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);

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




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

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

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

   






   sprite.setPosition(0,(windowHeight - 64));
   jump.setPosition(0,-(windowHeight - 64));
   still.setPosition(0,(windowHeight - 64));
   test2.setPosition(0, (windowHeight - 84));


   sprite2.setPosition(0,(windowHeight - 84));
   jump2.setPosition(0,-(windowHeight - 84));
   still2.setPosition(0,(windowHeight - 84));
   


   float moveSpeed = .4f, jumpSpeed = .7f;

    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.setTextureRect(sf::IntRect(source.x * 15, source.y * 20, 15, 20));
             still.setPosition(-50,-50);
             jump.setPosition(-50,-50);
         
         } else 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));
   
      
            
         } 


         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, (windowHeight - 64));
      }

         if (sprite.getPosition().x > windowWidth)
      {
         sprite.setPosition(0, (windowHeight - 64));
      }


          if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
             velocity.x = moveSpeed;
             sprite2.setTextureRect(sf::IntRect(source.x * 15, source.y * 20, 15, 20));
             still2.setPosition(-50,-50);
             jump2.setPosition(-50,-50);
          }
       
        else {
            velocity.x = 0;
      }

       if(Event.type == sf::Event::KeyReleased && Event.key.code == sf::Keyboard::D) {
           
          still2.setPosition(sprite2.getPosition().x,sprite2.getPosition().y);
             sprite2.setTextureRect(sf::IntRect(source.x * 0, source.y * 0, 0, 0));
             jump2.setPosition(-50,-50);
      }

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
      //   sprite2.setTextureRect(sf::IntRect(source.x * 0, source.y * 0, 0, 0));
         //   still2.setPosition(-50,-50);
            //jump2.setPosition(sprite2.getPosition().x,sprite2.getPosition().y);
             velocity.y = -jumpSpeed;
 
        if(sprite2.getPosition().y + texture2.getSize().y < groundHeight || velocity.y < 0 )
        {
            velocity.y += gravity;
        }
        else
        {
          //  sprite2.setPosition(sprite2.getPosition().x, groundHeight - texture2.getSize().y);
            velocity.y = 0;
        }

      if(sprite2.getPosition().y <= (windowHeight - 180) && sprite2.getPosition().y + texture2.getSize().y < groundHeight )
      {
         int test = 100;
         while (test > 0) {
         velocity.y += gravity;
         test--;
         }
      }

      
        sprite2.move(velocity.x, velocity.y);
      
      if (sprite2.getPosition().x < 0 )
      {
         sprite2.setPosition(0, (windowHeight - 84));
      }

         if (sprite2.getPosition().x > windowWidth)
      {
         sprite2.setPosition(0, (windowHeight - 84));
      }


         
        source.x++;
 
        if(source.x * 15 >= texture.getSize().x)
            source.x = 0;   
 
        Window.clear();

      Window.draw(blue);
      Window.draw(back);
      Window.draw(ground);
      Window.draw(still);

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

      Window.draw(still2);
      if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
        Window.draw(sprite2);
      }
      Window.draw(jump2);
      
        Window.display();
    }
    return 0;
}

Gobbles

  • Full Member
  • ***
  • Posts: 132
    • View Profile
    • Email
Re: Mario Jumping
« Reply #1 on: May 23, 2014, 03:11:39 am »
I'm not gonna lie, this is one big ball of a mess. Maybe you should start by cleaning it up a bit and better identifying your issue?

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Mario Jumping
« Reply #2 on: May 23, 2014, 03:35:44 am »
Probably one of the shittiest code written here, sorry.  :-\

Surround your code with [code=cpp][/code] when posting C++ code on the forum. (But you already know that)
Use events only inside the event loop. (But you already know that)
Don't uselessly load the same texture twice, you can use the same texture on different sprites.
You apply velocity.y (which is what makes your sprites jump or fall) to sprite2, but sprite2 is only drawn when the D key is pressed, so unless you're pressing D while jumping you won't even see it jump.

 

anything