Bonjour, je viens vous demander votre aide car voila une semaine que je corrige des erreurs et encore des erreurs et certaines persistent donc voila.
Je vous joint tout mes codes si vous pouvez résoudre mes erreurs et surtout m'aider a les comprendre.
Si vous pouvez aussi me dire se qui ne sert à rien.
Je vous remercie par avance pour votre aide qui je le sais sera conséquente.
************************************************************************************************
Code C/C++ :
//Bat.cc
#include "Bat.h"
#include "GameView.h"
//=======================================
// Constructeur
//=======================================
void Bat::collision1(int ballx, int bally, int modelh) {
if((_y)>(modelh) || _y<0) //collision
{
_y-=_dy;
}
if((_y>=bally) && ((_y+_h)<=(bally)) && (_x==ballx)) //rebonds raquette 1
{
_y-=_dy;
}
}
void Bat::collision2(int ballx, int bally, int ballw, int modelh) {
if((_y)>(modelh) || _y<0) //collision
{
_y-=_dy;
}
if((_y>=bally) && (_y+_h<=bally) && (_x==(ballx+ballw))) //rebonds raquette 2
{
_y-=_dy;
}
}
void Bat::moveUp(){
_y=_y+_dy;
}
void Bat::moveDown(){
_y=_y-_dy;
}
*************************************************************************************************
Code C/C++ :
//Bat.h
#ifndef _BAT_
#define _BAT_
#include "MovableElement.h"
class Bat : public MovableElement {
public:
Bat() : MovableElement() {}
Bat(int x, int y, int w, int h, int dx, int dy) : MovableElement(x, y, w, h, dx, dy) {}
~Bat();
void collision1(int ballx, int bally, int modelh);
void collision2(int ballx, int bally, int ballw, int modelh);
void moveUp();
void moveDown();
};
#endif
**********************************************************************************
Code C/C++ :
//Ball.cc
#include "Ball.h"
#include "GameView.h"
#include "GameModel.h"
//=======================================
// Accesseurs
//=======================================
int Ball::getDX() const {
return _dx;
}
void Ball::setDX(int dx) {
_dx= dx;
}
//=======================================
// Méthode
//=======================================
void Ball::move(int w, int h) {
_x=_x+_dx;
_y=_y+_dy;
if((_y+_h)>h) //rebonds parois horizontales
{
_y-=_dy;
_dy*=-1;
}
if(_y<0){
_y+=_dy;
_dy*=-1;
}
}
void Ball::repositionneBall(int w, int h){
setX(w/2);
setY(h/2);
}
int Ball::sortieCote(int w, int h) {
repositionneBall(w, h);
if(_x<0)
return 1;
else if(_x>w)
return 2;
else
return 0;
}
************************************************************************************
Code C/C++ :
//Ball.h
#ifndef _BALL_
#define _BALL_
#include "MovableElement.h"
#include "GameView.h"
class Ball : public MovableElement {
public:
Ball() : MovableElement() {}
Ball(int x, int y, int w, int h, int dx, int dy) : MovableElement(x, y, w, h, dx, dy) {}
~Ball();
int getDX() const;
void setDX(int dx);
void move(int w, int h); // déplacement balle
void repositionneBall(int w, int h);
int sortieCote(int w, int h); //detecte la sortie cote pour compabiliser les points
};
#endif
****************************************************************************************************
Code C/C++ :
//MovableElement.cc
#include "GameView.h"
#include "MovableElement.h"
//=======================================
// Constructeurs
//=======================================
MovableElement::MovableElement() : _x(0), _y(0), _w(20), _h(20), _dx(1), _dy(1){
} //constructeur par defaut
MovableElement::MovableElement(int x, int y, int w, int h, int dx, int dy)
:_x(x), _y(y), _w(w), _h(h), _dx(dx), _dy(dy){
} // constructeur surchargé
//=======================================
// Destructeur
//=======================================
MovableElement::~MovableElement(){}
//=======================================
// Accesseurs en lecture
//=======================================
int MovableElement::getX() const {
return _x;
}
int MovableElement::getY() const {
return _y;
}
int MovableElement::getW() const {
return _w;
}
int MovableElement::getH() const {
return _h;
}
int MovableElement::getDY() const {
return _dy;
}
//=======================================
// Accesseurs en écriture
//=======================================
void MovableElement::setX(int x) {
_x=x;
}
void MovableElement::setY(int y) {
_y=y;
}
void MovableElement::setW(int w) {
_w=w;
}
void MovableElement::setH(int h) {
_h=h;
}
void MovableElement::setDY(int dy) {
_dy=dy;
}
*********************************************************************************
Code C/C++ :
//MovableElement.h
#ifndef _MOVABLEELEMENT_
#define _MOVABLEELEMENT_
class MovableElement {
protected:
int _x, _y;
int _w, _h;
int _dx, _dy;
public:
MovableElement();
MovableElement(int x, int y, int w, int h, int dx, int dy);
~MovableElement();
int getX() const;
int getY() const;
int getW() const;
int getH() const;
int getDY() const;
void setX(int x);
void setY(int y);
void setW(int w);
void setH(int h);
void setDY(int dy);
};
#endif
**************************************************************************************
Code C/C++ :
//GameModel.cc
#include "GameModel.h"
#include "GameView.h"
#include "Ball.h"
#include "Bat.h"
#include "Tostring.h"
#include <iostream>
#include <string>
using namespace std;
using namespace sf;
//=======================================
// Constructeurs
//=======================================
GameModel::GameModel()
: _w(800), _h(600) {
_ball = new Ball();
_bat1 = new Bat();
_bat2 = new Bat();
scoreJoueur1 = 0;
scoreJoueur2 = 0;
_font = new Font();
_font->LoadFromFile("Antique Olive.ttf"); // utilisant la police "Antique Olive.ttf" fourni en fichier joint
}
//=======================================
GameModel::GameModel(int w, int h)
: _w(w), _h(h) {
_ball = new Ball(w/2, h/2, 20, 20, 1, 1);
_bat1 = new Bat(20, h/2, 20, 20, 1, 1);
_bat2 = new Bat(w-20, h/2, 20, 20, 1, 1);
}
//=======================================
// Destructeurs
//=======================================
GameModel::~GameModel(){
if(_ball != NULL){
delete _ball;
delete _bat1;
delete _bat2;
}
}
//=======================================
// Accesseurs en lecture
//=======================================
void GameModel::getBallPos(int &x, int &y) const {
x=_ball->getX();
y=_ball->getY();
}
void GameModel::getBallSize(int &w, int &h) const {
w=_ball->getW();
h=_ball->getH();
}
void GameModel::getBat1Pos(int &x, int &y) const {
x=_bat1->getX();
y=_bat1->getY();
}
void GameModel::getBat1Size(int &w, int &h) const {
w=_bat1->getW();
h=_bat1->getH();
}
void GameModel::getBat2Pos(int &x, int &y) const {
x=_bat2->getX();
y=_bat2->getY();
}
void GameModel::getBat2Size(int &w, int &h) const {
w=_bat2->getW();
h=_bat2->getH();
}
int GameModel::getScoreJoueur1() const {
return scoreJoueur1;
}
int GameModel::getScoreJoueur2() const {
return scoreJoueur2;
}
void GameModel::setScoreJoueur1(int scoreJ1) {
scoreJoueur2 = scoreJ1;
}
void GameModel::setScoreJoueur2(int scoreJ2) {
scoreJoueur1 = scoreJ2;
}
//=======================================
// Traitement de l'affichage
//=======================================
string GameModel::toString() const {
string scores = "";
scores += intToString(scoreJoueur1) + " - ";
scores += intToString(scoreJoueur2);
return scores;
}
//=======================================
// Calcul la prochaine étape
//=======================================
void GameModel::joueur1(bool up) {
int ballx, bally;
getBallPos(ballx, bally);
if(up)
_bat1->moveUp();
else
_bat1->moveDown();
_bat1->collision1(ballx, bally, _h);
}
void GameModel::joueur2(bool up) {
int ballx, bally, ballw, ballh;
getBallPos(ballx, bally);
getBallSize(ballw, ballh);
if(up)
_bat2->moveUp();
else
_bat2->moveDown();
_bat2->collision2(ballx, bally, ballw, _h);
}
string GameModel::retourneScore(){
string scores="";
int coteSortie = (_ball->sortieCote(_w, _h));
if (coteSortie == 2) {
setScoreJoueur1(getScoreJoueur1() + 1);
scores = toString();
}
else if (coteSortie == 1) {
setScoreJoueur2(getScoreJoueur2() + 1);
scores = toString();
}
return scores;
}
void GameModel::nextStep(){
int ballx, bally, ballw, ballh;
getBallPos(ballx, bally);
getBallSize(ballw, ballh);
if(_ball != NULL)
_ball->move(ballw, ballh);// Deplacement de la balle
/*if((ballx<0) || (ballx > _w)){
_font=new Font();
_font->afficheScore(); // affichage du score
delete _font;
}*/
}
*********************************************************************************
Code C/C++ :
//GameModel.h
#ifndef _GAME_MODEL_
#define _GAME_MODEL_
#include <SFML/System.hpp>
#include "Ball.h"
#include "Bat.h"
#include "GameView.h"
#include <string>
using namespace sf;
using namespace std;
class GameModel {
private:
int _w, _h;
int scoreJoueur1, scoreJoueur2;
Ball * _ball;
Bat * _bat1;
Bat * _bat2;
Font * _font;
public:
GameModel();
GameModel(int w, int h);
~GameModel();
void getBallPos(int &x, int &y) const;
void getBallSize(int &w, int &h) const;
void getBat1Pos(int &x, int &y) const;
void getBat1Size(int &w, int &h) const;
void getBat2Pos(int &x, int &y) const;
void getBat2Size(int &w, int &h) const;
int getScoreJoueur1() const;
int getScoreJoueur2() const;
void setScoreJoueur1(int scoreJ1);
void setScoreJoueur2(int scoreJ2);
string toString() const;
void joueur1(bool up);
void joueur2(bool up);
string retourneScore();
void nextStep();
};
#endif
**************************************************************************************************
Code C/C++ :
//GameView.cc
#include "GameView.h"
#include "GameModel.h"
#include "Bat.h"
#include <string>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
using namespace std;
using namespace sf;
//=======================================
// Constructeur
//=======================================
GameView::GameView(int w, int h): _w(w), _h(h){
_window = new RenderWindow(sf::VideoMode(w, h, 32), "Pong sfml Cyril", sf::Style::Resize);
_font = new Font();
_font->LoadFromFile("Antique Olive.ttf"); // utilisant la police "Antique Olive.ttf" fourni en fichier joint
string affichageDesScores= "0 - 0";
affichageDesScores.SetFont(_font);
affichageDesScores.SetSize(100);
affichageDesScores.SetColor(sf::Color(255, 64, 162));
affichageDesScores.Move(((_w/2) - 100), ((_h/2) - 50));
if (!_background_image.LoadFromFile("background.png") ||
!_ball_image.LoadFromFile("ball.png") ||
!_bat1_image.LoadFromFile("bat.png") ||
!_bat2_image.LoadFromFile("bat.png")){
_background_sprite = Sprite ();
_ball_sprite = Sprite();
}
else {
_background_sprite = Sprite (_background_image);
_background_sprite.Resize(_w, _h);
_background_sprite.SetPosition(0,0);
_ball_sprite = Sprite(_ball_image);
_bat1_sprite = Sprite(_bat1_image);
_bat2_sprite = Sprite(_bat2_image);
}
}
//=======================================
// Destructeur
//=======================================
GameView::~GameView(){
if(_window!= NULL){
delete _window;
delete _font;
}
}
//=======================================
// Accesseurs en écriture
//=======================================
void GameView::setModel(GameModel * model){
_model = model;
}
//=======================================
// Fonction de dessin
//=======================================
void GameView::draw(){
_window->Draw(_background_sprite);
//balle
int x_ball, y_ball;
int w_ball, h_ball;
_model->getBallPos(x_ball, y_ball);
_model->getBallSize(w_ball, h_ball);
_ball_sprite.Resize(w_ball, h_ball);
_ball_sprite.SetPosition(x_ball, y_ball);
_window->Draw(_ball_sprite);
//raquette 1
int x_bat1, y_bat1;
int w_bat1, h_bat1;
_model->getBat1Pos(x_bat1,y_bat1);
_model->getBat1Size(w_bat1, h_bat1);
_bat1_sprite.Resize(w_bat1, h_bat1);
_bat1_sprite.SetPosition(x_bat1, y_bat1);
_window->Draw(_bat1_sprite);
//raquette 2
int x_bat2, y_bat2;
int w_bat2, h_bat2;
_model->getBat2Pos(x_bat2, y_bat2);
_model->getBat2Size(w_bat2, h_bat2);
_bat2_sprite.Resize(w_bat2, h_bat2);
_bat2_sprite.SetPosition(x_bat2, y_bat2);
_window->Draw(_bat2_sprite);
//Scores
string scores = _model->retourneScore();
affichageDesScores.SetTexte= scores;
/*
scores.SetFont(* _font); // scores sera afficher en utilisant la Font : "_font"
scores.SetSize(100.f); // change la taille de la police */
_window->Draw(affichageDesScores);
_window->Display();
usleep(5000);
usleep(500);
}
//=======================================
// Traitement des evenements
// Retourne false si un evenement de fin est reçu
//=======================================
bool GameView::treatEvents(){
bool result = false;
if(_window->IsOpened()){
result = true;
sf::Event event;
while (_window->GetEvent(event)) {
bool up=false;
if ((event.Type == sf::Event::Closed) || ((event.Type == sf::Event::KeyPressed) && (event.Key.Code == sf::Key::Escape)))
{
_window->Close();
result = false;
}
else if ((event.Type == sf::Event::KeyPressed) && (event.Key.Code == sf::Key::A))
{
up=true;
_model->joueur1(up);
}
else if ((event.Type == sf::Event::KeyPressed) && (event.Key.Code == sf::Key::W))
{
_model->joueur1(up);
}
else if ((event.Type == sf::Event::KeyPressed) && (event.Key.Code == sf::Key::Up))
{
up=true;
_model->joueur2(up);
}
else if ((event.Type == sf::Event::KeyPressed) && (event.Key.Code == sf::Key::Down))
{
_model->joueur2(up);
}
}
}
return result;
}
******************************************************************************************
Code C/C++ :
//GameView.h
#ifndef _GAMEVIEW_
#define _GAMEVIEW_
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <sstream>
using namespace std;
using namespace sf;
class GameModel;
class GameView {
private:
int _w, _h;
string affichageDesScores;
Font * _font;
RenderWindow * _window;
Image _background_image;
Image _ball_image;
Image _bat1_image;
Image _bat2_image;
Sprite _background_sprite;
Sprite _ball_sprite;
Sprite _bat1_sprite;
Sprite _bat2_sprite;
GameModel * _model;
public:
GameView(int w, int h);
~GameView();
void setModel(GameModel * _model);
void draw();
bool treatEvents();
};
#endif
**************************************************************************************
Code C/C++ :
//Tostring.cc
#include "Tostring.h"
#include <cmath>
#include <cassert>
using namespace std;
string intToString(int x){
string x_s;
int a;
if(x==0)
x_s="0";
else{
x_s="";
if(x<0)
a=-x;
else
a=x;
while(a>0){
x_s= ((char)('0'+a%10))+x_s;
a/=10;
}
if(x<0)
x_s="-"+x_s;
}
return x_s;
}
*****************************************************************************
Code C/C++ :
//Tostring.h
#include <string>
using namespace std;
string intToString(int n);
*******************************************************************************************
Code C/C++ :
//Main.cc
#include "GameView.h"
#include "GameModel.h"
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
int main(){
srand(time(NULL));
GameModel * model = new GameModel(SCREEN_WIDTH, SCREEN_HEIGHT);
GameView * view = new GameView(SCREEN_WIDTH, SCREEN_HEIGHT);
view->setModel(model);
while(view->treatEvents()){
model->nextStep();
view->draw();
}
delete view;
delete model;
return EXIT_SUCCESS;
}
**************************************************************************************
Cyril Z.