graphics.h

Well , i wrote this code , which makes 2 lines of random colored circles and spawns a random colored circle at the bottom every time i press enter.
when i press enter , they get thrown into the circles that are placed at top .
I'm wondering how can i check if the ball that im throwing and the one which will be hit by that are the same color ? and how can i color both of them black?

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <graphics.h>
#include <time.h>


using namespace std;
int main()
{

    int g=0 , d=DETECT;
    initgraph(&g , &d , "");
    initwindow(420 , 900 , "Hobabha");
    int circle_x = 200 , circle_y =875 , circle_r = 25 , getp1 , getp2 , getp3 , getp4 , getp5;
    int cy = 20;
    char ch;
    srand(time(NULL));

    //DAYERE HAYE BALA

    for(int i=1 ; i<=381.6 ; i+=53)
    {
        int col = rand()%4 + 1;
        if(col == 1)    setcolor(1);
        if(col == 2)    setcolor(2);
        if(col == 3)    setcolor(3);
        if(col == 4)    setcolor(4);
        setfillstyle(SOLID_FILL , col);
        circle(20+i ,  cy , 25);
        floodfill(20 + i , cy , col);
    }
    for(int j=1 ; j<=381.6 ; j+=53)
    {
        int col = rand()%4 + 1;
        if(col == 1)    setcolor(1);
        if(col == 2)    setcolor(2);
        if(col == 3)    setcolor(3);
        if(col == 4)    setcolor(4);
        setfillstyle(SOLID_FILL , col);
        circle(20+j , cy + 53 , 25);
        floodfill(20 + j , cy + 50 , col);
    }


    //DAYEREYE PAIIN

int col = rand()%4 + 1;
        for(    ;   ;   )
        {

            ch=getch();

            setcolor(0);
            if(ch == 'A' || ch == 'a')
            {
                circle(circle_x , circle_y , circle_r);
                setcolor(col);
                circle_x-=5;

            }
            if(ch == 'D' || ch == 'd')
            {
                circle(circle_x , circle_y , circle_r);
                setcolor(col);
                circle_x+=5;
            }
            if( ch == 13)
            {

                for(int circle_y=875 ; circle_y>=25 ; circle_y--)
                {

                    setcolor(col);
                    setfillstyle(SOLID_FILL , col);
                    circle(circle_x , circle_y , circle_r);
                    floodfill(circle_x , circle_y , col);

                    getp1 = getpixel(circle_x+10 , circle_y - circle_r - 1.5);
                    getp2= getpixel(circle_x - 10 , circle_y - circle_r - 1.5);
                    getp3 = getpixel(circle_x , circle_y - circle_r - 1.5);
                    getp4 = getpixel(circle_x +25 , circle_y - circle_r - 1);
                    getp5 = getpixel(circle_x -25 , circle_y - circle_r -  1);

                    Sleep(10);


                    setcolor(0);
                    setfillstyle(SOLID_FILL , 0);

                    if(getp1 !=0 || getp2 !=0 || getp3 !=0 || getp4 !=0 || getp5 != 0)
                    {
                        col = rand()%4 + 1;
                        break;
                    }

                    circle(circle_x , circle_y  , circle_r);
                    floodfill(circle_x , circle_y , 0);


                }


            }

            circle(circle_x , circle_y , circle_r);
            setcolor(0);
            delay(10);
        }

    getch();
    return 0;
}
I'd recommend you to use classes in this situation, making every circle an instance of a different class with their own attributes (i.e: color), then using either a collision class or a collision function/method to check if they collide and compare their colors. Also I'd suggest to you to use either OpenGL(cross platform) or DirectX(windows only) as graphic libraries/frameworks, graphics.h is rather deprecated and offers far far less functionality/
Topic archived. No new replies allowed.