problem with my code

hey, i program in c++ for a month (ye im newbie :D)
and in SFML lib for 1.5 weeks.

i need help in my code:

i made an arr to draw my game map ( i build pacman)

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
	int x, y;
	int check;

	enum direction { DOWN, LEFT, RIGHT, UP };

	sf::Texture bar, upbar, warrior;
	sf::Sprite  sbar, sbar2, sbar3, sbar4, swarrior;

	if (!bar.loadFromFile("gamefiles//Background//Sidebar.png"))
		std::cout << "darkbar could not be loaded" << std::endl;
	if (!upbar.loadFromFile("gamefiles//Background//Upbar.png"))
		std::cout << "darkbar could not be loaded" << std::endl;
	if (!warrior.loadFromFile("gamefiles//Character//warrior.png"))
		std::cout << "character couldnt be load up";

	swarrior.setTexture(warrior);swarrior.setPosition(140, 140);
	sbar3.setTexture(upbar); sbar3.setPosition(0, 0);
	sbar4.setTexture(upbar); sbar4.setPosition(0, 700);
	sbar.setTexture(bar); sbar.setPosition(0, 0);
	sbar2.setTexture(bar); sbar2.setPosition(700, 0);



	sf::Vector2f source(1, DOWN);
	float framecounter = 0, switchframe = 100, framespeed = 700;
	sf::Clock frames, clock;
	sf::Time time;
	sf::Sprite block[18];
	sf::Texture map;

	sf::RectangleShape deletingdown(sf::Vector2f(32, 10)), deletingright(sf::Vector2f(10, 32)), deletingleft(sf::Vector2f(10, 32)), deletingup(sf::Vector2f(32, 10));

	deletingdown.setFillColor(sf::Color::Black);
	deletingright.setFillColor(sf::Color::Black);
	deletingleft.setFillColor(sf::Color::Black);
	deletingup.setFillColor(sf::Color::Black);

	map.loadFromFile("gamefiles//map//mapblocks.png");
	for (int load = 0; load <= 17; load++)
	{
		block[load].setTexture(map);
		block[load].setColor(sf::Color(red, green, blue));
		/* crazy mod	block[load].setColor(sf::Color(red = rand() % 256, blue = rand() % 256, green = rand() % 256)); */

	}
	std::cout << color;
	block[0].setTextureRect(sf::IntRect(0, 0, 40, 40));
	block[1].setTextureRect(sf::IntRect(41, 0, 40, 40));
	block[2].setTextureRect(sf::IntRect(0, 41, 40, 40));
	block[3].setTextureRect(sf::IntRect(41, 41, 40, 40));
	block[4].setTextureRect(sf::IntRect(82, 41, 40, 40));
	block[5].setTextureRect(sf::IntRect(123, 41, 40, 40));
	block[6].setTextureRect(sf::IntRect(123, 0, 40, 40));
	block[7].setTextureRect(sf::IntRect(41, 82, 40, 40));
	block[8].setTextureRect(sf::IntRect(0, 82, 40, 40));
	block[9].setTextureRect(sf::IntRect(82, 82, 40, 40));
	block[10].setTextureRect(sf::IntRect(82, 0, 40, 40));
	block[11].setTextureRect(sf::IntRect(123, 82, 40, 40));
	block[12].setTextureRect(sf::IntRect(164, 0, 40, 40));
	block[13].setTextureRect(sf::IntRect(164, 41, 40, 40));
	block[14].setTextureRect(sf::IntRect(164, 82, 40, 40));
	block[15].setTextureRect(sf::IntRect(205, 82, 40, 40));
	block[16].setTextureRect(sf::IntRect(205, 41, 40, 40));
	block[17].setTextureRect(sf::IntRect(205, 0, 40, 40));


	int gamesize[15][15] =
	{ 0 , 1  , 1  , 1  , 1  , 1  , 1  , 1  , 1  , 1  , 1  , 1  , 1  , 1  , 8 ,
	  2 , 20 , 20 , 20 , 20 , 20 , 20 , 20 , 20 , 20 , 20 , 20 , 20 , 20 , 2 ,
	  2 , 20 , 3  , 20 , 4  , 20 , 3  , 20 , 0  , 6  , 20 , 5  , 6  , 20 , 2 ,
	  2 , 20 , 2  , 20 , 20 , 20 , 2  , 20 , 2  , 20 , 20 , 20 , 20 , 20 , 2 ,
	  2 , 20 , 2  , 20 , 5  , 1  , 9  , 20 , 7  , 6  , 20 , 13 , 14 , 30 , 2 ,
	  2 , 20 , 10 , 20 , 20 , 20 , 20 , 20 , 20 , 20 , 20 , 16 , 15 , 20 , 2 ,
	  2 , 20 , 20  , 3 , 20 , 0  , 1  , 1  , 1  , 8  , 20 , 2  , 20 , 20 , 2 ,
	  7 , 6  , 20 , 10 , 20 , 2  , 20 , 20 , 20 , 2  , 20 , 2  , 20 , 5  , 9 ,
	  20, 20 , 20 , 20 , 20 , 7  , 1  , 1  , 1  , 9  , 20 , 2  , 20 , 20 , 20,
	  13, 14 , 20 , 3  , 20 , 20 , 20 , 20 , 20 , 20 , 20 , 2  , 20 , 5  , 8 ,
	  16, 15 , 20 , 2  , 20 , 5  , 1  , 1  , 12 , 6  , 20 , 10 , 20 , 20 , 2 ,
	  2 , 20 , 20 , 2  , 20 , 20 , 20 , 20 , 2  , 20 , 20 , 20 , 3  , 20 , 2 ,
	  2 , 20 , 5  , 11 , 1  , 1  , 6  , 20 , 10 , 20 , 5  , 1  , 9  , 20 , 2 ,
	  2 , 20 , 20 , 20 , 20 , 20 , 20 , 20 , 20 , 20 , 20 , 20 , 20 , 20 , 2 ,
	  7 , 1  , 1  , 1  , 1  , 1  , 1  , 1  , 1  , 1  , 1  , 1  , 1  , 1  , 9 };


	double moveddown = 0,movedleft = 0,movedright = 0, movedup = 0;



	int repeat;
	sf::Vector2f positions;
	int playerx = 140, playery = 140;
	int rights = 0, lefts = 0, ups = 0, downs = 0;
	int kk = 0;
	int ax = 1, ay = 1;
	while (window.isOpen())
	{


		sf::Event Event;


		while (window.pollEvent(Event))
		{
			switch (Event.type)
			{

			case sf::Event::Closed:
				PostMessage(GetConsoleWindow(), WM_CLOSE, 0, 0);
			}
		}
		for (y = 0; y <= 14; y++)

			for (x = 0; x <= 14; x++)
			{
				if (gamesize[y][x] == 0)
				{
					block[0].setPosition(100 + (x * 40), 100 + (y * 40)); window.draw(block[0]);

				}
				if (gamesize[y][x] == 1)
				{
					block[1].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[1]);
				}
				if (gamesize[y][x] == 2)
				{
					block[2].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[2]);
				}
				if (gamesize[y][x] == 3)
				{
					block[3].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[3]);
				}
				if (gamesize[y][x] == 4)
				{
					block[4].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[4]);
				}
				if (gamesize[y][x] == 5)
				{
					block[5].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[5]);
				}
				if (gamesize[y][x] == 6)
				{
					block[6].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[6]);
				}
				if (gamesize[y][x] == 7)
				{
					block[7].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[7]);
				}
				if (gamesize[y][x] == 8)
				{
					block[8].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[8]);
				}
				if (gamesize[y][x] == 9)
				{
					block[9].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[9]);
				}
				if (gamesize[y][x] == 10)
				{
					block[10].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[10]);
				}
				if (gamesize[y][x] == 11)
				{
					block[11].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[11]);
				}
				if (gamesize[y][x] == 12)
				{
					block[12].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[12]);
				}
				if (gamesize[y][x] == 13)
				{
					block[13].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[13]);
				}
				if (gamesize[y][x] == 14)
				{
					block[14].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[14]);
				}
				if (gamesize[y][x] == 15)
				{
					block[15].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[15]);
				}
				if (gamesize[y][x] == 16)
				{
					block[16].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[16]);
				}
				if (gamesize[y][x] == 17)
				{
					block[17].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[17]);
				}

			}



		if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
		{
			source.y = DOWN;

		}
		else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
		{
			source.y = UP;
		}
		else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
		{
			source.y = LEFT;
		}
		else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
		{
			source.y = RIGHT;
		}


Last edited on
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
		framecounter += framespeed * frames.restart().asSeconds();
		if (framecounter >= switchframe)
		{
			framecounter = 0;

			if (source.y == RIGHT)
			{



				if (gamesize[ay][ax + 1] != 1 
					&& gamesize[ay][ax + 1] != 2
					&& gamesize[ay][ax + 1] != 3
					&& gamesize[ay][ax + 1] != 4
					&& gamesize[ay][ax + 1] != 5
					&& gamesize[ay][ax + 1] != 6
					&& gamesize[ay][ax + 1] != 7
					&& gamesize[ay][ax + 1] != 8
					&& gamesize[ay][ax + 1] != 9
					&& gamesize[ay][ax + 1] != 10
					&& gamesize[ay][ax + 1] != 11
					&& gamesize[ay][ax + 1] != 12
					&& gamesize[ay][ax + 1] != 13
					&& gamesize[ay][ax + 1] != 14
					&& gamesize[ay][ax + 1] != 15
					&& gamesize[ay][ax + 1] != 16
					&& gamesize[ay][ax + 1] != 17)
				{

					swarrior.setPosition(playerx += 10, playery);
					deletingright.setPosition(playerx - 10, playery);
					movedright += 0.25;


				}
				if (movedright == 1)
				{
					movedright = 0;

					if (gamesize[ay][ax + 1] != 1
						&& gamesize[ay][ax + 1] != 2
						&& gamesize[ay][ax + 1] != 3
						&& gamesize[ay][ax + 1] != 4
						&& gamesize[ay][ax + 1] != 5
						&& gamesize[ay][ax + 1] != 6
						&& gamesize[ay][ax + 1] != 7
						&& gamesize[ay][ax + 1] != 8
						&& gamesize[ay][ax + 1] != 9
						&& gamesize[ay][ax + 1] != 10
						&& gamesize[ay][ax + 1] != 11
						&& gamesize[ay][ax + 1] != 12
						&& gamesize[ay][ax + 1] != 13
						&& gamesize[ay][ax + 1] != 14
						&& gamesize[ay][ax + 1] != 15
						&& gamesize[ay][ax + 1] != 16
						&& gamesize[ay][ax + 1] != 17)
					{
						ax++;
					}
				}

				if (source.x == 1)
					source.x = 2;

				else if (source.x == 0)
					source.x = 1;

				else if (source.x == 2)
					source.x = 0;



			}
			else  if (source.y == LEFT)
			{


				
				if (gamesize[ay][ax - 1] != 1
					&& gamesize[ay][ax - 1] != 2
					&& gamesize[ay][ax - 1] != 3
					&& gamesize[ay][ax - 1] != 4
					&& gamesize[ay][ax - 1] != 5
					&& gamesize[ay][ax - 1] != 6
					&& gamesize[ay][ax - 1] != 7
					&& gamesize[ay][ax - 1] != 8
					&& gamesize[ay][ax - 1] != 9
					&& gamesize[ay][ax - 1] != 10
					&& gamesize[ay][ax - 1] != 11
					&& gamesize[ay][ax - 1] != 12
					&& gamesize[ay][ax - 1] != 13
					&& gamesize[ay][ax - 1] != 14
					&& gamesize[ay][ax - 1] != 15
					&& gamesize[ay][ax - 1] != 16
					&& gamesize[ay][ax - 1] != 17)
				{
					swarrior.setPosition(playerx -= 10, playery);
					deletingleft.setPosition(playerx + 32, playery);
					movedleft += 0.25;

				}
				

				if (movedleft == 1)
				{
					movedleft = 0;

					if (gamesize[ay][ax - 1] != 1
						&& gamesize[ay][ax - 1] != 2
						&& gamesize[ay][ax - 1] != 3
						&& gamesize[ay][ax - 1] != 4
						&& gamesize[ay][ax - 1] != 5
						&& gamesize[ay][ax - 1] != 6
						&& gamesize[ay][ax - 1] != 7
						&& gamesize[ay][ax - 1] != 8
						&& gamesize[ay][ax - 1] != 9
						&& gamesize[ay][ax - 1] != 10
						&& gamesize[ay][ax - 1] != 11
						&& gamesize[ay][ax - 1] != 12
						&& gamesize[ay][ax - 1] != 13
						&& gamesize[ay][ax - 1] != 14
						&& gamesize[ay][ax - 1] != 15
						&& gamesize[ay][ax - 1] != 16
						&& gamesize[ay][ax - 1] != 17)
					{
						ax--;
					}
				}

				if (source.x == 1)
					source.x = 2;

				else if (source.x == 0)
					source.x = 1;

				else if (source.x == 2)
					source.x = 0;
			
	

			}
			else if (source.y == UP)
			{


			
				if (gamesize[ay - 1][ax] != 1
					&& gamesize[ay - 1][ax] != 2
					&& gamesize[ay - 1][ax] != 3
					&& gamesize[ay - 1][ax] != 4
					&& gamesize[ay - 1][ax] != 5
					&& gamesize[ay - 1][ax] != 6
					&& gamesize[ay - 1][ax] != 7
					&& gamesize[ay - 1][ax] != 8
					&& gamesize[ay - 1][ax] != 9
					&& gamesize[ay - 1][ax] != 10
					&& gamesize[ay - 1][ax] != 11
					&& gamesize[ay - 1][ax] != 12
					&& gamesize[ay - 1][ax] != 13
					&& gamesize[ay - 1][ax] != 14
					&& gamesize[ay - 1][ax] != 15
					&& gamesize[ay - 1][ax] != 16
					&& gamesize[ay - 1][ax] != 17)
				{

					swarrior.setPosition(playerx, playery -= 10);
					deletingup.setPosition(playerx, playery + 32);
					movedup += 0.25;

				}
	

				if (movedup == 1)
				{
					movedup = 0;

					if (gamesize[ay - 1][ax] != 1
						&& gamesize[ay - 1][ax] != 2
						&& gamesize[ay - 1][ax] != 3
						&& gamesize[ay - 1][ax] != 4
						&& gamesize[ay - 1][ax] != 5
						&& gamesize[ay - 1][ax] != 6
						&& gamesize[ay - 1][ax] != 7
						&& gamesize[ay - 1][ax] != 8
						&& gamesize[ay - 1][ax] != 9
						&& gamesize[ay - 1][ax] != 10
						&& gamesize[ay - 1][ax] != 11
						&& gamesize[ay - 1][ax] != 12
						&& gamesize[ay - 1][ax] != 13
						&& gamesize[ay - 1][ax] != 14
						&& gamesize[ay - 1][ax] != 15
						&& gamesize[ay - 1][ax] != 16
						&& gamesize[ay - 1][ax] != 17)
					{

						ay--;
					}

				}

				if (source.x == 1)
					source.x = 2;

				else if (source.x == 0)
					source.x = 1;

				else if (source.x == 2)
					source.x = 0;



			}
			else if (source.y == DOWN)
			{

	
				
				if (gamesize[ay + 1][ax] != 1
					&& gamesize[ay + 1][ax] != 2
					&& gamesize[ay + 1][ax] != 3
					&& gamesize[ay + 1][ax] != 4
					&& gamesize[ay + 1][ax] != 5
					&& gamesize[ay + 1][ax] != 6
					&& gamesize[ay + 1][ax] != 7
					&& gamesize[ay + 1][ax] != 8
					&& gamesize[ay + 1][ax] != 9
					&& gamesize[ay + 1][ax] != 10
					&& gamesize[ay + 1][ax] != 11
					&& gamesize[ay + 1][ax] != 12
					&& gamesize[ay + 1][ax] != 13
					&& gamesize[ay + 1][ax] != 14
					&& gamesize[ay + 1][ax] != 15
					&& gamesize[ay + 1][ax] != 16
					&& gamesize[ay + 1][ax] != 17)
				{
					swarrior.setPosition(playerx, playery += 10);
					deletingdown.setPosition(playerx, playery - 10);
			
					moveddown += 0.25;

				}
				if (moveddown == 1)
				{
					moveddown = 0;

					if (gamesize[ay + 1][ax] != 1
						&& gamesize[ay + 1][ax] != 2
						&& gamesize[ay + 1][ax] != 3
						&& gamesize[ay + 1][ax] != 4
						&& gamesize[ay + 1][ax] != 5
						&& gamesize[ay + 1][ax] != 6
						&& gamesize[ay + 1][ax] != 7
						&& gamesize[ay + 1][ax] != 8
						&& gamesize[ay + 1][ax] != 9
						&& gamesize[ay + 1][ax] != 10
						&& gamesize[ay + 1][ax] != 11
						&& gamesize[ay + 1][ax] != 12
						&& gamesize[ay + 1][ax] != 13
						&& gamesize[ay + 1][ax] != 14
						&& gamesize[ay + 1][ax] != 15
						&& gamesize[ay + 1][ax] != 16
						&& gamesize[ay + 1][ax] != 17)
					{

						ay++;
					}

				}
				if (source.x == 1)
					source.x = 2;

				else if (source.x == 0)
					source.x = 1;

				else if (source.x == 2)
					source.x = 0;


			}


		}


		position(0, 0);std::cout << ay;
		position(0, 1);std::cout << ax;
		position(0, 2);std::cout << moveddown;
		position(0, 3);std::cout << movedup;
		position(0, 4);std::cout << movedleft;
		position(0, 5);std::cout << movedright;


		window.draw(deletingdown);
		window.draw(deletingright);
		window.draw(deletingleft);
		window.draw(deletingup);

		swarrior.setTextureRect(sf::IntRect(source.x * 32, source.y * 32, 32, 32));
		window.draw(swarrior);
		if (color == 10) {
			time = clock.getElapsedTime();
			kk = time.asSeconds();
			if (kk == 1)
			{
				clock.restart();
				red = rand() % 256, blue = rand() % 256, green = rand() % 256;
				for (int load = 0; load <= 17; load++)
					block[load].setColor(sf::Color(red, green, blue));
			}
		}
		window.draw(sbar);
		window.draw(sbar2);
		window.draw(sbar3);
		window.draw(sbar4);


		window.display();

im having problems with blocking my pacman characater to not go into a wall.
my pacman size (32*32 pixels)
and wall blocks (40*40)
sorry for too long messages.
if u have any tips or comment on my code please tell me so i will learn for new time
cause i want get better on coding.
plus if u have solution for my problem , to block pacman from going into walls it will help alot :D
Last edited on
You have have to check if both entities are colliding with each other and check for collision on every game update(loop).

A way you can do this would to make a function that takes both images for example Pacman(player) and the wall(enemy).
One for Entity A and another one for Entity B.

Your function can look something like this
1
2
3
4
5
6
7
bool hasCollided(const sf::Sprite& player, const sf::Sprite& enemy)
{
   if (player.getGlobalBounds().intersects(enemy.getGlobalBounds())
      return true;

    return false;
}


If you don't want Pacman to go through the wall in a move function you can check like this.
1
2
3
4
5
6
7
8
9
void movePlayer()
{
    if (hasCollided(player, enemy))
        return;
    else
    {
        // Move Pacman here...
    }
}
Last edited on
tnx for the quick respond i will try that and i hope it will work, beside that , do u see anything in my code that i can write better?
tried it and i didnt work any other suggestions?
I just gave you a function to check for collision, of course you need to implement it correctly. How did you implement yours?
i created the function
1
2
3
4
5
6
7
bool hasCollided(const sf::Sprite& player, const sf::Sprite& enemy)
{
   if (player.getGlobalBounds().intersects(enemy.getGlobalBounds())
      return true;

    return false;
}

then i went to my movement section and write
1
2
3
4
5
6
7
8
if(direction == down)
{
   if(hasCollided(swarrior,block[0]) // swarrior is my player sprite , and block[0] is specific block
         goto lable;
else
swarrior.setposition(playerx , playery + 10) // that make the player move down 10 pixels

}


and i did the same for every direction , and it just let me go through all the blocks.
and i think i know why. it because block[0] (for example) in few places every time i changed his position when i built the map
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

	int gamesize[15][15] =
	{ 0 , 1  , 1  , 1  , 1  , 1  , 1  , 1  , 1  , 1  , 1  , 1  , 1  , 1  , 8 ,
	  2 , 20 , 20 , 20 , 20 , 20 , 20 , 20 , 20 , 20 , 20 , 20 , 20 , 20 , 2 ,
	  2 , 20 , 3  , 20 , 4  , 20 , 3  , 20 , 0  , 6  , 20 , 5  , 6  , 20 , 2 ,
	  2 , 20 , 2  , 20 , 20 , 20 , 2  , 20 , 2  , 20 , 20 , 20 , 20 , 20 , 2 ,
	  2 , 20 , 2  , 20 , 5  , 1  , 9  , 20 , 7  , 6  , 20 , 13 , 14 , 30 , 2 ,
	  2 , 20 , 10 , 20 , 20 , 20 , 20 , 20 , 20 , 20 , 20 , 16 , 15 , 20 , 2 ,
	  2 , 20 , 20  , 3 , 20 , 0  , 1  , 1  , 1  , 8  , 20 , 2  , 20 , 20 , 2 ,
	  7 , 6  , 20 , 10 , 20 , 2  , 20 , 20 , 20 , 2  , 20 , 2  , 20 , 5  , 9 ,
	  20, 20 , 20 , 20 , 20 , 7  , 1  , 1  , 1  , 9  , 20 , 2  , 20 , 20 , 20,
	  13, 14 , 20 , 3  , 20 , 20 , 20 , 20 , 20 , 20 , 20 , 2  , 20 , 5  , 8 ,
	  16, 15 , 20 , 2  , 20 , 5  , 1  , 1  , 12 , 6  , 20 , 10 , 20 , 20 , 2 ,
	  2 , 20 , 20 , 2  , 20 , 20 , 20 , 20 , 2  , 20 , 20 , 20 , 3  , 20 , 2 ,
	  2 , 20 , 5  , 11 , 1  , 1  , 6  , 20 , 10 , 20 , 5  , 1  , 9  , 20 , 2 ,
	  2 , 20 , 20 , 20 , 20 , 20 , 20 , 20 , 20 , 20 , 20 , 20 , 20 , 20 , 2 ,
	  7 , 1  , 1  , 1  , 1  , 1  , 1  , 1  , 1  , 1  , 1  , 1  , 1  , 1  , 9 };


		for (y = 0; y <= 14; y++)

			for (x = 0; x <= 14; x++)
			{
				if (gamesize[y][x] == 0)
				{
					block[0].setPosition(100 + (x * 40), 100 + (y * 40)); window.draw(block[0]);

				}
				if (gamesize[y][x] == 1)
				{
					block[1].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[1]);
				}
				if (gamesize[y][x] == 2)
				{
					block[2].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[2]);
				}
				if (gamesize[y][x] == 3)
				{
					block[3].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[3]);
				}
				if (gamesize[y][x] == 4)
				{
					block[4].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[4]);
				}
				if (gamesize[y][x] == 5)
				{
					block[5].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[5]);
				}
				if (gamesize[y][x] == 6)
				{
					block[6].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[6]);
				}
				if (gamesize[y][x] == 7)
				{
					block[7].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[7]);
				}
				if (gamesize[y][x] == 8)
				{
					block[8].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[8]);
				}
				if (gamesize[y][x] == 9)
				{
					block[9].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[9]);
				}
				if (gamesize[y][x] == 10)
				{
					block[10].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[10]);
				}
				if (gamesize[y][x] == 11)
				{
					block[11].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[11]);
				}
				if (gamesize[y][x] == 12)
				{
					block[12].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[12]);
				}
				if (gamesize[y][x] == 13)
				{
					block[13].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[13]);
				}
				if (gamesize[y][x] == 14)
				{
					block[14].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[14]);
				}
				if (gamesize[y][x] == 15)
				{
					block[15].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[15]);
				}
				if (gamesize[y][x] == 16)
				{
					block[16].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[16]);
				}
				if (gamesize[y][x] == 17)
				{
					block[17].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[17]);
				}

			}

so the function only check the last time i used that certain block and not every block[0]
Last edited on
Reduce your code to a manageable, compilable example that exhibits the behavior you're describing.

Nobody's going to wade through reams of (incomplete) neophyte code.
And, by neophyte code, I mean stuff like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
				if (gamesize[ay - 1][ax] != 1
						&& gamesize[ay - 1][ax] != 2
						&& gamesize[ay - 1][ax] != 3
						&& gamesize[ay - 1][ax] != 4
						&& gamesize[ay - 1][ax] != 5
						&& gamesize[ay - 1][ax] != 6
						&& gamesize[ay - 1][ax] != 7
						&& gamesize[ay - 1][ax] != 8
						&& gamesize[ay - 1][ax] != 9
						&& gamesize[ay - 1][ax] != 10
						&& gamesize[ay - 1][ax] != 11
						&& gamesize[ay - 1][ax] != 12
						&& gamesize[ay - 1][ax] != 13
						&& gamesize[ay - 1][ax] != 14
						&& gamesize[ay - 1][ax] != 15
						&& gamesize[ay - 1][ax] != 16
						&& gamesize[ay - 1][ax] != 17)

which is equivalent to:
if (gamesize[ay-1][ax] < 1 || gamesize[ay-1][ax] > 17)

and stuff like:

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
			if (gamesize[y][x] == 0)
				{
					block[0].setPosition(100 + (x * 40), 100 + (y * 40)); window.draw(block[0]);

				}
				if (gamesize[y][x] == 1)
				{
					block[1].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[1]);
				}
				if (gamesize[y][x] == 2)
				{
					block[2].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[2]);
				}
				if (gamesize[y][x] == 3)
				{
					block[3].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[3]);
				}
				if (gamesize[y][x] == 4)
				{
					block[4].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[4]);
				}
				if (gamesize[y][x] == 5)
				{
					block[5].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[5]);
				}
				if (gamesize[y][x] == 6)
				{
					block[6].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[6]);
				}
				if (gamesize[y][x] == 7)
				{
					block[7].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[7]);
				}
				if (gamesize[y][x] == 8)
				{
					block[8].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[8]);
				}
				if (gamesize[y][x] == 9)
				{
					block[9].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[9]);
				}
				if (gamesize[y][x] == 10)
				{
					block[10].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[10]);
				}
				if (gamesize[y][x] == 11)
				{
					block[11].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[11]);
				}
				if (gamesize[y][x] == 12)
				{
					block[12].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[12]);
				}
				if (gamesize[y][x] == 13)
				{
					block[13].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[13]);
				}
				if (gamesize[y][x] == 14)
				{
					block[14].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[14]);
				}
				if (gamesize[y][x] == 15)
				{
					block[15].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[15]);
				}
				if (gamesize[y][x] == 16)
				{
					block[16].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[16]);
				}
				if (gamesize[y][x] == 17)
				{
					block[17].setPosition(100 + (x * 40), 100 + (y * 40));  window.draw(block[17]);
				}


which is equivalent to:
1
2
3
4
5
6
    int block_id = gamesize[y][x];
    if (block_id > 0 && block_id < 18)
    {
        block[block_id].setPosition(100 + (x*40), 100 + (y*40));
        window.daw(block[block_id]);
    } 
Reduce your code to a manageable, compilable example that exhibits the behavior you're describing.

Nobody's going to wade through reams of (incomplete) neophyte code.


tnx for the comment and the tip i will use that in the program and in my next program.
ty for helping me learn new coding techniques.

do u have any solution for my pacman problem?
closed account (48T7M4Gy)
ROFL
Hi,

With those code snippets cire gave, make them functions then you can call them multiple times without having to repeat that code as well.

Also, get rid of the magic numbers like 40, 100, 18 in your code. Make them const variables and refer to the variable name in your code.

And don't use goto, use loops instead. goto does have a use, just not for beginners.

Good Luck !!
Topic archived. No new replies allowed.