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

Author Topic: How to make my game character jump C++  (Read 5111 times)

0 Members and 1 Guest are viewing this topic.

MartyM

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
How to make my game character jump C++
« on: January 06, 2021, 10:44:33 am »
Can someone help me to make my character jump? Im not so good at the game loop so i need someone to tell me where to put the code. I want my character to jump like Super Mario(Jump and move in midair and fall)
(Its a project, so it has different files)  Here's the code:

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();
}
« Last Edit: January 06, 2021, 01:43:47 pm by eXpl0it3r »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: How to make my game character jump C++
« Reply #1 on: January 06, 2021, 10:51:56 am »
Most people will not write the code for you. For jumping, what you usually need is a velocity, and a way to change that velocity over time (acceleration/gravity). The position of your character is then modified by that velocity, multiplied with the time of each frame.

If this sounds too abstract, there are tons of little examples and tutorials about basic platform movement on the Internet, I'm sure you'll find a good jump mechanic :)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10821
    • View Profile
    • development blog
    • Email
Re: How to make my game character jump C++
« Reply #2 on: January 06, 2021, 01:52:20 pm »
I think there's a nice answer on this GameDev StackExchange question: https://gamedev.stackexchange.com/a/29618/141514

Please check which sub-forum you post your question next time and use [code=cpp][/code] tags for posting code :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything