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:
)) {
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:
) {
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:
)) {
Window.draw(sprite2);
}
Window.draw(jump2);
Window.display();
}
return 0;
}