enemy keeps leaving screen

My game right now has a random movement setup for the enemy 'i' but it keeps leaving the screen. any way to keep it in the play area? here is my code right now PLEASE respond asap.

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

using namespace std;

char Map [20][20] = {
//Game MAP
"###################",
"#                 #", 
"#        i        #", 
"#                 #", 
"#                 #", 
"#                 #", 
"#                 #", 
"#                 #", 
"#                 #", 
"#                 #", 
"#                 #", 
"#                 #", 
"#                 #", 
"#                 #", 
"#                 #", 
"#                 #", 
"#      #   #      #", 
"########   ########", 
"#        +        #", 
"###################", };

bool GameRun = true;
int GameSpeed = 100;
int BossHp = 1000;
int Damage = 20;
int RandomMove;

int main() 
{
	system("color 0a");
	while(GameRun == true)	
	{
		srand(time(0));
		system("cls");
		for (int y = 0; y<20; y++){
			
			cout << Map[y] << endl;
			
		}
		
		cout << "Boss HP: " << BossHp << endl;
		
		for (int y = 0; y<20; y++){
			for (int x = 0; x<20; x++){
				
				switch (Map[y][x]){
					//CHARICTER MOVMENT CONTROLES
					case '+':
						if(GetAsyncKeyState(0x41) != 0){
							int x2 = x-1;
							
							switch(Map[y][x2]){
								case ' ':
									Map[y][x] = ' ';
									x--;
									Map[y][x2] = '+';
							}
						}
						if (GetAsyncKeyState(0x44) != 0){
							int x2 = x+1;
							
							switch(Map[y][x2]){
								
								case ' ':
								Map[y][x] = ' ';
								x++;
								Map[y][x2] = '+';
							
								
							}
						}
						if (GetAsyncKeyState(0x57) != 0){
							int y2 = y-1;
							
							switch(Map[y2][x]){
								
								case ' ':
								Map[y][x] = ' ';
								y--;
								Map[y2][x] = '+';
							
								
							}
						}
						if (GetAsyncKeyState(0x53) != 0){
							int y2 = y+1;
							
							switch(Map[y2][x]){
								
								case ' ':
								Map[y][x] = ' ';
								y++;
								Map[y2][x] = '+';
							
								
							}
						}
						//CHARICTER SHOOTING CONTROLES
						if(GetAsyncKeyState(VK_UP) != 0){
							y--;
							Map[y][x] = '!';
						}
						if(GetAsyncKeyState(VK_DOWN) != 0){
							y++;
							Map[y][x] = '|';
						}
						if(GetAsyncKeyState(VK_LEFT) != 0){
							x--;
							Map[y][x] = '<';
						}
						if(GetAsyncKeyState(VK_RIGHT) != 0){
							x++;
							Map[y][x] = '>';
						}
						break;
///////////////////////////////////////////////////////////////////////////////
						case '!':
						Map[y][x] = ' ';
						y--;
						if(Map[y-1][x] != '#')
						{
							Map[y][x] = '!';
						}
						else if (Map[y][x] = 'i')
						{
							BossHp -= 20;
						}
						break;
///////////////////////////////////////////////////////////////////////////////////						
						case '|':
						Map[y][x] = ' ';
						y++;
						if(Map[y+1][x] != '#')
						{
							Map[y][x] = '|';
						}
						else if (Map[y][x] = 'i')
						{
							BossHp -= 20;
						}
						break;
//////////////////////////////////////////////////////////////////////////////////////						
						case '>':
						Map[y][x] = ' ';
						x++;
						if(Map[y][x+1] != '#')
						{
							Map[y][x] = '>';
						}
						else if (Map[y][x] = 'i')
						{
							BossHp -= 20;
						}
						break;
//////////////////////////////////////////////////////////////////////////////				
						case '<':
						Map[y][x] = ' ';
						x--;
						if(Map[y][x-1] != '#')
						{
							Map[y][x] = '<';
						}
						else if (Map[y][x] = 'i')
						{
							BossHp -= 20;
						}
						break;
/////////////////////////////////////////////////////////////////////////////						
						case 'i':
							
							//RANDOM MOVMENT GENARATOR
							RandomMove = rand() %4 + 1;
							
						if(RandomMove == 1){
							Map[y][x] = ' ';
							x--;
							if(Map[y][x-1])
							{
								Map[y][x] = 'i';
							}
						}
						else if(RandomMove == 2){
							Map[y][x] = ' ';
							x++;
							if(Map[y][x+1])
							{
								Map[y][x] = 'i';
							}
						}
						else if(RandomMove == 3){
							Map[y][x] = ' ';
							y--;
							if(Map[y-1][x])
							{
								Map[y][x] = 'i';
							}
						}
						else if(RandomMove == 4){
							Map[y][x] = ' ';
							y++;
							if(Map[y+1][x])
							{
								Map[y][x] = 'i';
							}
						}					
						
				}
				
			}
		}
		Sleep(GameSpeed);
	}
}
closed account (j3Rz8vqX)
Immediately after if(RandomMove == 1){ you should test to see if x or y is beyond the limit; 1 and 18; 20 being beyond size.

Also:
1
2
3
4
						else if (Map[y][x] = 'i')
						{
							BossHp -= 20;
						}

Seems to be creating bosses. You'd probably want an equality operator there, rather than a equals operator.

Tip:
Remember:
0 is a wall on the left and top.
19 is the wall on the right and bottom.
20 is the limit to the array and not displayed.

So maybe something like this:
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
						if(RandomMove == 1){
						    if(x>1){
                                Map[y][x] = ' ';
                                x--;
                                if(Map[y][x-1])
                                {
                                    Map[y][x] = 'i';
                                }
						    }
						}
						else if(RandomMove == 2){
						    if(x<20-2){
                                Map[y][x] = ' ';
                                x++;
                                if(Map[y][x+1])
                                {
                                    Map[y][x] = 'i';
                                }
						    }
						}
						else if(RandomMove == 3){
						    if(y>1){
                                Map[y][x] = ' ';
                                y--;
                                if(Map[y-1][x])
                                {
                                    Map[y][x] = 'i';
                                }
						    }
						}
						else if(RandomMove == 4){
						    if(y<20-2){
                                Map[y][x] = ' ';
                                y++;
                                if(Map[y+1][x])
                                {
                                    Map[y][x] = 'i';
                                }
						    }
						}


Have fun.
ok so what would be a way to have the 'i' take damage without having more than 1 spawn?
closed account (j3Rz8vqX)
Your algorithm isn't incorrect (intended double negative).

I am saying that lines: 133, 146, 159, and 172 are assigning spots on the map to be "i" rather than comparing to "i".

You had:
1
2
3
4
else if (Map[y][x] = 'i')
{
    BossHp -= 20;
}

Whereas you needed:

1
2
3
4
else if (Map[y][x] = = 'i')
{
    BossHp -= 20;
}

That will remove the extra bosses.
Last edited on
Registered users can post here. Sign in or register to post.