Bonjour,
Je me suis mis a develloper un jeux avec la SFML sauf que je n'arrive pas a faire deplacer mon personnage
Voici mon code
Main.cpp
#include <SFML\Graphics.hpp>
#include <iostream>
#include "Human.h"
#include "Input.h"
using namespace std;
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Human vs Alien");
window.setFramerateLimit(60);
Human human;
Input input;
human.init(window);
human.setLife(100);
while (window.isOpen())
{
input.gestionInputs(window);
human.update(input);
window.clear();
human.draw(window);
window.display();
}
return 0;
}
Input.h
#ifndef INPUT_H
#define INPUT_H
#include <SFML\Graphics.hpp>
class Input
{
struct Button{ bool LEFT, RIGHT, UP, DOWN, SHOOT; };
public:
// Constructeur
Input::Input();
//Accesseur
Button Input::getButton(void) const;
//Mutateur
void Input::setButton(int bouton, bool etat);
//Fonction
void Input::gestionInputs(sf::RenderWindow &window);
void Input::getInput(sf::RenderWindow &window);
private:
// Variable de la classe en accès privé
sf::Event event;
Button button;
//Enum pour les boutons
const enum{ UP, DOWN, RIGHT, LEFT, SHOOT };
};
#endif // !INPUT_H
Input.cpp
#include "Input.h"
// Constructeur
Input::Input()
{
button.LEFT = button.RIGHT = button.UP = button.DOWN = button.SHOOT = false;
}
//Accesseur
Input::Button Input::getButton(void) const{ return button; }
//Mutateur
void Input::setButton(int bouton, bool etat)
{
switch (bouton)
{
case UP:
button.UP = etat;
break;
case DOWN:
button.DOWN = etat;
break;
case RIGHT:
button.RIGHT = etat;
break;
case LEFT:
button.LEFT = etat;
break;
case SHOOT:
button.SHOOT = etat;
break;
}
}
// Fonction
void Input::gestionInputs(sf::RenderWindow &window)
{
//Pour l'instant, on ne gère que le clavier.
//On gèrera les joysticks plus tard, s'il y en a
//un de branché.
//Pour l'instant, cette fonction n'est donc pas indispensable.
getInput(window);
}
void Input::getInput(sf::RenderWindow &window)
{
while (window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::KeyPressed:
switch (event.key.code)
{
case sf::Keyboard::Escape:
window.close();
break;
case sf::Keyboard::Left:
button.LEFT = true;
break;
case sf::Keyboard::Right:
button.RIGHT = true;
break;
case sf::Keyboard::Down:
button.DOWN = true;
break;
case sf::Keyboard::Up:
button.UP = true;
break;
default:
break;
}
break;
case sf::Event::KeyReleased:
switch (event.key.code)
{
case sf::Keyboard::Left:
button.LEFT = false;
break;
case sf::Keyboard::Right:
button.RIGHT = false;
break;
case sf::Keyboard::Down:
button.DOWN = false;
break;
case sf::Keyboard::Up:
button.UP = false;
break;
default:
break;
}
break;
// on ne traite pas les autres type d'évènement
}
}
}
Human.h
#ifndef HUMAN_H
#define HUMAN_H
#include <SFML\Graphics.hpp>
#include "Input.h"
class Human
{
public:
Human::Human();
// Accesseur
int Human::getX(void) const;
int Human::getY(void) const;
int Human::getW(void) const;
int Human::getH(void) const;
float Human::getDirX(void) const;
float Human::getDirY(void) const;
float Human::getStartX(void) const;
float Human::getStartY(void) const;
int Human::getLife(void) const;
int Human::getMunition(void) const;
int Human::getRecharge(void) const;
// Mutateur
void Human::setX(int valeur);
void Human::setY(int valeur);
void Human::setW(int valeur);
void Human::setH(int valeur);
void Human::setDirX(float valeur);
void Human::setDirY(float valeur);
void Human::setStartX(float valeur);
void Human::setStartY(float valeur);
void Human::setLife(int valeur);
void Human::setMunition(int valeur);
void Human::setRecharge(int valeur);
//Fonction
void Human::draw(sf::RenderWindow &window);
void Human::init(sf::RenderWindow &window);
void Human::update(Input &input);
private:
sf::Texture humanTexture;
sf::Sprite humanSprite;
const int speedPlayer = 5;
int x, y;
int w,h;
float dirX, dirY;
float startX, startY;
int munition;
int recharge;
int life;
int etat, direction;
enum { IDLE,LEFT, RIGHT, UP, DOWN, SHOOT };
};
#endif // !HUMAN_H
Human.cpp
#include <SFML\Graphics.hpp>
#include <iostream>
#include "Human.h"
#include "Input.h"
Input input;
Human::Human()
{
if (!humanTexture.loadFromFile("player/human-bas.png"))
{
std::cout << "Erreur lors du chargement de HUMAN" << std::endl;
}
else
{
humanSprite.setTexture(humanTexture);
}
x = y = 0;
w = h = 32;
dirX = 0;
dirY = 0;
startX = 100;
startY = 200;
munition = 32;
recharge = 3;
life = 100;
}
void Human::init(sf::RenderWindow &window)
{
x = y = 0;
w = h = 32;
dirX = 0;
dirY = 0;
startX = 100;
startY = 200;
munition = 32;
recharge = 3;
life = 100;
}
// Accesseur
int Human::getX(void) const{ return x; }
int Human::getY(void) const{ return y; }
float Human::getDirX(void) const{ return dirX; }
float Human::getDirY(void) const{ return dirY; }
float Human::getStartX(void) const { return startX; }
float Human::getStartY(void) const { return startY; }
int Human::getLife(void) const{ return life; }
int Human::getMunition(void) const{ return munition; }
int Human::getRecharge(void) const{ return recharge; }
// Mutateur
void Human::setX(int valeur){ x = valeur; }
void Human::setY(int valeur){ y = valeur; }
void Human::setDirX(float valeur){ dirX = valeur; }
void Human::setDirY(float valeur){ dirY = valeur; }
void Human::setStartX(float valeur) { startX = valeur; }
void Human::setStartY(float valeur) { startY = valeur; }
void Human::setLife(int valeur){ life = valeur; }
void Human::setMunition(int valeur){ munition = valeur; }
void Human::setRecharge(int valeur){ recharge = valeur; }
void Human::draw(sf::RenderWindow &window)
{
/*humanSprite.setPosition(sf::Vector2f(startX - w, startY - h));*/
window.draw(humanSprite);
}
void Human::update(Input &input)
{
if (input.getButton().UP == true)
{
std::cout << "UP" << std::endl;
y -= speedPlayer;
direction = UP;
}
else if (input.getButton().DOWN == true)
{
std::cout << "DOWN" << std::endl;
y += speedPlayer;
direction = DOWN;
}
else if (input.getButton().RIGHT == true)
{
std::cout << "RIGHT" << std::endl;
x += speedPlayer;
direction = RIGHT;
}
else if (input.getButton().LEFT == true)
{
std::cout << "LEFT" << std::endl;
x -= speedPlayer;
direction = LEFT;
}
}
Merci pour votre aide