Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: make game

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Junior Member Newbie
    Join Date
    Oct 2017
    Posts
    1

    Question make game

    Hi everyone i try to make a game but this is very difficult The rules of the game is the player who has 25 cells in this game which has 5 girls and 5 boys and one bomb if the bomb has been lost. If he has 5 girls and 5 boys wins the game using the function Mouse Click the code very long i'm sorry just pleas help me and thanks
    Click image for larger version. 

Name:	2.jpg 
Views:	11 
Size:	60.9 KB 
ID:	2526
    Code :
    #include <vector>
    #include <ctime>
    #include <string>
    #include <glut.h>
     
    enum type { empty = 0, male = 1, female = 2, bomb = 3};
     
    class Color
    {
    public:
    	Color(double r, double g, double b)
    		: red(r), green(g), blue(b)
    	{}
    	double red, green, blue;
    };
     
    class Position
    {
    public:
    	Position(float X = 0.0, float Y = 0.0)
    		: x(X), y(Y)
    	{}
    	float x, y;
    };
     
    class Rectangle
    {
    public:
    	Rectangle()
    	: pos(Position (0.0, 0.0)), width(0.0), height(0.0)
    	{}
    	Rectangle(Position p, double w, double h)
    		: pos(p), width(w), height(h)
    	{}
    	Position pos; // left bottom position
    	double width, height;
    };
     
    void drawBoy(const Rectangle& rec);
    void drawGirl(const Rectangle& rec);
    void drawBomb(const Rectangle& rec);
    void drawRect(const Rectangle& rec, Color clr);
    void drawRectFilled(const Rectangle& rec, Color clr);
    void drawCircle(const Position& pos, double radiusX, double radiusY, Color clr);
    void drawCircleFilled(const Position& pos, double radiusX, double radiusY, Color clr);
    void FillGrid();
     
    class Cell
    {
    public:
    	Cell(Rectangle r)
    		: rec(r), clicked(false), typeCell(empty)
    	{}
    	bool IsInside(double x, double y) const
    	{
    		if (x >= rec.pos.x && x <= rec.pos.x + rec.width && y >= rec.pos.y && y <= rec.pos.y + rec.height)
    			return true;
    		return false;
    	}
    	void Draw() const
    	{
    		if (!clicked)
    			drawRectFilled(rec, Color(0.7, 0.7, 0.7));
     
    		//TODO
    	}
     
    	void DrawBorder() const
    	{
    		drawRect(rec, Color(0, 0, 0));
    	}
    	Rectangle rec;
    	bool clicked;
    	type typeCell;
    };
     
     
     
    std::vector<Cell> vecGrid;
     
    Rectangle recGrid, recInfo;
     
    int width = 600, height = 700;
    const int numCells = 25,
    			numBoys = 5,
    			numGirls = 5,
    			numBombs = 1;
     
    int numBoysFound = 0,
    	numGirlsFound = 0,
    	numBombsFound = 0;
     
    bool bWon = false, bLost = false;
     
    Cell cellMale(Rectangle(Position (100, height - 50), 40, 40));
    Cell cellFemale(Rectangle(Position(210, height - 50), 40, 40));
    Cell cellBomb(Rectangle(Position(210, height - 90), 40, 40));
     
    void renderBitmapString(float x, float y, float z, void* font, const char* string)
    {
    	const char *c;
    	glRasterPos3f(x, y, z);
    	for (c = string; *c != '\0'; c++)
    		glutBitmapCharacter(font, *c);
    }
     
    void Init ()
    {
    	glClearColor(1.0, 1.0, 1.0, 0.0);	// set display-window color to white
     
    	recGrid.pos.x = 0;
    	recGrid.pos.y = 0;
    	recGrid.width = width;
    	recGrid.height = 0.857 * height;
     
    	recInfo.pos.x = recGrid.height;
    	recInfo.pos.y = 0;
    	recInfo.width = width;
    	recInfo.height = height - recGrid.height;
     
    	double cellW = recGrid.width / (int)sqrt(numCells);
    	double cellH = recGrid.height / (int)sqrt(numCells);
     
    	for (int i = 0; i < (int)sqrt(numCells); ++i)
    		for (int j = 0; j < (int)sqrt(numCells); ++j)
    			vecGrid.push_back(Cell(Rectangle(Position (j * cellW, i * cellH), cellW, cellH)));
     
    	FillGrid();
    }
     
    void FillGrid()
    {
    	srand(time(0));
    	// randomally position males
    	int i = 0;
    	while (i < numBoys)
    	{
    		int pos = rand() % numCells;
    		if (vecGrid[pos].typeCell == empty)
    		{
    			vecGrid[pos].typeCell = male;
    			++i;
    		}
    	}
    	// randomally position females
    	i = 0;
    	while (i < numGirls)
    	{
    		int pos = rand() % numCells;
    		if (vecGrid[pos].typeCell == empty)
    		{
    			vecGrid[pos].typeCell = female;
    			++i;
    		}
    	}
    	// randomally position bombs
    	i = 0;
    	while (i < numBombs)
    	{
    		int pos = rand() % numCells;
    		if (vecGrid[pos].typeCell == empty)
    		{
    			vecGrid[pos].typeCell = bomb;
    			++i;
    		}
    	}
    }
     
    void MouseClick(int button, int state, int x, int y)
    {
    	if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN && !bWon && !bLost)
    	{
    		//TODO
    	}
    }
     
    void Keyboard(unsigned char key, int x, int y)
    {
    	if (key == 27)
    		exit(0);
    	else if (key == 'r' || key == 'R')
    	{
    		//TODO
    	}
    }
     
    void Reshape (int width, int height)
    {
    	glViewport(0, 0, width, height);
     
    	glMatrixMode(GL_PROJECTION);		// set projection parameters
    	glLoadIdentity();
    	gluOrtho2D(0.0, width, 0.0, height);
     
    	glMatrixMode(GL_MODELVIEW);
    	glLoadIdentity();
     
    }
     
    void Display ()
    {
    	glClear (GL_COLOR_BUFFER_BIT);		// clear display window
     
    	glColor3f(0, 0, 0);
    	std::string str = "Found: " + std::to_string(numBoysFound) + "/" + std::to_string(numBoys);
    	renderBitmapString(0, height - 40, 0, GLUT_BITMAP_TIMES_ROMAN_24,  str.c_str());
    	str = " and " + std::to_string(numGirlsFound) + "/" + std::to_string(numGirls);
    	renderBitmapString(130, height - 40, 0, GLUT_BITMAP_TIMES_ROMAN_24, str.c_str());
    	str = "Be careful, there's  " + std::to_string(numBombs);
    	renderBitmapString(0, height - 80, 0, GLUT_BITMAP_TIMES_ROMAN_24, str.c_str());
     
    	cellMale.typeCell = male;
    	cellMale.clicked = true;
    	cellMale.Draw();
     
    	cellFemale.typeCell = female;
    	cellFemale.clicked = true;
    	cellFemale.Draw();
     
    	cellBomb.typeCell = bomb;
    	cellBomb.clicked = true;
    	cellBomb.Draw();
     
    	if (bWon)
    	{
    		drawRectFilled(Rectangle (Position(300, height - 80), 250, 70), Color(0, 1, 0));
    		glColor3f(0, 0, 0);
    		renderBitmapString(320, height - 55, 0, GLUT_BITMAP_TIMES_ROMAN_24, "Congrats, you won.");
    	}
    	if (bLost)
    	{
    		drawRectFilled(Rectangle(Position(300, height - 80), 250, 70), Color(1, 0, 0));
    		glColor3f(0, 0, 0);
    		renderBitmapString(320, height - 55, 0, GLUT_BITMAP_TIMES_ROMAN_24, "Game over, you Lost.");
    	}
    	for (unsigned i = 0; i < vecGrid.size(); ++i)
    	{
    		vecGrid[i].Draw();
    		vecGrid[i].DrawBorder();
    	}
     
    	glFlush ();							// process all openGl routines as quickly as possible
    }
     
    void main (int argc, char** argv)
    {
    	glutInit (&argc, argv);							// initialize GLUT
    	glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);	// set display mode
    	glutInitWindowPosition (200, 0);				// set top left display window position
    	glutInitWindowSize(width, height);					// set display window width and height
    	glutCreateWindow ("Homework 1");		// create display window
     
    	Init ();										// execute initialization function
    	glutReshapeFunc(Reshape);						// set the reshape callback for the current window
    	glutDisplayFunc (Display);						// send graphics to display window
    	glutIdleFunc(Display);
    	glutMouseFunc(MouseClick);
    	glutKeyboardFunc(Keyboard);
    	glutMainLoop ();								// dispaly everthing and wait
    }
     
    void drawBoy(const Rectangle& rec)
    {
    	Color clr(0.0, 0.0, 255 / 255.0); // blue color
    	//TODO
    }
     
    void drawGirl(const Rectangle& rec)
    {
    	Color clr(255 / 255.0, 0 / 255.0, 127 / 255.0);
     
    	//TODO
    }
     
    void drawBomb(const Rectangle& rec)
    {
    	//TODO
    }
     
    void drawRect(const Rectangle& rec, Color clr)
    {
    	glColor3f(clr.red, clr.green, clr.blue);
    	glLineWidth(1);
    	glBegin(GL_LINE_LOOP);
    		glVertex2d(rec.pos.x, rec.pos.y);
    		glVertex2d(rec.pos.x + rec.width, rec.pos.y);
    		glVertex2d(rec.pos.x + rec.width, rec.pos.y + rec.height);
    		glVertex2d(rec.pos.x, rec.pos.y + rec.height);
    	glEnd();
    }
     
    void drawRectFilled(const Rectangle& rec, Color clr)
    {
    	glColor3f(clr.red, clr.green, clr.blue);
    	glBegin(GL_QUADS);
    		glVertex2d(rec.pos.x, rec.pos.y);
    		glVertex2d(rec.pos.x + rec.width, rec.pos.y);
    		glVertex2d(rec.pos.x + rec.width, rec.pos.y + rec.height);
    		glVertex2d(rec.pos.x, rec.pos.y + rec.height);
    	glEnd();
    }
     
    void drawCircle(const Position& pos, double radiusX, double radiusY, Color clr)
    {
    	glColor3f(clr.red, clr.green, clr.blue);
    	const float DEG2RAD = 3.14159 / 180;
    	glBegin(GL_LINE_LOOP);
    	for (int i = 0; i < 360; i++) {
    		float degInRad = i * DEG2RAD;
    		glVertex2d(pos.x + cos(degInRad) * radiusX,
    			pos.y + sin(degInRad) * radiusY);
    	}
    	glEnd();
    }
     
    void drawCircleFilled(const Position& pos, double radiusX, double radiusY, Color clr)
    {
    	glColor3f(clr.red, clr.green, clr.blue);
    	const float DEG2RAD = 3.14159 / 180;
    	glBegin(GL_TRIANGLE_FAN);
    	glVertex2d(pos.x, pos.y);
    	for (int i = -1; i < 360; i++) {
    		float degInRad = i * DEG2RAD;
    		glVertex2d(pos.x + cos(degInRad) * radiusX,
    			pos.y + sin(degInRad) * radiusY);
    	}
    	glEnd();
    }

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2007
    Posts
    1,715
    Please don't cross-post to multiple subforums. It doesn't make you more likely to get a response, it does make you more likely to just annoy people.

  3. #3
    Senior Member OpenGL Pro
    Join Date
    Jan 2007
    Posts
    1,715
    Cross-posting won't make it quicker. There are many reasons why nobody has answered, including the simple fact that nobody might have been online.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •