-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathretroCar.cpp
More file actions
405 lines (330 loc) · 11.7 KB
/
retroCar.cpp
File metadata and controls
405 lines (330 loc) · 11.7 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
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
using namespace sf;
//Init game const
const double SCREEN_WIDTH = 800.0;
const double SCREEN_HEIGHT = 600.0;
//Road Border
const int borderLeft = 145;
const int borderRight = 655;
const int racerHeight = 70; //racer.getsize().y;
string stringscore = "";
int score = 0;
double gameSpeed = 3.0;
double racerspeed = 2.0;
//Create Main Windows
RenderWindow app(VideoMode((int)(SCREEN_WIDTH), (int)(SCREEN_HEIGHT)), "!! Car Racing !!", Style::Default);
//Creat random Number for game loop
int getRandomNumber(int a, int b);
// Create gameover screen
int gameOver();
int getRandomNumber(int a, int b)
{
static bool first = true;
if (first) // if(first != false)
{
srand((unsigned int)time(NULL)); // reset rand. no.
first = false;
}
int result = a + rand() % ((b + 1) - a - 45); // a % 5 == ( 0 to 4 )
//result = (result / 10) * 10;
return result;
}
int main()
{
//Setframerate limit
app.setFramerateLimit(60);
//Framerate independent => dt=0.16
sf::Clock clock;
float dt = 0.f;
float mul = 62.5f;
//Init game music
SoundBuffer gameSoundBuffer;
gameSoundBuffer.loadFromFile("sound/game.wav");
Sound GameSound;
GameSound.setBuffer(gameSoundBuffer);
SoundBuffer acceleration;
acceleration.loadFromFile("sound/Car interior.wav");
Sound accel;
accel.setBuffer(acceleration);
//Init font
Font myfont;
myfont.loadFromFile("font/xirod.ttf");
//Init texture
Texture background, racer, obs1, obs2, obs3, obs4, gameover;
//Load all images
if (!background.loadFromFile("cars/background.png")) return EXIT_FAILURE; //background size (840, 650)
if (!racer.loadFromFile("cars/racer.png")) return EXIT_FAILURE;
if (!obs1.loadFromFile("cars/obs1.png")) return EXIT_FAILURE;
if (!obs2.loadFromFile("cars/obs2.png")) return EXIT_FAILURE;
if (!obs3.loadFromFile("cars/obs3.png")) return EXIT_FAILURE;
if (!obs4.loadFromFile("cars/obs4.png")) return EXIT_FAILURE;
//Create sprite
Sprite Background(background), Background1(background), Racer(racer), Obs1(obs1), Obs2(obs2), Obs3(obs3), Obs4(obs4), Gameover(gameover);
//Init co-ordinates
double RacerX, RacerY, Obs1X, Obs1Y, Obs2X, Obs2Y, Obs3X, Obs3Y, Obs4X, Obs4Y;
//Init racer and enemy position
RacerX = SCREEN_WIDTH / 2;
RacerY = SCREEN_HEIGHT - racer.getSize().y * 1.2;
Obs1X = getRandomNumber(borderLeft, borderRight);
Obs2X = getRandomNumber(borderLeft, borderRight);
Obs3X = getRandomNumber(borderLeft, borderRight);
Obs4X = getRandomNumber(borderLeft, borderRight);
Obs1Y = 0.0;
Obs2Y = -100.0;
Obs3Y = -200.0;
Obs4Y = -300.0;
double BackgroundY1 = 0.0;
double BackgroundY2 = -600.0;
//Init racer position
Racer.setPosition((float)RacerX, (float)RacerY);
//Init enemy position
Obs1.setPosition((float)Obs1X, (float)Obs1Y);
Obs2.setPosition((float)Obs2X, (float)Obs2Y);
Obs3.setPosition((float)Obs3X, (float)Obs3Y);
Obs4.setPosition((float)Obs4X, (float)Obs4Y);
//Audio
GameSound.setVolume(10.f);
GameSound.play();
GameSound.setLoop(true);
accel.setVolume(20.f);
accel.play();
accel.setLoop(true);
//Update****************************************************************************
//Game loop
while (app.isOpen())
{
//Create event for input
Event event;
//Init and count score
stringscore = "SCORE : " + to_string(score);
//To update font use text class
Text text(stringscore, myfont, 20);
text.setPosition(5.f, 0.f);
text.setFillColor(Color::White);
//Dt is difference in fps
dt = clock.restart().asSeconds();
//std::cout << dt << "\n";
//Update enemy position
Obs1.setPosition((float)Obs1X, (float)Obs1Y);
Obs2.setPosition((float)Obs2X, (float)Obs2Y);
Obs3.setPosition((float)Obs3X, (float)Obs3Y);
Obs4.setPosition((float)Obs4X, (float)Obs4Y);
//Create scrolling background
Background.setPosition(0, (float)BackgroundY1);
Background1.setPosition(0, (float)BackgroundY2);
if (BackgroundY2 > 0)
{
BackgroundY1 = 0;
BackgroundY2 = BackgroundY1 - 600.0;
}
BackgroundY1 += 3.0 * dt * mul;
BackgroundY2 += 3.0 * dt * mul;
//Set Obs LOOP
if (Obs1Y > SCREEN_HEIGHT)
{
Obs1X = getRandomNumber(borderLeft, borderRight);
Obs1Y = -80.0;
score++;
}
else
{
Obs1Y = Obs1Y + (gameSpeed + 0.5f) * (double)dt * (double)mul;
}
if (Obs2Y > SCREEN_HEIGHT)
{
Obs2X = getRandomNumber(borderLeft, borderRight);
Obs2Y = -80.0;
score++;
}
else
{
Obs2Y = Obs2Y + (gameSpeed + 0.5f) * (double)dt * (double)mul;
}
if (Obs3Y > SCREEN_HEIGHT)
{
Obs3X = getRandomNumber(borderLeft, borderRight);
Obs3Y = -80.0;
score++;
}
else
{
Obs3Y = Obs3Y + (gameSpeed + 0.5f) * (double)dt * (double)mul;
}
if (Obs4Y > SCREEN_HEIGHT)
{
Obs4X = getRandomNumber(borderLeft, borderRight);
Obs4Y = -80.0;
score++;
}
else
{
Obs4Y = Obs4Y + (gameSpeed + 0.5f) * (double)dt * (double)mul;
}
//Game level
if (score > 25)
{
gameSpeed = 5.0 * (double)dt * (double)mul;
}
if (score > 50)
{
gameSpeed = 6.0 * (double)dt * (double)mul;
}
if (score > 100)
{
gameSpeed = 7.0 * (double)dt * (double)mul;
}
//Create event to handle input from keyboard
while (app.pollEvent(event))
{
if (event.type == Event::Closed)
app.close();
}
/*if (event.type == sf::Event::KeyPressed)
{
if (event.key.code == sf::Keyboard::Left)
{
if (RacerX > borderLeft) { RacerX = RacerX - 10; }
}
if (event.key.code == sf::Keyboard::Right)
{
if (RacerX < borderRight) { RacerX = RacerX + 10; }
}
if (event.key.code == sf::Keyboard::Up)
{
if (RacerY > 0) { RacerY = RacerY - 10; }
}
if (event.key.code == sf::Keyboard::Down)
{
if (RacerY < SCREEN_HEIGHT - 70) { RacerY = RacerY + 10; }
}
}*/
//Racer movement
if (Keyboard::isKeyPressed(Keyboard::W))
{
Racer.move(0.f, -(float)racerspeed * dt * mul);
BackgroundY1 += 1.5 * dt * mul;
Obs1Y = Obs1Y + 1.5 * (double)dt * (double)mul;
Obs2Y = Obs2Y + 1.5 * (double)dt * (double)mul;
Obs3Y = Obs3Y + 1.5 * (double)dt * (double)mul;
Obs4Y = Obs4Y + 1.5 * (double)dt * (double)mul;
BackgroundY2 += 1.5 * dt * mul;
}
if (Keyboard::isKeyPressed(Keyboard::S))
{
Racer.move(0.f, (float)racerspeed * dt * mul);
BackgroundY1 += -1.0 * dt * mul;
Obs1Y = Obs1Y + -1.0 * (double)dt * (double)mul;
Obs2Y = Obs2Y + -1.0 * (double)dt * (double)mul;
Obs3Y = Obs3Y + -1.0 * (double)dt * (double)mul;
Obs4Y = Obs4Y + -1.0 * (double)dt * (double)mul;
BackgroundY2 += -1.0 * dt * mul;
}
if (Keyboard::isKeyPressed(Keyboard::A))
{
Racer.move(-(float)racerspeed * dt * mul, 0.f);
}
if (Keyboard::isKeyPressed(Keyboard::D))
{
Racer.move((float)racerspeed * dt * mul, 0.f);
}
//Racer collision with track-border
if (Racer.getPosition().x <= borderLeft) //Left
Racer.setPosition(borderLeft, Racer.getPosition().y);
if (Racer.getPosition().x >= borderRight - Racer.getGlobalBounds().width) //Right
Racer.setPosition(borderRight - Racer.getGlobalBounds().width, Racer.getPosition().y);
if (Racer.getPosition().y <= 0.f) //Top
Racer.setPosition(Racer.getPosition().x, 0.f);
if (Racer.getPosition().y >= app.getSize().y - Racer.getGlobalBounds().height) //Bottom
Racer.setPosition(Racer.getPosition().x, app.getSize().y - Racer.getGlobalBounds().height);
//Collision between Racer and enemy
/*if (((RacerX >= (Obs1X - 30)) && (RacerX <= (Obs1X + 30))) && ((RacerY >= (Obs1Y - 30)) && (RacerY) <= (Obs1Y + 30)))
{
GameSound.stop(); gameOver();
};
if (((RacerX >= (Obs2X - 30)) && (RacerX <= (Obs2X + 30))) && ((RacerY >= (Obs2Y - 30)) && (RacerY) <= (Obs2Y + 30)))
{
GameSound.stop(); gameOver();
};
if (((RacerX >= (Obs3X - 30)) && (RacerX <= (Obs3X + 30))) && ((RacerY >= (Obs3Y - 30)) && (RacerY) <= (Obs3Y + 30)))
{
GameSound.stop(); gameOver();
};
if (((RacerX >= (Obs4X - 30)) && (RacerX <= (Obs4X + 30))) && ((RacerY >= (Obs4Y - 30)) && (RacerY) <= (Obs4Y + 30)))
{
GameSound.stop(); gameOver();
};*/
if (Racer.getGlobalBounds().intersects(Obs1.getGlobalBounds())
|| Racer.getGlobalBounds().intersects(Obs2.getGlobalBounds())
|| Racer.getGlobalBounds().intersects(Obs3.getGlobalBounds())
|| Racer.getGlobalBounds().intersects(Obs4.getGlobalBounds()))
{
accel.stop();
GameSound.stop();
gameOver();
}
//Draw**********************************************************************
//Clear and draw position
app.clear();
app.draw(Background);
app.draw(Background1);
app.draw(Racer);
app.draw(Obs1);
app.draw(Obs2);
app.draw(Obs3);
app.draw(Obs4);
app.draw(text);
app.display();
}
return EXIT_SUCCESS; //return 0;
}
//int startGame()
//{
//TODO
//}
//Game over
int gameOver()
{
Texture gameover, troll;
if (!gameover.loadFromFile("cars/Go1.png"))
return EXIT_FAILURE;
/*if (!troll.loadFromFile("cars/troll.png"))
return EXIT_FAILURE;*/
Sprite Gameover(gameover);
//Sprite Troll(troll);
//Troll.setPosition(90.f, 350.f);
//Troll.scale(1.1f, 1.4f);
SoundBuffer gameOver;
gameOver.loadFromFile("sound/crash.wav");
Sound GameOver;
GameOver.setBuffer(gameOver);
GameOver.setVolume(30.f);
GameOver.play();
while (app.isOpen())
{
Event event;
while (app.pollEvent(event))
{
if (event.type == Event::Closed)
app.close();
}
Font myfont;
myfont.loadFromFile("font/xirod.ttf");
//Troll.move(15.f, 7.f);
stringscore = " YOUR SCORE : " + to_string(score);
Text text(stringscore, myfont, 30);
text.setPosition(200.f, 100.f);
text.setFillColor(Color::Yellow);
app.clear();
app.draw(Gameover);
app.draw(text);
//app.draw(Troll);
app.display();
}
return 0;
}