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
| #pragma once
#include "stdafx.h"
#include "map_for_field.h"
#include "view.h"
#include <sstream>
using namespace sf;
////////////////////////////////////////////////////*Ћј—— »√–ќ*ј////////////////////////
class Playerr {
private: float byy = 0;
public:
float ww, hh, dxx, dyy, xx, yy, speedd;
int dirr, playerScoree, healthh;
bool lifee;
String Filee;
Image imagee;
Texture texturee;
Sprite spritee;
Playerr(String FF, float XX, float YY, float WW, float HH) {
dirr = 0; speedd = 0; playerScoree = 0; healthh = 100; dxx = 0; dyy = 0;
lifee = true;
Filee = FF;
ww = WW; hh = HH;
imagee.loadFromFile("images/" + Filee);
imagee.createMaskFromColor(Color(41, 33, 59));
texturee.loadFromImage(imagee);
spritee.setTexture(texturee);
xx = XX; yy = YY;
spritee.setTextureRect(IntRect(0, 0, ww, hh));
spritee.setOrigin(ww / 2, hh / 2);
}
void updatee(float timee)
{
switch (dirr)
{
case 0: dxx = speedd; dyy = 0; break;
case 1: dxx = -speedd; dyy = 0; break;
case 2: dxx = 0; dyy = speedd; break;
case 3: dxx = 0; dyy = -speedd; break;
}
xx += dxx*timee;
yy += dyy*timee;
speedd = 0;
spritee.setPosition(xx, yy);
interactionWithMapp(timee);
if (healthh <= 0) { lifee = false; }
}
float getplayercoordinateXX() {
return xx;
}
float getplayercoordinateYY() {
return yy;
}
void interactionWithMapp(int timee)
{
for (int i = yy / 32; i < (yy + hh) / 32; i++)
for (int j = xx / 32; j<(xx + ww) / 32; j++)
{
if (TileMap1[i][j] == '0' || TileMap1[i][j] == 'd')
{
if (dyy>0) //если мы шли вниз,
{
yy = i * 32 - hh;
}
if (dyy<0)
{
yy = i * 32 + 32; // аналогично с ходьбой вверх.
}
if (dxx>0)
{
xx = j * 32 - ww;//если идем вправо
}
if (dxx < 0)
{
xx = j * 32 + 32;//аналогично идем влево
}
if (dyy <= 0)
{
//y = i * 32 + 32; // аналогично с ходьбой вверх.
//x = j * 32 - w;//если идем вправо
//gh();
//std::cout << "Geted";
}
}
if (TileMap1[i][j] == 's') {
playerScoree++;
TileMap1[i][j] = ' ';
}
if (TileMap1[i][j] == 'f') {
healthh -= 40;
TileMap1[i][j] = ' ';
}
if (TileMap1[i][j] == 'h') {
healthh += 20;
TileMap1[i][j] = ' ';
}
if (TileMap1[i][j] == 'd') {
//health += 20;
TileMap1[i][j] = ' ';
}
}
}
};
class Enemy
{
public:
Sprite spriteee;
Texture textur;
String Fillee;
Image imaagee;
float www, hhh, dxxx, dyyy, xxx, yyy, speeddd;
Enemy(String FFF, float XXX, float YYY, float WWW, float HHH)
{
dxxx = 0; dyyy = 0;
Fillee = FFF;
www = WWW; hhh = HHH;
imaagee.loadFromFile("images/" + Fillee);
imaagee.createMaskFromColor(Color(41, 33, 59));
textur.loadFromImage(imaagee);
spriteee.setTexture(textur);
xxx = XXX; yyy = YYY;
spriteee.setTextureRect(IntRect(0, 0, www, hhh));
spriteee.setOrigin(www / 2, hhh / 2);
}
float getplayercoordinateXXX() {
return xxx;
}
float getplayercoordinateYYY() {
return yyy;
}
void interactionWithMapp(int timee)
{
for (int i = yyy / 23; i < (yyy + hhh) / 23; i++)
for (int j = xxx / 23; j<(xxx + www) / 23; j++)
{
if (TileMap1[i][j] == '0' || TileMap1[i][j] == 'd')
{
if (dyyy>0) //если мы шли вниз,
{
yyy = i * 23 - hhh;
}
if (dyyy<0)
{
yyy = i * 23 + 23; // аналогично с ходьбой вверх.
}
if (dxxx>0)
{
xxx = j * 23 - www;//если идем вправо
}
if (dxxx < 0)
{
xxx = j * 23 + 23;//аналогично идем влево
}
}
}
}
};
int ggh()
{
//
RenderWindow windowss(VideoMode(1366, 768, 8), "");
view.reset(FloatRect(0, 0, 640, 480));
//
Font fontt;
fontt.loadFromFile("cyrillic_old.ttf");
Text textt("", fontt, 20);
textt.setColor(Color::Black);
//
Image map_imagee;
map_imagee.loadFromFile("images/fieldd.png");
Texture mapp;
mapp.loadFromImage(map_imagee);
Sprite s_mapp;
s_mapp.setTexture(mapp);
Image door_imagee;
door_imagee.loadFromFile("images/door.png");
Texture doorr;
doorr.loadFromImage(door_imagee);
Sprite sprr;
sprr.setTexture(doorr);
sprr.setPosition(2.f, 5.f);
Image house_imagee;
house_imagee.loadFromFile("images/house.png");
Texture house;
house.loadFromImage(house_imagee);
Sprite hous;
hous.setTexture(house);
hous.setPosition(400, 10);
Image tree_image1;
tree_image1.loadFromFile("images/tree1.png");
Texture tree1;
tree1.loadFromImage(tree_image1);
IntRect recttree1(0, 0, 200, 200);
Sprite tre1(tree1, recttree1);
tre1.setPosition(350, 5);
Image tree_image3;
tree_image3.loadFromFile("images/tree.png");
Texture tree3;
tree3.loadFromImage(tree_image3);
IntRect recttree3(0, 0, 200, 200);
Sprite tre3(tree3, recttree3);
tre3.setPosition(620, 5);
Image house_imageee;
house_imageee.loadFromFile("images/house.png");
Texture housee;
housee.loadFromImage(house_imageee);
Sprite houss;
houss.setTexture(housee);
houss.setPosition(400, 600);
Image church_imagee;
church_imagee.loadFromFile("images/chourch.png");
Texture church;
church.loadFromImage(church_imagee);
Sprite churchh;
churchh.setTexture(church);
churchh.setPosition(800, 10);
Image tree_image4;
tree_image4.loadFromFile("images/tree1.png");
Texture tree4;
tree4.loadFromImage(tree_image4);
IntRect recttree4(0, 0, 200, 200);
Sprite tre4(tree4, recttree4);
tre4.setPosition(750, 10);
sf::Clock clockkk;
Image fountain_image;
fountain_image.loadFromFile("images/fountain2.png");
Texture fountain;
fountain.loadFromImage(fountain_image);
IntRect rectfountain(0, 0, 96, 95);
Sprite spritain (fountain, rectfountain);
spritain.setPosition(600, 300);
Image tree_image;
tree_image.loadFromFile("images/tree1.png");
Texture tree;
tree.loadFromImage(tree_image);
IntRect recttree(0, 0, 200, 200);
Sprite tre(tree, recttree);
tre.setPosition(400, 500);
Image tree_image2;
tree_image2.loadFromFile("images/tree1.png");
Texture tree2;
tree2.loadFromImage(tree_image2);
IntRect recttree2(0, 0, 200, 200);
Sprite tre2(tree2, recttree2);
tre2.setPosition(600, 600);
const int countPeople = 20;//count of people
Playerr pp("hero1.png", 250, 250, 32.0, 62.0);
Enemy enm("people112.png", 0, 0, 23.0, 23.0);
bool showMissionTextt = true;
float currentFramee = 0;
Clock clockk;
Clock gameTimeClockk;
int gameTimee = 0;
int createObjectForMapTimerr = 0;//timer for random
while (windowss.isOpen())
{
float timee = clockk.getElapsedTime().asMicroseconds();
float timeenm = clockkk.getElapsedTime().asMicroseconds();
if (pp.lifee) gameTimee = gameTimeClockk.getElapsedTime().asSeconds(); else { view.move(0.8, 0); }
clockk.restart();
timee = timee / 800;
Event event;
while (windowss.pollEvent(event))
{
if (event.type == sf::Event::Closed)
windowss.close();
}
/////////////////////////////////////////////////////fountain///////////////////////////////////////////////////////////////////////////////////////////
if (clockkk.getElapsedTime().asSeconds() > 80.0f)
{
if (rectfountain.left == 96)
rectfountain.left = 0;
else
rectfountain.left += 95;
spritain.setTextureRect(rectfountain);
clockkk.restart();
}
////////////////////////////////////////////////////////////people////////////////////////////////////////////////////////////////////////////////////
if (clockkk.getElapsedTime().asSeconds() > 10.0f)
{
currentFramee += 0.005*timee;
if (currentFramee > 3) currentFramee -= 3;
if (currentFramee > 3) currentFramee -= 3;
enm.spriteee.setTextureRect(IntRect(23 * int(currentFramee), 0, 23, 23));
clockkk.restart();
}
enm.spriteee.move(1.f, 1.f);
enm.spriteee.move(1.f, 0.f);
enm.spriteee.move(1.f, 1.f);
enm.spriteee.move(0.f, 1.f);
///////////////////////////////////////////”правление персонажем с анимацией////////////////////////////////////////////////////////////////////////
if (pp.lifee) {
if (Keyboard::isKeyPressed(Keyboard::Left)) {
pp.dirr = 1; pp.speedd = 0.1;
currentFramee += 0.005*timee;
if (currentFramee > 3) currentFramee -= 3;
pp.spritee.setTextureRect(IntRect(64 * int(currentFramee), 64, 32, 64));
}
if (Keyboard::isKeyPressed(Keyboard::Right)) {
pp.dirr = 0; pp.speedd = 0.1;
currentFramee += 0.005*timee;
if (currentFramee > 3) currentFramee -= 3;
pp.spritee.setTextureRect(IntRect(64 * int(currentFramee), 192, 32, 64));
}
if (Keyboard::isKeyPressed(Keyboard::Up)) {
pp.dirr = 3; pp.speedd = 0.1;
currentFramee += 0.005*timee;
if (currentFramee > 3) currentFramee -= 3;
pp.spritee.setTextureRect(IntRect(32 * int(currentFramee), 127, 32, 64));
}
if (Keyboard::isKeyPressed(Keyboard::Down)) {
pp.dirr = 2; pp.speedd = 0.1;
currentFramee += 0.005*timee;
if (currentFramee > 3) currentFramee -= 3;
pp.spritee.setTextureRect(IntRect(32 * int(currentFramee), 0, 32, 62));
}
getplayercoordinateforview(pp.getplayercoordinateXX(), pp.getplayercoordinateYY());
}
sf::Vector2i localPosition = Mouse::getPosition(windowss);
if (localPosition.x < 3) { view.move(-0.2*timee, 0); }
if (localPosition.x > windowss.getSize().x - 3) { view.move(0.2*timee, 0); }
if (localPosition.y > windowss.getSize().y - 3) { view.move(0, 0.2*timee); }
if (localPosition.y < 3) { view.move(0, -0.2*timee); }
createObjectForMapTimerr += timee;//наращиваем таймер
pp.updatee(timee);
windowss.setView(view);
windowss.clear();
for (int i = 0; i < HEIGHTT; i++)
for (int j = 0; j < WIDTHH; j++)
{
if (TileMap1[i][j] == ' ') s_mapp.setTextureRect(IntRect(0, 0, 32, 32));
if (TileMap1[i][j] == 's') s_mapp.setTextureRect(IntRect(32, 0, 32, 32));
if ((TileMap1[i][j] == '0')) s_mapp.setTextureRect(IntRect(64, 0, 32, 32));
if ((TileMap1[i][j] == 'f')) s_mapp.setTextureRect(IntRect(96, 0, 32, 32));
if ((TileMap1[i][j] == 'h')) s_mapp.setTextureRect(IntRect(128, 0, 32, 32));
s_mapp.setPosition(j * 32, i * 32);
windowss.draw(s_mapp);
windowss.draw(sprr);
windowss.draw(hous);
windowss.draw(houss);
windowss.draw(churchh);
windowss.draw(spritain);
windowss.draw(tre);
windowss.draw(tre1);
windowss.draw(tre2);
windowss.draw(tre3);
windowss.draw(tre4);
}
std::ostringstream playerHealthString, gameTimeString; // объ¤вили переменную здоровь¤ и времени
playerHealthString << pp.healthh; gameTimeString << gameTimee; //формируем строку
textt.setString("Health: " + playerHealthString.str() + "\nTime: " + gameTimeString.str());//задаем строку тексту и вызываем сформированную выше строку методом .str()
textt.setPosition(view.getCenter().x - 165, view.getCenter().y - 200);//задаем позицию текста, отступа¤ от центра камеры
windowss.draw(textt);//рисую этот текст
windowss.draw(pp.spritee);
windowss.draw(enm.spriteee);
windowss.display();
}
return 0;
} |