trouble in moving ball and bar at same time (brick game)

Hi
i am making a simple brick game using win32 console application. I am facing trouble in moving ball and paddle at same time. In the game ball is need to be moved every time. So function which moves the ball need to be called every time.
please suggest me something. Here is my code


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
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <conio.h>
using namespace std;

HANDLE con=GetStdHandle(STD_OUTPUT_HANDLE);
const int delay = 15000000;

void gotoxy(int x,int y)
{
	COORD Coord;
	Coord.X=x;
	Coord.Y=y;

	SetConsoleCursorPosition(con,Coord);
}

void hide_cursur(void)
{
	HANDLE hConsoleOutput;
	CONSOLE_CURSOR_INFO structCursorInfo;
	hConsoleOutput = GetStdHandle( STD_OUTPUT_HANDLE );
	GetConsoleCursorInfo( hConsoleOutput, &structCursorInfo );
	structCursorInfo.bVisible = FALSE;
	SetConsoleCursorInfo( hConsoleOutput, &structCursorInfo );
}

class BRICK
{
private:
	char  brick_element;
	int xPos;
	int yPos;

public:
	BRICK(void) {brick_element = '@';}
	void setPos(int x, int y) {xPos = x; yPos = y;}
	void print(void) {gotoxy(xPos,yPos); cout << brick_element;}
};

class BAR
{
private:
	char bar_element[5];
	int xPos;
	const int yPos;

public:
	BAR(void):yPos(24) {for(int i = 0; i < 5; i ++) bar_element[i] = '=' ; xPos = 36;}
	void setPos(int x) {xPos = x;}
	void print(void) {gotoxy(xPos,yPos); for(int i = 0; i < 5; i++) cout << bar_element[i];}
	int getX(void) { return xPos;}
};

class BALL
{
private:
	char ball_element;
	int xPos;
	int yPos;

public:
	BALL(void){ball_element = '*'; xPos = 38; yPos = 23;}
	void setPos(int x, int y) {xPos = x; yPos = y;}
	void print(void) {gotoxy(xPos,yPos); cout << ball_element;}
};

class LEVEL
{
private:
	BALL ball;
	BAR bar;
	BRICK bricks[2][20];

	//ball movements
	void move_upright(int& x, int& y)
	{
		for(;;)
		{
			ball.setPos(x,y);
			ball.print();
			for(int i = 0; i < delay; i++);
			gotoxy(x,y); cout << " ";
			x++; y--;

			char input;
			if(kbhit())
			{
				input = getch();
				if(input == 'a') move_bar(1);
				if(input == 's') move_bar(2);
			}

			if(y == 1)   move_downright(x,y);
			if(x == 77) move_upleft(x,y);
		}
	}
	void move_downright(int& x, int& y)
	{
		for(;;)
		{
			ball.setPos(x,y);
			ball.print();
			for(int i = 0; i < delay; i++);
			gotoxy(x,y); cout << " ";
			x++; y++;

			char input;
			if(kbhit())
			{
				input = getch();
				if(input == 'a') move_bar(1);
				if(input == 's') move_bar(2);
			}

			if(x == 77) move_downleft(x,y);
			if(y == 23) move_upright(x,y);
		}
	}
	void move_downleft(int& x, int& y)
	{
		for(;;)
		{
			ball.setPos(x,y);
			ball.print();
			for(int i = 0; i < delay; i++);
			gotoxy(x,y); cout << " ";
			x--; y++;
			
			char input;
			if(kbhit())
			{
				input = getch();
				if(input == 'a') move_bar(1);
				if(input == 's') move_bar(2);
			}
			
			if(x == 1) move_downright(x,y);
			if(y == 23) move_upleft(x,y);
		}
	}
	void move_upleft(int& x, int& y)
	{
		for(;;)
		{
			ball.setPos(x,y);
			ball.print();
			for(int i = 0; i < delay; i++);
			gotoxy(x,y); cout << " ";
			x--; y--;

			char input;
			if(kbhit())
			{
				input = getch();
				if(input == 'a') move_bar(1);
				if(input == 's') move_bar(2);
			}

			if(y == 1) move_downleft(x,y);
			if(x == 1) move_upright(x,y);
		}
	}

	//cursur movemnt
	void move_bar(int a)
	{
		if(a == 1)
		{
			int x = bar.getX();
			bar.setPos(x++);
			bar.print();
		}

		if(a == 2)
		{
			int x = bar.getX();
			bar.setPos(x--);
			bar.print();
		}
	}

public:
	LEVEL(void)
	{
		bar.print();
		ball.print();

		int x = 22;int y = 5;
		for(int i = 0; i < 2; i++)
		{

			for(int j = 0; j < 20; j++)
			{
				bricks[i][j].setPos(x, y);
				bricks[i][j].print();
				x += 2;
			}
			x = 22;
			y++;
		}
	}
	void run(void)
	{	
		int x = 38, y = 23;
		move_upright(x,y);
	}
};


int _tmain(int argc, _TCHAR* argv[])
{
	hide_cursur();
	LEVEL level1;
	level1.run();

	_getch();
	return 0;
}


It is not complete but i think the trouble i am in is major one relativly.
further correct me if i am doing anything which is not a good practice or against OOP.
and if someone suggest a different way to do it, will appreciated.
-regards
Last edited on
closed account (GbX36Up4)
I don't know how to use them, but don't you need to use threads to be able to have the program do two separate things at the same time?
Check this out:

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
#include <windows.h>
#include <stdio.h>
#include <conio.h>

void gotoxy(int x, int y)
{
    COORD pos = { x, y };

    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}

struct Object
{
    const char sprite;

    int  x,  y;
    int dx, dy;

    Object(char sprite_, int x_ = 0, int y_ = 0, int dx_ = 0, int dy_ = 0):
        sprite(sprite_), x(x_), y(y_), dx(dx_), dy(dy_) {}

    void UpdateAndDraw()
    {
        gotoxy(x, y);
        printf(" ");

        x += dx;
        y += dy;

        gotoxy(x, y);
        printf("%c", sprite);
    }
};

int main()
{
    Object ball ('*', 10, 10, 1, 1);
    Object bar  ('=', 40, 15);

    const int delay = 25;

    int ball_timer = 0;
    int bar_timer  = 0;

    char input;

    while (true)
    {
        // hit esc to quit
        if (GetAsyncKeyState(VK_ESCAPE) >> 15) break;

        if (ball_timer > 250)
        {
            if (ball.x == 0 || ball.x == 79) ball.dx *= -1;
            if (ball.y == 0 || ball.y == 24) ball.dy *= -1;

            ball.UpdateAndDraw();

            ball_timer = 0;
        }

        if (bar_timer > 50)
        {
            if (_kbhit())
            {
                input = _getch();

                switch (input)
                {
                    case 'w': if (bar.y >  0) bar.dy = -1; break;
                    case 'a': if (bar.x >  0) bar.dx = -1; break;
                    case 's': if (bar.y < 24) bar.dy =  1; break;
                    case 'd': if (bar.x < 79) bar.dx =  1; break;
                }
            }

            bar.UpdateAndDraw();

            bar.dx = 0;
            bar.dy = 0;

            bar_timer = 0;
        }

        ball_timer += delay;
        bar_timer += delay;

        Sleep(delay);
    }

    return 0;
}
Topic archived. No new replies allowed.