Make circles move to opposing directions after sometime

Hey, I am new in c++ programming and I wanted to write a small game using classes and objects. So my problem is that I would like to make the two 'Enemy' circles 'tp' (top) and 'btm' (bottom) move randomly in the x plane, let's say after every 10 seconds they change direction either from left to right or vice versa INDEPENDENTLY. I know my whole code is not the best or the most efficient but please just bear with me, Thanks in advance.

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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
  #include <iostream>
#include <graphics.h>
#include <dos.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <windows.h>

using namespace std;

class hero {
protected:
    string heroname;
    int heroHP;
    int heroattack;
    int i;
    int j;

public:
    void setheroname(string hm)
    {
        heroname = hm;
    }
    string getheroname()
    {
        return heroname;
    }
    void setcirc1(int circ1)
    {
        i = circ1;
    }
    void setcirc2(int circ2)
    {
        j = circ2;
    }
    int getcirc1()
    {
        return i;
    }
    int getcirc2()
    {
        return j;
    }
    int getheroHP(void)
    {
        return heroHP;
    }

    hero()
    {
        heroHP = 100;
        heroattack = 10;
    }
};

class enemy {
protected:
    string enemyname;
    int enemyHP;
    int enemyattack;
    int a;
    int b;
    int c;
    int d;

public:
    void setenemyname(string en)
    {
        enemyname = en;
    }
    string getenemyname(void)
    {
        return enemyname;
    }
    void setenemyattack(int ea)
    {
        enemyattack = ea;
    }
    int getenemyattack(void)
    {
        return enemyattack;
    }
    int getenemyHP(void)
    {
        return enemyHP;
    }
    void setEcirc1(int Ecirc1)
    {
        a = Ecirc1;
    }
    void setEcirc2(int Ecirc2)
    {
        b = Ecirc2;
    }
    int getEcirc1()
    {
        return a;
    }
    int getEcirc2()
    {
        return b;
    }
    void setEcirc3(int Ecirc3)
    {
        c = Ecirc3;
    }
    void setEcirc4(int Ecirc4)
    {
        d = Ecirc4;
    }
    int getEcirc3()
    {
        return c;
    }
    int getEcirc4()
    {
        return d;
    }
    virtual void print() = 0;
};

class bottom : public enemy {
public:
    void setbtmhp(int btmhp)
    {
        enemyHP = btmhp;
    }
    void print()
    {
        cout << "I am a bottom, bottom takes - " << getenemyattack() << " from your bottom" << endl;
    }
};
class top : public enemy {
public:
    void settophp(int tophp)
    {
        enemyHP = tophp;
    }
    void print()
    {
        cout << "I am a top, top takes - " << getenemyattack() << " from your head" << endl;
    }
};

int main()
{
    int x = 0, y = -1, ch, gd = DETECT, gm;
    int t = 0, s = -1;
    hero stan;
    bottom btm;
    top tp;
    stan.setheroname("Stan");
    stan.setcirc1(250);
    stan.setcirc2(250);
    int i = stan.getcirc1(), j = stan.getcirc2(); //Giving the position settings of hero circle to var i & j
    btm.setenemyattack(5);
    tp.setenemyattack(5);
    btm.setbtmhp(10);
    tp.settophp(10);
    btm.setEcirc1(350);
    btm.setEcirc2(350);
    tp.setEcirc3(400);
    tp.setEcirc4(400);
    int a = btm.getEcirc1(), b = btm.getEcirc2(); //Giving the position settings of enemy circle to var a b c &d
    int c = tp.getEcirc3(), d = tp.getEcirc4();
    int HP = stan.getheroHP(); //giving the HP settings of hero circle to var HP
    initgraph(&gd, &gm, "c:/turboc3/bgi");
    while (1) //1 represent true in boolean, 0 would have represented false.
    //so while (1) implies inifinity since true goes for everything
    {
        //Circles
        circle(i, j, 5);
        setcolor(LIGHTBLUE);
        circle(a, b, 5);
        setcolor(LIGHTRED);
        circle(c, d, 5);
        setcolor(YELLOW);
        outtextxy(400, 400, "Press Esc to Exit...");
        //Keypad control
        if (kbhit()) {
            ch = getch();
            if (ch == 72) {
                x = 0;
                y = -1;
            }
            if (ch == 75) {
                x = -1;
                y = 0;
            }
            if (ch == 77) {
                x = 1;
                y = 0;
            }
            if (ch == 80) {
                x = 0;
                y = 1;
            }
            if (ch == 27)
                exit(0);
        }
        i = i + x;
        j = j + y;
        a++; // Part I would like to change so that after say every 10seconds, it either goes left or right
        c++; //Part I would like to change so that after say every 10seconds, it either goes left or right

        //Making sure the circles restart when reaches the end of window.
        if (a >= getmaxx())
            a = 0;
        if (c >= getmaxx())
            c = 0;
        if (i <= 0)
            i = getmaxx() - 1;
        if (i >= getmaxx())
            i = 0;
        if (j <= 0)
            j = getmaxy() - 1;
        if (j >= getmaxy())
            j = 0;

        //Circles Overlapping Calculation//
        double T = i - a;
        double S = j - b;
        double D = sqrt(T * T + S * S);
        double sumrad = 2.5 + 2.5;
        double T2 = i - c;
        double T3 = j - d;
        double D2 = sqrt(T2 * T2 + T3 * T3);
        double sumrad2 = 5 + 5;

        //Checking if Circles overlap with previous calculation
        if (D < sumrad) {
            btm.print();
            HP = HP - btm.getenemyattack();
            cout << "Hero Life = " << HP << endl;
        }
        else if (D2 < sumrad2) {
            tp.print();
            HP = HP - tp.getenemyattack();
            cout << "Hero Life = " << HP << endl;
        }
        if (HP <= 0) {
            cout << "END ! " << endl;
            return 0;
        }

        delay(20);
        cleardevice();
    }
}
Topic archived. No new replies allowed.