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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443 | // --------------------------- Ciekawski George - Balonik / source C++ by lanky ------------------------
#include <SFML/Graphics.hpp>
#include <iostream>
#include <sstream> // do stringstream
#include <algorithm>
#include <vector>
int lvl = 1; // która mapa
bool mi = 0; // banan
int fine = 5; // koniec gry
bool banPos = 0; // zmiana pozycji banana w lvl 4
class Player // sfml isn't optimized - only use it if you really need it xD
{
public:
sf::Sprite player;
float bottom, left, right, top;
//sf::Image image;
std::vector<std::vector<bool> > mask;
void UpdateColMask(sf::Vector2f position, sf::Texture &texture)
{
std::vector<std::vector<bool> > mask2;
sf::Image image;
image = texture.copyToImage();
for(int i = 0; i < image.getSize().x; i++)
{
std::vector<bool> tempMask2;
for(int j = 0; j < image.getSize().y; j++)
{
if(image.getPixel(i, j).a > 0)
tempMask2.push_back(true);
else
tempMask2.push_back(false);
}
mask2.push_back(tempMask2);
mask = mask2;
}
player.setTexture(texture);
player.setPosition(position);
}
Player(sf::Vector2f position, sf::Texture &texture)
{
sf::Image image;
image = texture.copyToImage();
for(int i = 0; i < image.getSize().x; i++)
{
std::vector<bool> tempMask;
for(int j = 0; j < image.getSize().y; j++)
{
if(image.getPixel(i, j).a > 0)
tempMask.push_back(true);
else
tempMask.push_back(false);
}
mask.push_back(tempMask);
}
player.setTexture(texture);
player.setPosition(position);
}
void Update()
{
bottom = player.getPosition().y + player.getTextureRect().height;
left = player.getPosition().x;
right = player.getPosition().x + player.getTextureRect().width;
top = player.getPosition().y;
}
bool Collision(Player p)
{
if(right <= p.left || left >= p.right ||
top >= p.bottom || bottom <= p.top)
{
}
else
{
float colBottom, colTop, colLeft, colRight;
colBottom = std::min(bottom, p.bottom);
colTop = std::max(top, p.top);
colLeft = std::max(left, p.left);
colRight = std::min(right, p.right);
for(int i = colTop; i < colBottom; i++)
{
for(int j = colLeft; j < colRight; j++)
{
if(mask[j - left][i - top] && p.mask[j - p.left][i - p.top])
return true;
}
}
}
return false;
}
~Player()
{
//delete Player::Update();
}
};
static inline std::string int2Str(int x) // konwersja (int to str)
{
std::stringstream type;
type << x;
return type.str();
}
int main()
{
int roro;
std::cout << "Wybierz rodzielczość ekranu:\n1. 640x480\n2. 800x600\n3. 1024x768\n4. 1280x1024\n5. 1366x768" <<
std::endl;
std::cin >> roro;
clock_t startTime = clock();
//sf::RenderWindow Window(sf::VideoMode(1280, 1024, 32), "Pixel Perfect Collision", sf::Style::Fullscreen);
sf::RenderWindow Window(sf::VideoMode(1280, 1024, 32), "Ciekawski George", sf::Style::Resize);
Window.setPosition(sf::Vector2i(0, 0));
//Window.create(sf::VideoMode(640, 480, 32), "Pixel Perfect Collision", sf::Style::Resize);
//starter(Window);
//Window.setSize(sf::Vector2u(640, 480));
if (roro == 1)
Window.setSize(sf::Vector2u(640, 480)); else //std::cout << "Wybrales: a" << std::endl;
if (roro == 2)
Window.setSize(sf::Vector2u(800, 600)); else
if (roro == 3)
Window.setSize(sf::Vector2u(1024, 768)); else
if (roro == 4)
Window.setSize(sf::Vector2u(1280, 1024)); else
if (roro == 5)
Window.setSize(sf::Vector2u(1366, 768)); else
{std::cout << "Wybrales: " << roro << std::endl;
Window.close();}
//sf::RenderWindow::setSize(Vector2i(50, 700));
sf::Font polski;
if (!polski.loadFromFile("texture/Dep.ttf")) return EXIT_FAILURE;
sf::Text napis;
napis.setFont(polski);
napis.setPosition(1280-60,40);
sf::Mouse::setPosition(sf::Vector2i(50, 700), Window); //50x700
sf::Texture pan;
pan.loadFromFile("texture/pan.png");
sf::Texture george;
george.loadFromFile("texture/georgeM.png"); // player
sf::Texture georgeS;
georgeS.loadFromFile("texture/georgeS.png");
sf::Texture mini;
mini.loadFromFile("texture/Banane.png");
sf::Texture mapa;
mapa.loadFromFile("texture/mapa1.png");
sf::Texture mapa2;
mapa2.loadFromFile("texture/mapa2.png");
sf::Texture mapa3;
mapa3.loadFromFile("texture/mapa3.png");
sf::Texture mapa4;
mapa4.loadFromFile("texture/mapa4.png");
sf::Texture koniec;
koniec.loadFromFile("koniec.jpg");
sf::Texture kula;
kula.loadFromFile("texture/kula.png");
sf::Sprite Skoniec;
Skoniec.setTexture(koniec);
Window.setMouseCursorVisible(0);
Player p1(Player(sf::Vector2f(50, 700), george)), // 50x700
p2(Player(sf::Vector2f(1, 250), pan)),
p3(Player(sf::Vector2f(0, 0), mapa)), // mapa 1, 2, 3
p4(Player(sf::Vector2f(615, 555), mini)),
p5(Player(sf::Vector2f(335, 600), kula)),
p6(Player(sf::Vector2f(590, 280), kula)),
p7(Player(sf::Vector2f(900, 750), kula));
// Player p5(Player(sf::Vector2f(0, 0), mapa2));
// Player p6(Player(sf::Vector2f(0, 0), mapa3));
// sf::View view(Window.getDefaultView());
while(Window.isOpen())
{
sf::Event Event;
while(Window.pollEvent(Event))
{
switch (Event.type)
{
case sf::Event::Closed:
Window.close();
break;
//case sf::Event::MouseButtonPressed: // zamykanie okna kliknięciem myszy
// Window.close();
// break;
}
}
Window.clear(sf::Color(0, 0, 0));
//napis.setString(int2Str(myszka.x));
if (lvl < fine) napis.setString(int2Str(double( clock() - startTime ) / (double)CLOCKS_PER_SEC));
sf::Vector2i myszka = sf::Mouse::getPosition(Window); // pobranie pozycji myszy
p1.player.setPosition(myszka.x, myszka.y);
// sf::Vector2i myszka = sf::Mouse::getPosition(Window); // pobranie pozycji myszy
if (lvl == 3) // kule
{
p5.player.move(0, 2.0f);
p6.player.move(0, -2.0f);
p7.player.move(0, 2.0f);
if (p5.player.getPosition().y > 1024) p5.player.setPosition(335, 600);
if (p6.player.getPosition().y < 0) p6.player.setPosition(590, 280);
if (p7.player.getPosition().y > 1024) p7.player.setPosition(900, 600);
} else
if (lvl == 4) // kule
{
p5.player.move(1.0f, 2.0f);
p6.player.move(0, 2.0f);
p7.player.move(-1.0f, 2.0f);
if (p5.player.getPosition().y > 900) p5.player.setPosition(750, 50);
if (p6.player.getPosition().y > 900) p6.player.setPosition(750, 50);
if (p7.player.getPosition().y > 900) p7.player.setPosition(750, 50);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) // Keyboard ######################
Window.close();
//else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
// Window.setSize(sf::Vector2u(640, 480));
//else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
// p1.player.move(0, -1.0f);
//else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
// p1.player.move(0, 1.0f);
if(p1.Collision(p4)) // player vs banan
{
if (lvl < 4)
{
p1.player.setTexture(georgeS, 1);
mi = 1;
} else
if (lvl == 4)
{
if (banPos == 0)
{
p4.player.setColor(sf::Color(255, 255, 255)); // kolor banana
p4.player.setPosition(380, 700); // banan
mi = 0;
banPos = 1;
} else
{
p1.player.setTexture(georgeS, 1);
mi = 1;
}
}
}
if(p1.Collision(p3)) // player vs mapa!
{
mi = 0;
p1.player.setTexture(george, 1);
if (lvl == 1) sf::Mouse::setPosition(sf::Vector2i(50, 750), Window);
if (lvl == 2) sf::Mouse::setPosition(sf::Vector2i(50, 50), Window);
if (lvl == 3) sf::Mouse::setPosition(sf::Vector2i(50, 750), Window);
if (lvl == 4) sf::Mouse::setPosition(sf::Vector2i(1090, 50), Window);
}
if ( ((p1.Collision(p5)) || (p1.Collision(p6)) || (p1.Collision(p7) )) )// player vs kula
{
mi = 0;
p1.player.setTexture(george, 1);
if (lvl == 1) sf::Mouse::setPosition(sf::Vector2i(50, 750), Window);
if (lvl == 2) sf::Mouse::setPosition(sf::Vector2i(50, 50), Window);
if (lvl == 3) sf::Mouse::setPosition(sf::Vector2i(50, 750), Window);
if (lvl == 4) sf::Mouse::setPosition(sf::Vector2i(1090, 50), Window);
}
if(p1.Collision(p2)) // player vs pan
{
//lvl++; // mapa
//mi = 0; // banan
if ((lvl == 1) && (mi == 1))
{
lvl++;
mi = 0;
p1.player.setTexture(george, 1); // george duży
p2.player.setPosition(110, 580); // pan
p3.UpdateColMask(sf::Vector2f(0, 0), mapa2); // mapa next
p4.player.setPosition(750, 850); // banan
} else
if ((lvl == 2) && (mi == 1))
{
lvl++;
mi = 0;
p1.player.setTexture(george, 1); // george duży
p2.player.setPosition(1100, 30); // pan
p3.UpdateColMask(sf::Vector2f(0, 0), mapa3); // mapa next
p4.player.setPosition(680, 600); // banan
} else
if ((lvl == 3) && (mi == 1))
{
lvl++;
mi = 0;
p1.player.setTexture(george, 1); // george duży
p2.player.setPosition(1, 50); // pan
p3.UpdateColMask(sf::Vector2f(0, 0), mapa4); // mapa next
p4.player.setPosition(780, 700); // banan
p4.player.setColor(sf::Color(0, 255, 0)); // kolor banana green
sf::Mouse::setPosition(sf::Vector2i(1080, 40), Window); // pos georga
p5.player.setPosition(750, 50); // kula
p6.player.setPosition(750, 50); // kula
p7.player.setPosition(750, 50); // kula
} else
if ((lvl == 4) && (mi == 1)) // koniec
{
napis.setString("Twoj wynik to: \n "+ napis.getString());
mi = 1;
lvl++;
napis.setPosition(350, 400);
napis.setScale(4.f, 4.f);
Window.draw(napis);
}
// lvl++; // mapa
// mi = 0; // banan
}
if (lvl < fine) p1.Update();
if (lvl < fine) p2.Update();
if (lvl < fine) p3.Update();
if (mi == 0) p4.Update();
if (lvl > 2)
{
p5.Update(); //kule
p6.Update();
p7.Update();
}
if (lvl < fine) Window.draw(p1.player);
if (lvl < fine) Window.draw(p2.player);
if (lvl < fine) Window.draw(p3.player);
if (mi == 0) Window.draw(p4.player); // banan
if (lvl > 2)
{
Window.draw(p5.player); //kule
Window.draw(p6.player);
Window.draw(p7.player);
}
Window.draw(napis);
Window.display();
}
return 0;
}
|