How to make your game character to jump

I made a game engine 6 months ago. Now i want to upgrade it and make it to a full game, but i dont know how to make my character jump.
Here is the code:
Main.cpp
#include "Engine.h"

int main()
{
Engine engine;

engine.start();

return 0;
}




Bob.cpp
#include "bob.h"

Bob::Bob()
{

m_Speed = 400;


m_Texture.loadFromFile("bob.png");
m_Sprite.setTexture(m_Texture);


m_Position.x = 500;
m_Position.y = 800;

}


Sprite Bob::getSprite()
{
return m_Sprite;
}

void Bob::moveLeft()
{
m_LeftPressed = true;
}

void Bob::moveRight()
{
m_RightPressed = true;
}

void Bob::stopLeft()
{
m_LeftPressed = false;
}

void Bob::stopRight()
{
m_RightPressed = false;
}


void Bob::update(float elapsedTime)
{
if (m_RightPressed)
{
m_Position.x += m_Speed * elapsedTime;
}

if (m_LeftPressed)
{
m_Position.x -= m_Speed * elapsedTime;
}

m_Sprite.setPosition(m_Position);

}





Bob.h
#pragma once
#include <SFML/Graphics.hpp>

using namespace sf;

class Bob
{

private:


Vector2f m_Position;


Sprite m_Sprite;


Texture m_Texture;


bool m_LeftPressed;
bool m_RightPressed;


float m_Speed;


public:


Bob();


Sprite getSprite();


void moveLeft();

void moveRight();


void stopLeft();

void stopRight();


void update(float elapsedTime);

};



Engine.h
#pragma once
#include <SFML/Graphics.hpp>
#include "Bob.h";

using namespace sf;

class Engine
{
private:


RenderWindow m_Window;


Sprite m_BackgroundSprite;
Texture m_BackgroundTexture;


Bob m_Bob;


void input();
void update(float dtAsSeconds);
void draw();

public:

Engine();


void start();

};




Engine.cpp
#include "Engine.h"

Engine::Engine()
{

Vector2f resolution;
resolution.x = VideoMode::getDesktopMode().width;
resolution.y = VideoMode::getDesktopMode().height;

m_Window.create(VideoMode(resolution.x, resolution.y),
"Simple Game Engine",
Style::Fullscreen);


m_BackgroundTexture.loadFromFile("background.jpg");


m_BackgroundSprite.setTexture(m_BackgroundTexture);

}

void Engine::start()
{

Clock clock;

while (m_Window.isOpen())
{

Time dt = clock.restart();


float dtAsSeconds = dt.asSeconds();

input();
update(dtAsSeconds);
draw();
}
}



Input.cpp
#include "Engine.h"

void Engine::input()
{

if (Keyboard::isKeyPressed(Keyboard::Escape))
{
m_Window.close();
}


if (Keyboard::isKeyPressed(Keyboard::A))
{
m_Bob.moveLeft();
}
else
{
m_Bob.stopLeft();
}

if (Keyboard::isKeyPressed(Keyboard::D))
{
m_Bob.moveRight();
}
else
{
m_Bob.stopRight();
}

}



Update.cpp

#include "Engine.h"

using namespace sf;

void Engine::update(float dtAsSeconds)
{
m_Bob.update(dtAsSeconds);
}




Draw.cpp
#include "Engine.h"

void Engine::draw()
{
m_Window.clear(Color::White);


m_Window.draw(m_BackgroundSprite);
m_Window.draw(m_Bob.getSprite());


m_Window.display();
}
It's going to be similar to your move functions, but you'll want to use a clock to emulate gravity.

https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Clock.php

Assuming you're familiar with the game loop...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void jump() {
  // Move your sprite by some pixel amount
  // This function is probably called somewhere in the getInput function
}

void runGame() {
  getInput(); // get input from player
  update();   // update the game physics
  draw();     // draw to the screen
}

int main() {
  bool gameLoop = true;
  while(gameLoop) {
    runGame(); // run the game
  }
  return 0;
}

I've provided messy code, but hopefully it should help (assuming you've also a good foundation in C++).
Last edited on
Thanks, but when i put it it doesn't work. Can you please help me and tell me where to put it?
Thanks!
Last edited on
Topic archived. No new replies allowed.