Bonjour , je suis en train de programmer un jeu de la vie, mais je me heurte a un problème lorsque je veux qu'un cellule morte devienne vivante et change de couleur lorsque je clic sur celle ci.*
J'utilise SFML pour la fenêtre du jeu.
Le code est assez long mais plutôt simple. j'espère que quelqu'un me donnera un coup de main je suis un peu desesperé j'imagine que cela doit être un problème assez simple auquel je n'ai pas du tout penser.*
En gros dans mon main je detecte l'event mousebutton left is pressed, et j'utilise une de mes méthodes pour que la cellule sous le curseur passe de la mort a la vie, elle est aussi sensé passé de la couleur blanche a la couleur noire, or rien de ceci ne se passe.
Il est ecrit ci dessous.
Merci d'avance si vous décidez de vous y arrêter pour m'aider.
Main
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include "Grid.h"
*
using namespace std;
*
*
int main()
{
****sf::RenderWindow window(sf::VideoMode(800,600,3 2),"GOL",sf::Style::Close);
****window.setFramerateLimit(1 0);
*
****sf::Event e;
*
****Grid grid(800,600);
*
*
****const int UPDATING=0, MODIFYING =1;
****int state=MODIFYING;
*
****while(window.isOpen())
****{
********while(window.pollEvent (e))
********{
************if(e.type == sf::Event::Closed)
************{
****************window.close() ;
************}
*
************if(e.type == sf::Event::KeyPressed)
************{
****************switch(e.key.c ode)
****************{
********************case sf::Keyboard::Escape:
************************window .close();
************************break;
*
********************default:br eak;
****************}
************}
*
************if(e.type == sf::Event::MouseButtonPressed)
************{
****************if(e.mouseButt on.button== sf::Mouse::Left)
****************{
********************int x;
********************int y;
*
********************x=sf::Mous e::getPosition(window).x/20;
********************cout<<x;
********************y=sf::Mous e::getPosition(window).y/20;
*
********************if(x >= 0 && y >= 0 && x <=39 && y <=29)
********************{
************************grid.M ettreCellA(!grid.cellVivanteA( x,y),x,y);
************************cout<< grid.cellVivanteA(x,y);
************************cout<< "hey";
************************
********************}
*
*
****************}
************}
*
*
********}
*********
*********
*
********if(state==UPDATING)
********{
************grid.NextGeneratio n();
********}
*
*
********window.clear(sf::Color ::White);
********grid.draw(window);
********window.display();
*
*
*
*
****}
*
*
****return 0;
}
Cell.h(classe)
#include <iostream>
#include <SFML/Graphics.hpp>
*
*
#define CELL
#ifdef* CELL
*
using namespace sf;
*
class Cell
{
public:
********Cell();
********Cell(bool alive,int x,int y);
********bool cellVivante();
********void setCellstate(bool alive);
********void draw(sf::RenderWindow &window);
********sf::RectangleShape* getshape();
*
private:
****sf::RectangleShape m_shape;
****bool m_alive;
*
*
};
#endif // CELL
Grid.h(classe)
#define GRID
#ifdef* GRID
#include <iostream>
#include <SFML/Graphics.hpp>
#include "Cell.h"
#include <vector>
*
*
class Grid
{
*
protected:
*********int m_largeur;
*********int m_hauteur;
*********Cell m_cells[40][30];
*
public:
********Grid();
********Grid( int largeur, int hauteur);
********bool cellVivanteA(int x,int y);
********void MettreCellA(bool alive, int x,int y);
********void NextGeneration();
********void draw(sf::RenderWindow &window); //dessiner la fenetre
*
};
#endif // GRID
Cell.cpp*
#include <iostream>
#include "Cell.h"
#include <SFML/Graphics.hpp>
*
using namespace std;
*
Cell::Cell()
{
*
}
*
Cell::Cell(bool alive, int x , int y)
{
****this->m_shape = sf::RectangleShape(sf::Vector2 f(20,20));
****this->m_shape.setPosition(x,y);
****this->m_alive=alive;
*
****if(alive)
****{
********this->m_shape.setFillColor(sf::Colo r::Black);
*
****}
****else
****{
********this->m_shape.setFillColor(sf::Colo r::White);
****}
*
*
*
}
*
bool Cell::cellVivante()
{
****return m_alive;
}
*
void Cell::setCellstate(bool alive)
{
****if(alive)
****{
********this->m_shape.setFillColor(sf::Colo r::Black);
****}
****else
****{
********this->m_shape.setFillColor(sf::Colo r::White);
****}
}
*
sf::RectangleShape* Cell::getshape()
{
****return &m_shape;
}
*
void Cell::draw(sf::RenderWindow &window)
{
****window.draw(m_shape);
}
Grid.cpp
#include <iostream>
#include "SFML/Graphics.hpp"
#include "Grid.h"
*
using namespace std;
*
Grid::Grid(int largeur, int hauteur )
{
*
*****int x,y;
*****for(x=0;x<m_largeur/20;x++)
*****{
*********for(y=0;y<m_hauteur/20;y++)
*********{
************m_cells[x][y]=Cell(false,x,y);
*********}
*****}
*
*****this->m_hauteur=m_hauteur/20;
*****this->m_largeur=m_largeur/20;
}
*
bool Grid::cellVivanteA(int x,int y)
{
***return m_cells[x][y].cellVivante();
*
*
}
*
void Grid::MettreCellA(bool alive, int x ,int y)
{
****m_cells[x][y].setCellstate(alive);
*
}
*
void Grid::draw(sf::RenderWindow &window)
{
****int x,y;
****for(x=0;x<m_largeur;x++)
****{
********for(y=0;y<m_hauteur;y+ +)
********{
************m_cells[x][y].draw(window);
********}
****}
}
*
void Grid::NextGeneration()
{
*
}
END
-----