multiplayer space shooter problems

Write your question here.
I found a space shooter program and decided to tweak it. I tried to make it multiplayer. I can't figure out how to make player 2 (the top space ship) bullets to shoot downward. Also, I want both player 1 and 2 to be hostile to each other, while the enemies go up and down. Any help would be appreciated. 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include <iostream>
#include <conio.h>
#include <dos.h>
#include <windows.h>
#include <time.h>

#define SCREEN_WIDTH 90
#define SCREEN_HEIGHT 26
#define WIN_WIDTH 70
#define MENU_WIDTH 20
#define GAP_SIZE 7
#define Enemy_DIF 45

using namespace std;

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;

int enemyY[3];
int enemyX[3];
int enemyFlag[3];
char bird[3][5] = {' ',' ','±',' ',' ',
				   '|','±','±','±','|',
				   '±','±','±','±','±' };

char bird2[3][5] = { '±','±','±','±','±',
				     '|','±','±','±','|',
					 ' ',' ','±',' ',' ' };

int birdPos2 = WIN_WIDTH / 2;
int birdPos = WIN_WIDTH / 2;
int score = 0;
int bullets[20][4];
int bulletsLife[20];
int bIndex = 0;

void gotoxy(int x, int y) {
	CursorPosition.X = x;
	CursorPosition.Y = y;
	SetConsoleCursorPosition(console, CursorPosition);

}

void setcursor(bool visible, DWORD size) {
	if (size == 0)
		size = 20;

	CONSOLE_CURSOR_INFO lpCursor;
	lpCursor.bVisible = visible;
	lpCursor.dwSize = size;
	SetConsoleCursorInfo(console, &lpCursor);

}
void drawBorder() {
	for (int i = 0; i < SCREEN_HEIGHT+2; i++) {
		gotoxy(0, i); cout << "±±";
		gotoxy(SCREEN_WIDTH, i); cout << "±";
	}
	for (int i = 0; i < SCREEN_HEIGHT; i++) {
		gotoxy(WIN_WIDTH, i); cout << "±";
	}
	for (int i = 0; i < SCREEN_WIDTH; i++) {
		gotoxy(i, 0); cout << "±";
		gotoxy(i, SCREEN_HEIGHT); cout << "±";
		
	}	
}
void genEnemy(int ind) {
	enemyX[ind] = 3 + rand() % (WIN_WIDTH - 10);
}
void drawEnemy(int ind) {
	if (enemyFlag[ind] == true) {
		gotoxy(enemyX[ind], enemyY[ind]);	cout << ".**.";
		gotoxy(enemyX[ind], enemyY[ind]+1);	cout << "****";
		gotoxy(enemyX[ind], enemyY[ind]+2);	cout << "****";
		gotoxy(enemyX[ind], enemyY[ind]+3);	cout << ".**.";
	}
}
void eraseEnemy(int ind) {
	if (enemyFlag[ind] == true) {
		gotoxy(enemyX[ind], enemyY[ind]); cout << "    ";
		gotoxy(enemyX[ind], enemyY[ind] + 1);cout << "    ";
		gotoxy(enemyX[ind], enemyY[ind] + 2);cout << "    ";
		gotoxy(enemyX[ind], enemyY[ind] + 3);cout << "    ";
	}
}
void resetEnemy(int ind) {
	eraseEnemy(ind);
	enemyY[ind] = 4;
	genEnemy(ind);
}
void genBullet() {
	bullets[bIndex][0] = 22;
	bullets[bIndex][1] = birdPos;
	bullets[bIndex][2] = 22;
	bullets[bIndex][3] = birdPos+4;
	bIndex++;
	if (bIndex == 20)
		bIndex = 0;
}
void genBullet2() {
	bullets[bIndex][0] = 4;
	bullets[bIndex][1] = birdPos2;
	bullets[bIndex][2] = 4;
	bullets[bIndex][3] = birdPos2 + 4;
	bIndex++;
	if (bIndex == 20)
		bIndex = 0;
}

void moveBullet() {
	for (int i = 0; i < 20; i++) {
		if (bullets[i][0] > 2)
			bullets[i][0]--;
		else
			bullets[i][0] = 0;

		if (bullets[i][2] > 2)
			bullets[i][2]--;
		else
			bullets[i][2] = 0;


	}
}
void drawBullets() {
	for (int i = 0; i < 20; i++) {
		if (bullets[i][0] > 1) {
			gotoxy(bullets[i][1], bullets[i][0]); cout << ".";
			gotoxy(bullets[i][3], bullets[i][2]); cout << ".";
		}
	}
}
void eraseBullets() {
	for (int i = 0; i < 20; i++) {
		if (bullets[i][0] >= 1) {
			gotoxy(bullets[i][1], bullets[i][0]); cout << " ";
			gotoxy(bullets[i][3], bullets[i][2]); cout << " ";
		}
	}
}
void eraseBullet(int i) {
	gotoxy(bullets[i][1], bullets[i][0]); cout << " ";
	gotoxy(bullets[i][3], bullets[i][2]); cout << " ";
}
void drawBird() {
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 5; j++) {
			gotoxy(j + birdPos, i + 22); cout << bird[i][j];
		}
	}
}
void drawBird2() {
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 5; j++) {
			gotoxy(j + birdPos2, i + 2); cout << bird2[i][j];
		}
	}
}
void eraseBird() {
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 5; j++) {
			gotoxy(j + birdPos, i + 22); cout << " ";
		}
	}
}
void eraseBird2() {
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 5; j++) {
			gotoxy(j + birdPos2, i+2); cout << " ";
		}
	}
}

int collision() {
	if (enemyY[0] + 4 >= 23) {
		if (enemyX[0] + 4 - birdPos >= 0 && enemyX[0] + 4 - birdPos < 8) {
			return 1;
		}
	}
	return 0;
}
int bulletHit() {
	for (int i = 0; i < 20; i++) {
		for (int j = 0; j < 4; j += 2) {
			if (bullets[i][j] != 0) {
				if (bullets[i][j] >= enemyY[0] && bullets[i][j] <= enemyY[0] + 4) {
					if (bullets[i][j+1] >= enemyX[0] && bullets[i][j + 1] <= enemyX[0] + 4) {
						eraseBullet(i);
						bullets[i][j] = 0;
						resetEnemy(0);
						return 1;
					}
				}
				if (bullets[i][j] >= enemyY[1] && bullets[i][j] <= enemyY[1] + 4) {
					if (bullets[i][j+1] >= enemyX[1] && bullets[i][j + 1] <= enemyX[1] + 4) {
						eraseBullet(i);
						resetEnemy(1);
						bullets[i][j] = 0;
						return 1;
					}
				}
			}
		}
	}
	return 0;
}
void gameOver() {
	system("cls");
	cout << endl;
	cout << "\t\t---------------------------" << endl;
	cout << "\t\t--------GAME OVER----------" << endl;
	cout << "\t\t---------------------------" << endl << endl;
	cout << "\t\tPress any key to go back to menu.";
	_getch();
}
void updateScore() {
	gotoxy(WIN_WIDTH + 7, 5); cout << "Score: " << score << endl;
}
void instructions() {
	system("cls");
	cout << "Controls";
	cout << "\n---------------------";
	cout << "\n Player 1 controls are 'a','d', and spacebar";
	cout << "\n Plater 2 controls are 'j, 'l', and 'k'";
	cout << "\n Press spacebar or 'k to make the ships shoot";
	cout << "\n Press any key to go back to menu";
	_getch();
}
void play() {
	birdPos = -1 + WIN_WIDTH / 2;
	birdPos2 = -1 + WIN_WIDTH / 2;
	score = 0;
	enemyFlag[0] = 1;
	enemyFlag[1] = 1;
	enemyY[0] = enemyY[1] = 4;
	
	for (int i = 0; i < 20; i++) {
		bullets[i][0] = bullets[i][1] = 0;
	}

	system("cls");
	drawBorder();
	genEnemy(0);
	genEnemy(1);
	updateScore();

	gotoxy(WIN_WIDTH + 5, 2); cout << "MULTIPLAYER: ";
	gotoxy(WIN_WIDTH + 5, 3); cout << "SPACE SHOOTER ";
	gotoxy(WIN_WIDTH + 6, 4); cout << "-----------";
	gotoxy(WIN_WIDTH + 6, 6); cout << "-----------";
	gotoxy(WIN_WIDTH + 7, 12); cout << "CONTROL";
	gotoxy(WIN_WIDTH + 7, 13); cout << "Player 1: ";
	gotoxy(WIN_WIDTH + 2, 14); cout << "A Key - Left";
	gotoxy(WIN_WIDTH + 2, 15); cout << "D Key - Right";
	gotoxy(WIN_WIDTH + 2, 16); cout << "Spacebar = shoot";
	gotoxy(WIN_WIDTH + 7, 18); cout << "Player 2: ";
	gotoxy(WIN_WIDTH + 2, 19); cout << "J Key - Left";
	gotoxy(WIN_WIDTH + 2, 20); cout << "L Key - Right";
	gotoxy(WIN_WIDTH + 2, 21); cout << "K = shoot";

	gotoxy(WIN_WIDTH + 7, 8); cout << "Press any";
	gotoxy(WIN_WIDTH + 6, 9); cout << "key to start";
	_getch();
	gotoxy(WIN_WIDTH + 10, 8); cout << "                      ";

	while (1) {
		if (_kbhit()) {
			char ch = _getch();
			if (ch == 'a' || ch == 'A') {
				if (birdPos > 2)
					birdPos -= 2;
			}
			if (ch == 'd' || ch == 'D') {
				if (birdPos < WIN_WIDTH-7)
					birdPos += 2;
			}
			if (ch == 'j' || ch == 'J') {
				if (birdPos2 > 2)
					birdPos2 -= 2;
			}
			if (ch == 'l' || ch == 'L') {
				if (birdPos2 < WIN_WIDTH - 7)
					birdPos2 += 2;
			}
			if (ch == 32) {
				genBullet();
			}
			if (ch == 107 || ch == 75) {
				genBullet2();
			}	
			if (ch == 27) {
				break;
			}
		}

		drawBird();
		drawBird2();
		drawEnemy(0);
		drawEnemy(1);
		drawBullets();

		if (collision() == 1) {
			gameOver();
			return;
		}
		if (bulletHit() == 1) {
			score++;
			updateScore();
		}
		Sleep(200);
		eraseBird();
		eraseBird2();
		eraseEnemy(0);
		eraseEnemy(1);
		eraseBullets();
		moveBullet();

		if (enemyFlag[0] == 1)
			enemyY[0] += 1;
		
		if (enemyFlag[1] == 1)
			enemyY[1] += 1;

		if (enemyY[0] > SCREEN_HEIGHT - 5) {
			resetEnemy(0);
		}
		if (enemyY[1] > SCREEN_HEIGHT - 5) {
			resetEnemy(1);
		}
		if (enemyFlag[0] == 1)
			enemyY[0] += 1;

		if (enemyFlag[1] == 1)
			enemyY[1] += 1;

		if (enemyY[0] > SCREEN_HEIGHT - 5) {
			resetEnemy(0);
		}
		if (enemyY[1] > SCREEN_HEIGHT - 5) {
			resetEnemy(1);
		}
	}

}

int main() {
	setcursor(0, 0);
	srand((unsigned)time(NULL));

	do {
		system("cls");
		gotoxy(10, 5); cout << "-----------------------------";
		gotoxy(10, 6); cout << "|     SPACE SHOOTER         |";
		gotoxy(10, 7); cout << "-----------------------------";
		gotoxy(10, 9); cout << "1. Start Game";
		gotoxy(10, 10); cout << "2. Instructions";
		gotoxy(10, 11); cout << "3. Quit";
		gotoxy(10, 13); cout << "Select option: ";
		char op = _getch();

		if (op == '1') play();
		else if (op == '2') instructions();
		else if (op == '3') exit(0);


	}while (1);
	

	return 0;
}
> I found a space shooter program and decided to tweak it. I tried to make it multiplayer.
What you should have done first is tweak the obvious 'C' program into an actual C++ program with a class or three.

Copy/pasting a whole load of functions by just adding a '2' on the end of every symbol in sight is not how you do it.

1
2
3
class Player {
    Player(char left, char right, char shoot, directions direction);
};


So your initial refactor for one player would be
1
2
3
4
5
6
7
8
9
10
enum directions {
  UP,
  // to be added DOWN,
};
int main ( ) {
   Player one('A','D',' ',UP);
   Game game;
   game.addPlayer(one);
   game.play();
}


Two player could be as simple as this, if you'd got everything else right.
1
2
3
4
5
6
7
8
9
10
11
enum directions {
  UP,
  DOWN,
};
int main ( ) {
   Player one('A','D',' ',UP);
   Player two('J','L','L',DOWN);
   Game game;
   game.addPlayer(one);
   game.play();
}

Thank you I now have a better grasp at OOP and yeah lol you brought up a good point.
Topic archived. No new replies allowed.