Space Invaders Scorekeeper & Enemy Motion

I need help on two things: How do I create a scorekeeper to track when a "bullet" hits an "M"?
How do I make each M move downward?
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
 #include <iostream>
#include <windows.h>
void Cursor(int, int);
using namespace std;
int main() {
	char exit;
	char play;
	ShowWindow(GetConsoleWindow(), SW_MAXIMIZE);
	char map[100][100];
	int i, j; 
	int r, c; //Row and column
	int rf, cf;
	bool gameContinue;
	int bulletCounter;
	int counter;
	struct Bullets {
		bool exist = false;
		int row;
		int col;
		bool New = false;
	};
	struct Enemy {
		bool exist = false; //Structure for enemy
	};
	struct Bullets b1;
	struct Bullets b2;
	struct Bullets b3;
	struct Bullets b4;
	struct Bullets b5;
	struct Enemy M; //Structure for enemy
	gameContinue = true;
	counter = 0;
	bulletCounter = 0;
	//b1.New = false;
	b1.exist = false;
	b2.exist = false;
	b3.exist = false;
	b4.exist = false;
	b5.exist = false;
		for (i = 0; i <= 50; i++) { //Initialize board
			for (j = 0; j <= 50; j++) {
				map[i][j] = '±';
			}
		}
		for (i = 1; i <= 49; i++) { //Initialize board
			for (j = 1; j <= 49; j++) {
				map[i][j] = ' ';
			}
		}
		r = 45;				//Place A
		c = 24;
		map[r][c] = 'A';
		//Place Enemy Location
			for (i = 1; i <= 12; i++) {
			rf = rand() % 8 + 2;
			cf = rand() % 47 + 2;
			map[rf][cf] = 'M';
		}
		for (i = 0; i <= 50; i++) {
			for (j = 0; j <= 50; j++) { //Print board
				cout << map[i][j];
				
				
			}
			cout << endl;

		}
		do{
			//Delay
			Sleep(10); // The key timing mechanism
			counter++; //Tracks memory usage
			//Left movement
			if (GetAsyncKeyState(VK_LEFT) && map[r][c - 1] != '±' && (counter % 4) == 0) {
				Cursor(r, c);
				cout << " ";
				map[r][c] = ' ';
				c--;
				map[r][c] = 'A';
				Cursor(r, c);
				cout << "A";
			}//Reprint the map		

			 //Right movement
			if (GetAsyncKeyState(VK_RIGHT) && map[r][c + 1] != '±' && (counter % 4) == 0) {
				Cursor(r, c);
				cout << " ";
				map[r][c] = ' ';
				c++;
				map[r][c] = 'A';
				Cursor(r, c);
				cout << "A";
			} 
			if(GetAsyncKeyState(VK_DOWN) && map[r+1][c] != '±' && (counter % 4) == 0) {
				Cursor(r, c);
				cout << " ";
				map[r][c] = ' ';
				r++;
				map[r][c] = 'A';
				Cursor(r, c);
				cout << "A";

			}
			if(GetAsyncKeyState(VK_UP) && map[r-1][c] != '±' && (counter % 4) == 0) {
				Cursor(r, c);
				cout << " ";
				map[r][c] = ' ';
				r--;
				map[r][c] = 'A';
				Cursor(r, c);
				cout << "A";
			} //Bullets

			
			if (GetAsyncKeyState(VK_SPACE) && b1.exist == false && (counter % 20) == 0) {
				b1.exist = true;
				//1.New = true;
				b1.row = r - 1;				//Allow memory in printing of bullet
				b1.col = c;
				map[b1.row][b1.col] = '|';
				Cursor(b1.row, b1.col);
				cout << "|";
			}
			else if (GetAsyncKeyState(VK_SPACE) && b2.exist == false) {
				b2.exist = true;
				//1.New = true;
				b2.row = r - 1;				//Allow memory in printing of bullet
				b2.col = c;
				map[b2.row][b2.col] = '|';
				Cursor(b2.row, b2.col);
				cout << "|";

			}
			else if (GetAsyncKeyState(VK_SPACE) && b3.exist == false) {
				b3.exist = true;
				//1.New = true;
				b3.row = r - 1;				//Allow memory in printing of bullet
				b3.col = c;
				map[b3.row][b3.col] = '|';
				Cursor(b3.row, b3.col);
				cout << "|";

			}
			else if (GetAsyncKeyState(VK_SPACE) && b4.exist == false) {
				b4.exist = true;
				//1.New = true;
				b4.row = r - 1;				//Allow memory in printing of bullet
				b4.col = c;
				map[b4.row][b4.col] = '|';
				Cursor(b4.row, b4.col);
				cout << "|";

			}
			else if (GetAsyncKeyState(VK_SPACE) && b5.exist == false) {
				b5.exist = true;
				//1.New = true;
				b5.row = r - 1;				//Allow memory in printing of bullet
				b5.col = c;
				map[b5.row][b5.col] = '|';
				Cursor(b5.row, b5.col);
				cout << "|";

			}
			//Bullet 1
			if (b1.exist == true && map[b1.row - 1][b1.col] == '±') {
				b1.exist = false;
				cout << " ";
				map[b1.row][b1.col] = ' ';
				Cursor(b1.row, b1.col);
				cout << " ";
			}
			else if (b1.exist == true) {

				map[b1.row][b1.col] = ' ';
				Cursor(b1.row, b1.col);
				cout << " ";
				b1.row--;
				map[b1.row][b1.col] = '|';
				Cursor(b1.row, b1.col);
				cout << "|";
			}
			//Bullet 2
			if (b2.exist == true && map[b2.row - 1][b2.col] == '±') {
				b2.exist = false;
				cout << " ";
				map[b2.row][b2.col] = ' ';
				Cursor(b2.row, b2.col);
				cout << " ";
			}
			else if (b2.exist == true) {
				map[b2.row][b2.col] = ' ';
				Cursor(b2.row, b2.col);
				cout << " ";
				b2.row--;
				map[b2.row][b2.col] = '|';
				Cursor(b2.row, b2.col);
				cout << "|";

			}
			//Bullet 3
			if (b3.exist == true && map[b3.row - 1][b3.col] == '±') {
				b3.exist = false;
				cout << " ";
				map[b3.row][b3.col] = ' ';
				Cursor(b3.row, b3.col);
				cout << " ";
			}
			else if (b3.exist == true) {

				map[b3.row][b3.col] = ' ';
				Cursor(b3.row, b3.col);
				cout << " ";
				b3.row--;
				map[b3.row][b3.col] = '|';
				Cursor(b3.row, b3.col);
				cout << "|";

			}
			if (b4.exist == true && map[b4.row - 1][b3.col] == '±') {
				b4.exist = false;
				cout << " ";
				map[b4.row][b4.col] = ' ';
				Cursor(b4.row, b4.col);
				cout << " ";
			}
			else if (b4.exist == true) {

				map[b4.row][b4.col] = ' ';
				Cursor(b4.row, b4.col);
				cout << " ";
				b4.row--;
				map[b4.row][b4.col] = '|';
				Cursor(b4.row, b4.col);
				cout << "|";

			}
			if (b5.exist == true && map[b5.row - 1][b5.col] == '±') 
				b5.exist = false;
				cout << " ";
				map[b5.row][b5.col] = ' ';
				Cursor(b5.row, b5.col);
				cout << " ";
			}
			else if (b5.exist == true) {

				map[b5.row][b5.col] = ' ';
				Cursor(b5.row, b5.col);
				cout << " ";
				b5.row--;
				map[b5.row][b5.col] = '|';
				Cursor(b5.row, b5.col);
				cout << "|";

			} //Destroy Enemy
			
			//Move cursor away from the game map
			Cursor(51, 30);
			cout << " ";
			Cursor(20, 55); //Tracks memory usage
			cout << counter;
			Cursor(51, 30);
		
			if (b1.exist == false && M.exist == false) { //Most relevant part
				bulletCounter++; //Gives off readings other than score
				Cursor(52, 30);
				cout << "Score: ";
				
				Cursor(23, 55);
				cout << bulletCounter;
				Cursor(52, 30);
			}
			if (counter == 1000000) {
				counter = 1;
			}
		}while (gameContinue = true);
cout << "\nPlease press a key and <enter> to exit.\n";
cin >> exit;
return 0;
}
Last edited on
This code might be the problem:
(Line 25)
1
2
3
4
5
6
7
8
{
struct Bullets b1;
struct Bullets b2;
struct Bullets b3;
struct Bullets b4;
struct Bullets b5;
struct Enemy M; //Structure for enemy
}

Try this without the 'struct' keyword in front. Now you aren't creating struct objects.

Also, instead of defining 5 different bullets and handling them all at a different time, why not create an array of bullets and loop through them?
Something like this:

1
2
3
4
5
6
7
8
9
10
11
const int BULLET_COUNT = 5;
Bullets bullets[BULLET_COUNT];
for (int i = 0; i < BULLET_COUNT; ++i)
{
    bullets[i] = //define your bullets here
}
for (int i = 0; i < BULLET_COUNT; ++i)
{
   //handle all bullets here, using bullet[i] to access the current bullet
}


Hope this helps
Last edited on
Topic archived. No new replies allowed.