Pong game problem

so the video will explain better than I can in words,

but the problem is that on the second rebound the ball bounces of the bottom wall then passes the side wall and disappears into the abyss never to be seen again lol

not sure why this is happening I have conditions in my code to check for this situation but doesn't seem to be working

ps it's only a simple console pong game nothing too fancy or complicated


https://www.youtube.com/watch?v=6CkpBDic8AQ&feature=youtu.be



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
252
 #include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <time.h>


using namespace std;

enum edir
{

    STOP = 0,
    UP,
    DOWN,
    LEFT,
    RIGHT
};

int ballx,bally;
const int WIDTH = 60;
const int HEIGHT = 20;
int score;
int batX[5];
int batY[5];
bool gameOver;
edir dir;
edir batDir;
int number;


void setUp()
{

    ballx = WIDTH / 2;
    bally = HEIGHT / 2;
    gameOver = false;
    number = HEIGHT / 2;
    score = 0;
    for(int y = 0; y < 5; y++)
    {

        batY[y] = number;
        batX[y] = 1;
        number++;

    }
    dir = RIGHT;
    batDir = STOP;
}

void draw()
{

    system("cls");

    for(int i = 0; i < WIDTH+1; i++)
    {

        cout << "#";
    }

    cout << endl;
    int k = number;


    for(int y = 0; y < HEIGHT; y++)
    {
        bool printed = false;

        for(int x = 0; x < WIDTH; x++)
        {

            if(x == 0)
            {

                cout << "#";
            }
            if(x == WIDTH-1)
            {

                cout << "#";
            }

            if( y == bally && x == ballx)
            {

                cout << "O";
            }

            else
            {
                for(int i = 0; i < 5; i++)
                {
                    if(batY[i] == y && !printed)
                    {
                        cout << "|";
                        printed = true;
                        break;
                    }
                }
                cout << " ";
            }
        }
        cout << endl;
    }

    for(int i = 0; i < WIDTH+1; i++)
    {

        cout << "#";
    }
}

void check(){

    if(ballx == WIDTH-2)
    {
        dir = LEFT;
    }
}

void check2(){

if(bally == HEIGHT-1){

            dir = UP;
        }
    if(bally == 0)
    {

        dir = DOWN;
    }
}

void logic()
{
    switch(dir)
    {

    case UP:
        bally--;
        break;
    case DOWN:
        bally++;
        break;
    case LEFT:
        ballx--;
        break;
    case RIGHT:
        ballx++;
        break;
    }

    switch(batDir)
    {
    case UP:
        for(int i = 0; i < 5; i++)
        {
            batY[i]--;
        }
        break;
    case DOWN:
        for(int i = 0; i < 5; i++)
        {
            batY[i]++;
        }
        break;
    }
    check();
    check2();

    if(ballx == -2){
        gameOver = true;
    }
    if(batY[0] == 0 || batY[4] == HEIGHT-1){

        batDir = STOP;
    }
    for(int i = 0; i < 5; i++){

        if(batY[i] == bally && batX[i] == ballx){

            dir = RIGHT;
            score++;
        }
    }


    if(score > 1){

        int random = rand() % 2;
        if(random == 1){


            ballx++;
            check();
            check2();

            bally++;
            check();
            check2();


            ballx++;
            check();
            check2();

        }
      check2();
    }
}

void input()
{

    if(kbhit())
    {

        switch(_getch())
        {

        case 'w':
            batDir = UP;
            break;
        case 'z':
            batDir = DOWN;
            break;

        }
    }
}

int main()
{
    setUp();
    while(!gameOver)
    {

        draw();
        input();
        logic();
    }

    if(gameOver){

        cout << endl;
        cout << endl;
        cout << "GAME OVER" << endl;
    }

}
Last edited on
Really suggest using a proper debugger, so that you can step through your code frame by frame, or pause the process as soon as something goes haywire. It can be really convenient.
Also, I would avoid global variables.

You'll notice your game only starts going haywire once score is > 1.
This corresponds to your line 189 if-block.
Inside this if-block, you increment ballx twice. This is in addition to where you either increment or decrement ballx in line 137's switch. You are doing ballx++ regardless of whether or not the ball is supposed to be going up, down, left, or right. Your ball is going to have a net direction of at least +1x even if it's supposed to be going -1x.

I suggest only updating the ball's position and direction in one unified place, once a frame.
Not in 2-3 different places.

(edit: typos)
Last edited on
thanks Ganado will do

now it seems like I have created life lol my ball seems to try to escape the grid

https://www.youtube.com/watch?v=6a-YRL0BuSA

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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275


#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <time.h>


using namespace std;

enum edir
{

    STOP = 0,
    UP,
    DOWN,
    LEFT,
    RIGHT
};

int ballx,bally;
const int WIDTH = 60;
const int HEIGHT = 20;
int score;
int batX[5];
int batY[5];
bool gameOver;
edir dir;
edir batDir;
int number;


void setUp()
{

    ballx = WIDTH / 2;
    bally = HEIGHT / 2;
    gameOver = false;
    number = HEIGHT / 2;
    score = 0;
    for(int y = 0; y < 5; y++)
    {

        batY[y] = number;
        batX[y] = 1;
        number++;

    }
    dir = RIGHT;
    batDir = STOP;
}

void draw()
{

    system("cls");

    for(int i = 0; i < WIDTH+1; i++)
    {

        cout << "#";
    }

    cout << endl;
    int k = number;


    for(int y = 0; y < HEIGHT; y++)
    {
        bool printed = false;

        for(int x = 0; x < WIDTH; x++)
        {

            if(x == 0)
            {

                cout << "#";
            }
            if(x == WIDTH-1)
            {

                cout << "#";
            }

            if( y == bally && x == ballx)
            {

                cout << "O";
            }

            else
            {
                for(int i = 0; i < 5; i++)
                {
                    if(batY[i] == y && !printed)
                    {
                        cout << "|";
                        printed = true;
                        break;
                    }
                }
                cout << " ";
            }
        }
        cout << endl;
    }

    for(int i = 0; i < WIDTH+1; i++)
    {

        cout << "#";
    }
}

bool check(){

    if(ballx == WIDTH-2)
    {
        dir = LEFT;
        return true;
    }
    return false;
}

bool check2(){

if(bally == HEIGHT-1){

            dir = UP;
            return true;
        }
    if(bally == 0)
    {

        dir = DOWN;
        return true;
    }
    return false;
}

void logic()
{

    check();
    check2();
    switch(dir)
    {

    case UP:
        bally--;
        break;
    case DOWN:
        bally++;
        break;
    case LEFT:
        ballx--;
        break;
    case RIGHT:
        ballx++;
        break;
    }

    switch(batDir)
    {
    case UP:
        for(int i = 0; i < 5; i++)
        {
            batY[i]--;
        }
        break;
    case DOWN:
        for(int i = 0; i < 5; i++)
        {
            batY[i]++;
        }
        break;
    }
    check();
    check2();

    if(ballx == -2){
        gameOver = true;
    }
    if(batY[0] == 0 || batY[4] == HEIGHT-1){

        batDir = STOP;
    }
    for(int i = 0; i < 5; i++){

        if(batY[i] == bally && batX[i] == ballx){

            dir = RIGHT;
            score++;
        }
    }


    if(score > 1){

        int random = rand() % 2;
        if(random == 1){


            ballx++;
            if(check()){
                return;
            }
            if(check2()){
               return;
            }

            bally++;
            if(check()){
                return;
            }
            if(check2()){
               return;
            }


            ballx++;
            if(check()){
                return;
            }
            if(check2()){
               return;
            }

        }
      check2();
      check();
    }
}

void input()
{

    if(kbhit())
    {

        switch(_getch())
        {

        case 'w':
            batDir = UP;
            break;
        case 'z':
            batDir = DOWN;
            break;

        }
    }
}

int main()
{
    setUp();
    while(!gameOver)
    {

        draw();
        input();
        logic();
    }

    if(gameOver){

        cout << endl;
        cout << endl;
        cout << "GAME OVER" << endl;
    }

}



n line 137's switch. You are doing ballx++ regardless of whether or not the ball is supposed to be going up, down, left, or right. Your ball is going to have a net direction of at least +1x even if it's supposed to be going -1x.



very good point that is a major problem,also I am trying to use a debugger but codeblocks is not having any of it the debugger keeps failing to open
Topic archived. No new replies allowed.