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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - MartyM

Pages: [1]
1
General / 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();
}

Pages: [1]
anything