-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·613 lines (534 loc) · 19.6 KB
/
Copy pathmain.cpp
File metadata and controls
executable file
·613 lines (534 loc) · 19.6 KB
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include "view.h"
#include <iostream>
#include <sstream>
#include "mission.h"
#include "iostream"
#include "level.h"
#include <vector>
#include <list>
#include <math.h>
#include "LifeBar.h"
#include "menu.h"
using namespace sf;
class fly{
public:
float x, y;
fly(float X, float Y)
{
x = X; y = Y;
}
void update(float time)
{
y -= 0.03*time;
view.setCenter(x,y);
}
};
////////////////////////////////////Îáùèé êëàññ-ðîäèòåëü//////////////////////////
class Entity {
public:
std::vector<Object> obj;
float dx, dy, x, y, speed, moveTimer, shootTimer;
int w, h, health, damage;
bool life, isMove, playerNear, isShoot;
Texture texture;
Sprite sprite;
String name;
Entity(Image &image, String Name, float X, float Y, int W, int H){
x = X; y = Y; w = W; h = H; name = Name; moveTimer = 0;
speed = 0; health = 100; dx = 0; dy = 0;
life = true; isMove = false;
texture.loadFromImage(image);
sprite.setTexture(texture);
sprite.setOrigin(w / 2, h / 2);
}
FloatRect getRect(){
return FloatRect(x, y, w, h);
}
virtual void update(float time) = 0;//âñå ïîòîìêè ïåðåîïðåäåëÿþò ýòó ô-öèþ
};
////////////////////////////////////////////////////ÊËÀÑÑ ÈÃÐÎÊÀ////////////////////////
class Player :public Entity {
public:
enum { left, right, up, down, stay } state;
int playerScore;
bool isShoot;
bool exitFromTheMapBot, exitFromTheMapTop;
Player(Image &image, String Name, Level &lev, float X, float Y, int W, int H) :Entity(image, Name, X, Y, W, H){
playerScore = isShoot = 0; state = stay; obj = lev.GetAllObjects(); exitFromTheMapBot = 0, exitFromTheMapTop = 0;
if (name == "Player1"){
sprite.setTextureRect(IntRect(205, 220, w, h));
}
}
void control(){
if (Keyboard::isKeyPressed){
if (Keyboard::isKeyPressed(Keyboard::Left)) {
state = left; speed = 0.1;
}
if (Keyboard::isKeyPressed(Keyboard::Right)) {
state = right; speed = 0.1;
}
if ((Keyboard::isKeyPressed(Keyboard::Up))) {
state = up; speed = 0.1;
}
if (Keyboard::isKeyPressed(Keyboard::Down)) {
state = down; speed = 0.1;
}
/////âûñòðåë
if (Keyboard::isKeyPressed(Keyboard::Space)) {
isShoot = true;
}
}
}
/*void checkCollisionWithMap(float Dx, float Dy)
{
for (int i = 0; i<obj.size(); i++)
if (getRect().intersects(obj[i].rect))
{
if (obj[i].name == "solid")
{
if (Dy>0) { y = obj[i].rect.top - h; dy = 0; }
if (Dy<0) { y = obj[i].rect.top + obj[i].rect.height; dy = 0; }
if (Dx>0) { x = obj[i].rect.left - w; }
if (Dx<0) { x = obj[i].rect.left + obj[i].rect.width; }
}
}
}*/
void update(float time)
{
control();
switch (state)
{
case right: if (x < 1105) dx = speed; else dx = 0; sprite.setTextureRect(IntRect(410, 220, 90, 60)); break;
case left: if (x > 0) dx = -speed; else dx = 0; sprite.setTextureRect(IntRect(10, 220, 90, 60)); break;
case up: if (exitFromTheMapTop == false) dy = -speed; else dy = 0; sprite.setTextureRect(IntRect(205, 30, 90, 60)); break;
case down: if (exitFromTheMapBot == false) dy = speed; else dy = 0; sprite.setTextureRect(IntRect(205, 420, 90, 60)); break;
//case stay: break;
}
x += dx*time;
//checkCollisionWithMap(dx, 0);
y -= 0.03*time;
//if (life) { setPlayerCoordinateForView(600, y + 80); }
y += dy*time;
//checkCollisionWithMap(0, dy);
if ((dx == 0) && (dy == 0)) sprite.setTextureRect(IntRect(205, 220, 90, 60));
sprite.setPosition(x + w / 2, y + h / 2);
if (health <= 0){ life = false; }
if (!isMove){ speed = 0;}
}
};
class Enemy :public Entity{
public:
bool playerNear;
bool isShoot;
Enemy(Image &image, String Name, Level &lvl, float X, float Y, int W, int H) :Entity(image, Name, X, Y, W, H)
{
playerNear = false;
isShoot = false;
if (name == "tankleft")
{
sprite.setTextureRect(IntRect(0, 0, 32, 32));
health = 200;
//dx = 0.1;
}
if (name == "tankright")
{
sprite.setTextureRect(IntRect(0, 0, 32, 32));
health = 200;
//dx = 0.1;
}
if (name == "heli")
{
sprite.setTextureRect(IntRect(0, 0, 32, 64));
dx = 0.08;//èçíà÷àëüíîå óñêîðåíèå ïî Õ
health = 150;
}
if (name == "airplane")
{
sprite.setTextureRect(IntRect(0, 0, 32, 32));
dx = 0.08;//èçíà÷àëüíîå óñêîðåíèå ïî Õ
health = 100;
}
}
void update(float time)//ôóíêöèÿ îáíîâëåíèÿ ïëàòôîðìû.
{
if (name == "airplane")
{
x += dx * time;//ðåàëèçàöèÿ äâèæåíèÿ ïî ãîðèçîíòàëè
moveTimer += time;//íàðàùèâàåì òàéìåð
if (moveTimer > 4000) { dx *= -1; moveTimer = 0; }//åñëè ïðîøëî ïðèìåðíî 2 ñåê, òî ìåíÿåòñÿ íàïðàâëåíèå äâèæåíèÿ ïëàòôîðìû,à òàéìåð îáíóëÿåòñÿ
sprite.setPosition(x + w / 2, y + h / 2);//çàäàåì ïîçèöèþ ñïðàéòó
}
if (name == "heli")
{
x += dx * time;//ðåàëèçàöèÿ äâèæåíèÿ ïî ãîðèçîíòàëè
moveTimer += time;//íàðàùèâàåì òàéìåð
if (moveTimer > 4000) { dx *= -1; moveTimer = 0; }//åñëè ïðîøëî ïðèìåðíî 2 ñåê, òî ìåíÿåòñÿ íàïðàâëåíèå äâèæåíèÿ ïëàòôîðìû,à òàéìåð îáíóëÿåòñÿ
sprite.setPosition(x + w / 2, y + h / 2);//çàäàåì ïîçèöèþ ñïðàéòó
}
if (name == "tankleft")
{
sprite.setPosition(x + w / 2, y + h / 2);//çàäàåì ïîçèöèþ ñïðàéòó
}
if (name == "tankright")
{
sprite.setPosition(x + w / 2, y + h / 2);//çàäàåì ïîçèöèþ ñïðàéòó
}
}
};
class Bullet :public Entity{//êëàññ ïóëè
public:
int direction;//íàïðàâëåíèå ïóëè
int damage = 0;
Bullet(Image &image, String Name, Level &lvl, float X, float Y, int W, int H, int dir, int DAMAGE) :Entity(image, Name, X, Y, W, H){//âñ¸ òàê æå, òîëüêî âçÿëè â êîíöå ñîñòîÿíèå èãðîêà (int dir)
obj = lvl.GetObjects("solid");//èíèöèàëèçèðóåì .ïîëó÷àåì íóæíûå îáúåêòû äëÿ âçàèìîäåéñòâèÿ ïóëè ñ êàðòîé
x = X;
y = Y;
direction = dir;
damage = DAMAGE;
speed = 0.1;
w = 10;
h = 20;
if (name == "Bullet1"){
sprite.setTextureRect(IntRect(0, 1, w, h));
damage = 4;
}
if (name == "Bullet2"){
sprite.setTextureRect(IntRect(0, 29, w, h));
damage = 3;
}
if (name == "Bullet3"){
sprite.setTextureRect(IntRect(0, 53, w, h));
damage = 2;
}
if (name == "Bullet4"){
sprite.setTextureRect(IntRect(0, 74, w, h));
damage = 1;
}
if (name == "Bullet5"){
sprite.setTextureRect(IntRect(0, 96, 10, 10));
}
life = true;
//âûøå èíèöèàëèçàöèÿ â êîíñòðóêòîðå
}
void update(float time)
{
switch (direction)
{
case 0: dx = -speed; dy = 0; break;//èíòîâîå çíà÷åíèå state = left
case 1: dx = speed; dy = 0; break;//èíòîâîå çíà÷åíèå state = right
case 2: dx = 0; dy = speed; break;//èíòîâîå çíà÷åíèå state = down
case 3: dx = 0; dy = -speed; break;//èíòîâîå çíà÷åíèå state = up
}
x += dx*time;//ñàìî äâèæåíèå ïóëè ïî õ
y += dy*time;//ïî ó
if (x <= 0) x = 1;// çàäåðæêà ïóëè â ëåâîé ñòåíå, ÷òîáû ïðè ïðîñåäàíèè êàäðîâ îíà ñëó÷àéíî íå âûëåòåëà çà ïðåäåë êàðòû è íå áûëî îøèáêè
if (y <= 0) y = 1;
for (int i = 0; i < obj.size(); i++) {//ïðîõîä ïî îáúåêòàì solid
if (getRect().intersects(obj[i].rect)) //åñëè ýòîò îáúåêò ñòîëêíóëñÿ ñ ïóëåé,
{
life = false;// òî ïóëÿ óìèðàåò
}
}
sprite.setPosition(x + w / 2, y + h / 2);//çàäàåòñÿ ïîçèöèþ ïóëå
}
};
class Bonus : public Entity
{
public:
Bonus(Image &image, String Name, Level &lvl, float X, float Y, int W, int H) :Entity(image, Name, X, Y, W, H)
{
if (name == "bonus"){
sprite.setTextureRect(IntRect(0, 0, 58, 58));
}
}
void update(float time)
{
sprite.setPosition(x, y);
if (health <= 0){ life = false; }
}
};
int main()
{
RenderWindow window(VideoMode(1280, 720), "Game"); menu(window);
view.reset(FloatRect(0, 0, 1280, 720));
Level lvl;
lvl.LoadFromFile("map.tmx");
Image heroImage;
heroImage.loadFromFile("images/airplane.png");
Image heliImage;
heliImage.loadFromFile("images/heli.png");
//heliImage.createMaskFromColor(Color(255, 255, 255));
Image tankLeftImage;
tankLeftImage.loadFromFile("images/tankleft.png");
//tankLeftImage.createMaskFromColor(Color(255, 255, 255));
Image tankRightImage;
tankRightImage.loadFromFile("images/tankright.png");
//tankRightImage.createMaskFromColor(Color(255, 255, 255));
Image airplaneImage;
airplaneImage.loadFromFile("images/air.png");
airplaneImage.createMaskFromColor(Color(255, 255, 255));
Image bonusImage;
bonusImage.loadFromFile("images/bonus.png");
bonusImage.createMaskFromColor(Color(255, 255, 255));
Image healthImage;
healthImage.loadFromFile("images/health.png");
healthImage.createMaskFromColor(Color(255, 255, 255));
Texture t;
Sprite s;
t.loadFromImage(healthImage);
s.setTexture(t);
s.setTextureRect(IntRect(0, 0, 188, 56));
Image BulletImage;//èçîáðàæåíèå äëÿ ïóëè
BulletImage.loadFromFile("images/bullet.png");//çàãðóçèëè êàðòèíêó â îáúåêò èçîáðàæåíèÿ
BulletImage.createMaskFromColor(Color(0, 0, 0));//ìàñêà äëÿ ïóëè ïî ÷åðíîìó öâåòó
SoundBuffer shootBuffer1;//ñîçäà¸ì áóôåð äëÿ çâóêà
shootBuffer1.loadFromFile("Sounds/Shot.wav");//çàãðóæàåì â íåãî çâóê
Sound shot1(shootBuffer1);//ñîçäàåì çâóê è çàãðóæàåì â íåãî çâóê èç áóôåðà
SoundBuffer shootBuffer2;//ñîçäà¸ì áóôåð äëÿ çâóêà
shootBuffer2.loadFromFile("Sounds/Shot2.ogg");//çàãðóæàåì â íåãî çâóê
Sound shot2(shootBuffer2);//ñîçäàåì çâóê è çàãðóæàåì â íåãî çâóê èç áóôåðà
SoundBuffer shootBuffer3;//ñîçäà¸ì áóôåð äëÿ çâóêà
shootBuffer3.loadFromFile("Sounds/Shot3.ogg");//çàãðóæàåì â íåãî çâóê
Sound shot3(shootBuffer3);//ñîçäàåì çâóê è çàãðóæàåì â íåãî çâóê èç áóôåðà
SoundBuffer shootBuffer4;//ñîçäà¸ì áóôåð äëÿ çâóêà
shootBuffer4.loadFromFile("Sounds/Shot4.ogg");//çàãðóæàåì â íåãî çâóê
Sound shot4(shootBuffer4);//ñîçäàåì çâóê è çàãðóæàåì â íåãî çâóê èç áóôåðà
SoundBuffer shootBuffer5;//ñîçäà¸ì áóôåð äëÿ çâóêà
shootBuffer5.loadFromFile("Sounds/GameOver.ogg");//çàãðóæàåì â íåãî çâóê
Sound GameOver(shootBuffer5);//ñîçäàåì çâóê è çàãðóæàåì â íåãî çâóê èç áóôåðà
Music music;//ñîçäàåì îáúåêò ìóçûêè
music.openFromFile("Sounds/maintheme.ogg");//çàãðóæàåì ôàéë
music.setLoop(true);
music.play();//âîñïðîèçâîäèì ìóçûêó
std::list<Entity*> entities;
std::list<Entity*>::iterator it;
std::list<Entity*>::iterator it2;//âòîðîé èòåðàòîð.äëÿ âçàèìîäåéñòâèÿ ìåæäó îáúåêòàìè ñïèñêà
std::vector<Object> he = lvl.GetObjects("heli");
std::vector<Object> tl = lvl.GetObjects("tankleft");
std::vector<Object> tr = lvl.GetObjects("tankright");
std::vector<Object> ap = lvl.GetObjects("airplane");
std::vector<Object> k = lvl.GetObjects("bonus");
std::cout << "k. size " << k.size() << "\n";
for (int i = 0; i < he.size(); i++) entities.push_back(new Enemy(heliImage, "heli", lvl, he[i].rect.left, he[i].rect.top, 32, 64));
for (int i = 0; i < tl.size(); i++) entities.push_back(new Enemy(tankLeftImage, "tankleft", lvl, tl[i].rect.left, tl[i].rect.top, 32, 32));
for (int i = 0; i < tr.size(); i++) entities.push_back(new Enemy(tankRightImage, "tankright", lvl, tr[i].rect.left, tr[i].rect.top, 32, 32));
for (int i = 0; i < ap.size(); i++) entities.push_back(new Enemy(airplaneImage, "airplane", lvl, ap[i].rect.left, ap[i].rect.top, 32, 32));
for (int i = 0; i < k.size(); i++) entities.push_back(new Bonus(bonusImage, "bonus", lvl, k[i].rect.left, k[i].rect.top, 58, 58));
std::cout << "ent. size " << entities.size() << "\n";
Object player = lvl.GetObject("player");
Player p(heroImage, "Player1", lvl, player.rect.left - 20, player.rect.top, 90, 60);
fly fly(640, p.y - 250);
float start = p.y -250;
Clock clock;
float movetimer = 0, musicTimer = 8000;
int bonus_num = 0, i = 0;
Font font;//øðèôò
font.loadFromFile("CyrilicOld.ttf");//ïåðåäàåì íàøåìó øðèôòó ôàéë øðèôòà
Text text("", font, 100);//ñîçäàåì îáúåêò òåêñò. çàêèäûâàåì â îáúåêò òåêñò ñòðîêó, øðèôò, ðàçìåð øðèôòà(â ïèêñåëÿõ);//ñàì îáúåêò òåêñò (íå ñòðîêà)
text.setColor(Color::Red);//ïîêðàñèëè òåêñò â êðàñíûé. åñëè óáðàòü ýòó ñòðîêó, òî ïî óìîë÷àíèþ îí áåëûé
text.setStyle(Text::Bold);//æèðíûé òåêñò.
Text text2("", font, 20);//ñîçäàåì îáúåêò òåêñò. çàêèäûâàåì â îáúåêò òåêñò ñòðîêó, øðèôò, ðàçìåð øðèôòà(â ïèêñåëÿõ);//ñàì îáúåêò òåêñò (íå ñòðîêà)
text2.setColor(Color::Black);//ïîêðàñèëè òåêñò â êðàñíûé. åñëè óáðàòü ýòó ñòðîêó, òî ïî óìîë÷àíèþ îí áåëûé
text2.setStyle(Text::Bold);//æèðíûé òåêñò.
while (window.isOpen())
{
float time = clock.getElapsedTime().asMicroseconds();
clock.restart();
time = time / 800;
Event event;
while (window.pollEvent(event))
{
movetimer += time;
if (event.type == sf::Event::Closed)
window.close();
if ((p.isShoot == true) && (movetimer > 1800))
{
if (bonus_num == 0)
{
p.isShoot = false;
entities.push_back(new Bullet(BulletImage, "Bullet4", lvl, p.x + 53, p.y, 10, 20, 3, 1));
entities.push_back(new Bullet(BulletImage, "Bullet4", lvl, p.x + 27, p.y, 10, 20, 3, 1));
shot1.play();//èãðàåì çâóê ïóëè
movetimer = 0;
}
if (bonus_num == 1)
{
p.isShoot = false;
entities.push_back(new Bullet(BulletImage, "Bullet3", lvl, p.x + 53, p.y, 10, 20, 3, 2));
entities.push_back(new Bullet(BulletImage, "Bullet3", lvl, p.x + 27, p.y, 10, 20, 3, 2));
shot2.play();//èãðàåì çâóê ïóëè
movetimer = 0;
}
if (bonus_num == 2)
{
p.isShoot = false;
entities.push_back(new Bullet(BulletImage, "Bullet2", lvl, p.x + 53, p.y, 10, 20, 3, 3));
entities.push_back(new Bullet(BulletImage, "Bullet2", lvl, p.x + 27, p.y, 10, 20, 3, 3));
shot3.play();//èãðàåì çâóê ïóëè
movetimer = 0;
}
if (bonus_num == 3)
{
p.isShoot = false;
entities.push_back(new Bullet(BulletImage, "Bullet1", lvl, p.x + 53, p.y, 10, 20, 3, 4));
entities.push_back(new Bullet(BulletImage, "Bullet1", lvl, p.x + 27, p.y, 10, 20, 3, 4));
shot4.play();//èãðàåì çâóê ïóëè
movetimer = 0;
}
}
}
for (it = entities.begin(); it != entities.end();)//ãîâîðèì ÷òî ïðîõîäèìñÿ îò íà÷àëà äî êîíöà
{
Entity *b = *it;//äëÿ óäîáñòâà, ÷òîáû íå ïèñàòü (*it)->
b->update(time);//âûçûâàåì ô-öèþ update äëÿ âñåõ îáúåêòîâ (ïî ñóòè äëÿ òåõ, êòî æèâ)
if (b->life == false || ((b->y - p.y) > 800) || (p.health <= 0)) { it = entities.erase(it); delete b; }// åñëè ýòîò îáúåêò ìåðòâ, òî óäàëÿåì åãî
else { it++;}//è èäåì êóðñîðîì (èòåðàòîðîì) ê ñëåä îáúåêòó. òàê äåëàåì ñî âñåìè îáúåêòàìè ñïèñêà
}
for (it = entities.begin(); it != entities.end(); it++)//ïðîõîäèìñÿ ïî ýë-òàì ñïèñêà
{
//std::cout << "bonus x =" << (*it)->x << "bonus y =" << (*it)->y << "\n";
if (((*it)->name == "bonus") && (p.getRect().intersects((*it)->getRect())))
{
std::cout << "x of it " << (*it)->getRect().left << "\n";
std::cout << "y of it " << (*it)->getRect().top << "\n";
std::cout << "height of it " << (*it)->getRect().height << "\n";
std::cout << "width of it " << (*it)->getRect().width << "\n";
std::cout << "x of p " << p.getRect().left << "\n";
std::cout << "y of p " << p.getRect().top << "\n";
std::cout << "height of p " << p.getRect().height << "\n";
std::cout << "width of p " << p.getRect().width << "\n";
(*it)->life = false;
std::cout << "bonus x =" << (*it)->x << "bonus y =" << (*it)->y << "\n";
std::cout << "coords of char: " << p.x << " " << p.y << "\n";
std::cout << "bonus podobran \n";
bonus_num++;
}
if (((*it)->name == "Bullet1") || ((*it)->name == "Bullet2") || ((*it)->name == "Bullet3") || ((*it)->name == "Bullet4"))
{
for (it2 = entities.begin(); it2 != entities.end(); it2++)//ïðîõîäèìñÿ ïî ýë-òàì ñïèñêà
{
if ((*it2)->playerNear)
if ((((*it2)->name == "airplane") || ((*it2)->name == "tankright") || ((*it2)->name == "tankleft") || ((*it2)->name == "heli")) && ((*it2)->getRect().intersects((*it)->getRect())))
{
if ((*it)->name == "Bullet1")
(*it2)->health -= 80;
if ((*it)->name == "Bullet2")
(*it2)->health -= 60;
if ((*it)->name == "Bullet3")
(*it2)->health -= 40;
if ((*it)->name == "Bullet4")
(*it2)->health -= 20;
(*it)->life = false;
if ((*it2)->health <= 0)
{
(*it2)->life = false;
if ((*it2)->name == "tankleft")
p.playerScore += 200;
if ((*it2)->name == "tankright")
p.playerScore += 200;
if ((*it2)->name == "air")
p.playerScore += 100;
if ((*it2)->name == "heli")
p.playerScore += 150;
}
std::cout << "health: " << (*it2)->health << "\n";
}
}
if ((fly.y - (*it)->y) > 720) (*it)->life = false;
}
if (((*it)->name == "airplane") || ((*it)->name == "tankleft") || ((*it)->name == "tankright") || ((*it)->name == "heli"))
{
if ((p.y - (*it)->y) < 500 && (((*it)->y - p.y) < 500)) (*it)->playerNear = true; else (*it)->playerNear = false;
if ((*it)->playerNear)
{
(*it)->isShoot = true;
(*it)->shootTimer += time;
}
if ((*it)->isShoot == true && ((*it)->shootTimer > 3000))
{
(*it)->shootTimer = 0;
p.isShoot = false;
if (((*it)->name == "airplane") || ((*it)->name == "heli"))
entities.push_back(new Bullet(BulletImage, "Bullet5", lvl, (*it)->x, (*it)->y, 10, 10, 2, 1));
if ((*it)->name == "tankleft")
entities.push_back(new Bullet(BulletImage, "Bullet5", lvl, (*it)->x, (*it)->y, 10, 10, 1, 1));
if ((*it)->name == "tankright")
entities.push_back(new Bullet(BulletImage, "Bullet5", lvl, (*it)->x, (*it)->y, 10, 10, 0, 1));
}
}
if (((*it)->name == "Bullet5") && (p.getRect().intersects((*it)->getRect())))
{
(*it)->life = false;
p.health -= 20;
}
}
//std::cout << "coords of fly: " << fly.x << " " << fly.y << "\n";
if ((p.health) > 0 && (fly.y > 0))
{
fly.update(time);
if ((p.y - fly.y) > 240) p.exitFromTheMapBot = true; else p.exitFromTheMapBot = false;
if ((fly.y - p.y) > 240) p.exitFromTheMapTop = true; else p.exitFromTheMapTop = false;
p.update(time);//update èãðîêà
}
window.setView(view);
window.clear(Color(77, 83, 140));
lvl.Draw(window);
//std::cout << "ent. size " << entities.size() << "\n";
for (it = entities.begin(); it != entities.end(); it++)
{
if (((*it)->name == "air") || ((*it)->name == "tankleft") || ((*it)->name == "tankright") || ((*it)->name == "heli"))
{
if ((*it)->playerNear)
{
window.draw((*it)->sprite); i++;
}
}
else
{
window.draw((*it)->sprite); i++;
}
}
//std::cout << "narisovano: " << i << "\n";
i = 0;
window.draw(p.sprite);
if (p.health <= 0)
{
music.stop();
musicTimer += time;
if (musicTimer > 8000)
{
musicTimer = 0;
GameOver.play();
}
std::ostringstream playerHealthString; // îáúÿâèëè ïåðåìåííóþ
playerHealthString << p.health; //çàíåñëè â íåå ÷èñëî î÷êîâ, òî åñòü ôîðìèðóåì ñòðîêó
text.setString("GAME OVER");//çàäàåì ñòðîêó òåêñòó è âûçûâàåì ñôîðìèðîâàííóþ âûøå ñòðîêó ìåòîäîì .str()
text.setPosition(view.getCenter().x-200, view.getCenter().y-100);//çàäàåì ïîçèöèþ òåêñòà, îòñòóïàÿ îò öåíòðà êàìåðû
window.draw(text);//ðèñóþ ýòîò òåêñò
}
std::ostringstream playerScoreString; // îáúÿâèëè ïåðåìåííóþ
playerScoreString << p.playerScore + int(start-fly.y); //çàíåñëè â íåå ÷èñëî î÷êîâ, òî åñòü ôîðìèðóåì ñòðîêó
text2.setString("SCORE: " + playerScoreString.str());//çàäàåì ñòðîêó òåêñòó è âûçûâàåì ñôîðìèðîâàííóþ âûøå ñòðîêó ìåòîäîì .str()
text2.setPosition(fly.x - 580, fly.y - 350);//çàäàåì ïîçèöèþ òåêñòà, îòñòóïàÿ îò öåíòðà êàìåðû
window.draw(text2);//ðèñóþ ýòîò òåêñò
if (p.health >= 0)
if (p.health <= 100)
{
s.setTextureRect(IntRect(150 - (p.health / 20) * 30, 0, 150, 30));
}
if (fly.y <= 10)
{
std::ostringstream playerHealthString; // îáúÿâèëè ïåðåìåííóþ
playerHealthString << p.health; //çàíåñëè â íåå ÷èñëî î÷êîâ, òî åñòü ôîðìèðóåì ñòðîêó
text.setString("YOU WIN");//çàäàåì ñòðîêó òåêñòó è âûçûâàåì ñôîðìèðîâàííóþ âûøå ñòðîêó ìåòîäîì .str()
text.setPosition(view.getCenter().x - 200, view.getCenter().y - 100);//çàäàåì ïîçèöèþ òåêñòà, îòñòóïàÿ îò öåíòðà êàìåðû
window.draw(text);//ðèñóþ ýòîò òåêñò
}
s.setPosition(fly.x + 600 - p.health, fly.y - 350);
window.draw(s);
window.display();
}
return 0;
}