Trying to make a simple game.

Hello everyone .
So , there's a simple game which is our class project for this term and I'm currently thinking about it . The game is supposed to
Spawn random shapes(Triangle , rectangle and circle using graphics.h) falling from the top of the window one by one . And the players can control the shape until it falls(something like Tetris) .It gives a +1 point for each 3 similar shapes that get on top of each other and the game finishes when the screen is full of shapes .
the game repeats for 3 times (So the whole game must be written in a "for" loop) . and the results will be shown on a diagram for each player.

As I'm new in c++ coding , that might be a little bit confusing for me .
P.S. i know that graphics.h,conio.h and the other includes that I'm going to use aren't supported anymore but its a must-use for our class project.
So I'm having some questions :
1.How can i make random shapes spawn? We use to use rand(); on CodeBlocks to make random numbers . but how is it done for shapes?

2.How can i make the shapes stay ON each other and avoid them from getting inside each other?

3.How can i check if there are three similar shapes on the top of each other?
Hi,

1.How can i make random shapes spawn? We use to use rand(); on CodeBlocks to make random numbers . but how is it done for shapes?


Presumably you will be using classes, but I suppose one could get to work with a POD struct C style:

Have a std::vector<Shape *> or just an ordinary array: Shape* ShapeArray[3] , then use your random number on the subscript of the array or vector. This is just for the purposes of selectign a random Shape, you will need another container for the shapes to be dsrawn on screen.

When initialising them, push_back the individual derived shapes (Triangle , rectangle and circle etc. ) . This is a very handy aspect of polymorphism: The container is declared as pointer to base class, but one puts pointers to derived class into it. There is no conversion to base pointer, but the relevant derived class function is called via the pointer. The functions need to be virtual, here is a rough outline:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class Shape {
public:
         virtual draw() = 0; // pure virtual function - must be redefined by derived class
         virtual move (const int deltaX, const int deltaY) = 0;
protected:

        BoundingBox ShapeBdyBox;  // make another class for this with it's own interface, make sure it is initialised then
                                                       //  updated after every call to move.
};

class Circle : public Shape {
    public:
     Circle (const double radius, const unsigned int centreX,  const unsigned int centreY)
               :  // member initialiser list
    radius(radius),
    centreX(centreX),
    centreY(centreY),
    ShapeBdyBox    //  call BoundingBox construtor  - you can do the arguments :+)
   {
     // call a validation function here

    };

public:

    draw();    // put definition of these in the Circle.cpp file
    move();  // changes position of object, and updates bounding box

private:

    double radius;
    unsigned int centreX;
    unsigned int centreY;

    VaidateInput(); // radius is positive
    UpDateBoundingBox ();  //call BoundingBox.Update interface function
     

};  // end class Circle


int main() {

//vector of valid shapes

std::vector<Shape*>  VaildShapes;

// push_back or emplace_back 1 of each of the Shapes into VaildShapes

// function that returns random number

// create a shape of the type from VaildShapes[randomnumber]

// push that Shape into another vector which is the ShapesOnScreen say, this is the first part of the Polymorphism

// iterate through ShapesOnScreen drawing them in their initial position, Polymorphism happening here the derived draw function called - ShapesOnScreen[variable]->draw() does the right thing

// have a loop that keeps iterating through  all the ShapesOnScreen with a loop, moving them and checking bounding box
// Polymorphism happenning here too.
return 0;
}


2.How can i make the shapes stay ON each other and avoid them from getting inside each other?


Bounding Box.

3.How can i check if there are three similar shapes on the top of each other?


Divide the screen up into columns, if the centre of a bounding box is in that column, then that shape is deemed to be in that column. The sideways move commands could ensure the Shape is central in the columnm so it looks nice. Maintain a vector for each column - it contains pointers to the Shape in that column. You could do poor person RTTI, by having a private variable in each derived Shape, saying what type of shape it is. Or use type_traits is_same to determine what Shape it is.

Good Luck !!
well that was a bit confusing for me , as a starter!
I found an easier way for the random thing .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <graphics.h>
#include <time.h>

using namespace std;

int main()
{
    int g=0 , d=DETECT;
    int rshapes,y=-100 , circler=45;
    initgraph(&g, &d, "");
    srand(time(NULL));
    initwindow(400 , 900 , "Game");
    int circle_x = 200;

    //Intro
    setcolor(10);
    settextstyle(COMPLEX_FONT , HORIZ_DIR , 200);
    outtextxy(100, 285, "SRGP\n");
    setcolor(12);
    settextstyle(GOTHIC_FONT , HORIZ_DIR , 47);
    outtextxy(100 , 300 , "GAME");
    delay(2000);
    cleardevice();
    char ch;




        for(int i=1 ; i<=40 ; i++)
        {

            rshapes= rand()%3 + 1;

            for(y=-100 ; y<=900-circler ; y++)
            {
                if(rshapes== 1)
                {
                    setcolor(4);
                    circle(200 , y , circler);

                }

                else if(rshapes== 2)
                {

                    setcolor(5);
                    circle(200 , y , circler);

                }
                else if(rshapes== 3)
                {

                    setcolor(6);
                    circle(200 , y , circler);

                }

                delay(20);
                setcolor(0);
                circle(200 , y , circler);

            }


        }



    getch();
    return 0;
}



the shapes fall down but after they fall , they get disappeared .
Does someone have any ideas about how to fix it?
Topic archived. No new replies allowed.