-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
246 lines (199 loc) · 6.33 KB
/
main.cpp
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
#include <SFML/Graphics.hpp>
#include<iostream>
#include<cstdlib>
#include<ctime>
#include <string>
using namespace std;
using namespace sf;
//Init game const
const double SCREEN_WIDTH=800;
const double SCREEN_HEIGH=600;
const int borderLeft=130;
const int borderRight=670;
const int racerWidth=50;
const int racerHeight=80;
string stringscore="";
int score=0;
double gameSpeed=0.3;
//Create Main Windows
RenderWindow app(VideoMode(SCREEN_WIDTH, SCREEN_HEIGH), "Car Racing");
//Creat random Number for game loop
int getRandomNumber(int a, int b);
// Create gameover screen
int gameOver();
int win();
int main()
{
//Init font
Font myfont;
myfont.loadFromFile("font/xirod.ttf");
Texture background, racer, obs1, obs2, obs3, obs4,gameover,wins;
//Load all images
background.loadFromFile("images/background.png");
racer.loadFromFile("images/racer.png");
obs1.loadFromFile("images/car1.png");
obs2.loadFromFile("images/car2.png");
obs3.loadFromFile("images/car3.png");
obs4.loadFromFile("images/platform.png");
//Create sprite
Sprite Background(background),Background1(background),Racer(racer),Obs1(obs1),Obs2(obs2),Obs3(obs3),Obs4(obs4),Gameover(gameover),Wins(wins);
double RacerX, RacerY,Obs1X, Obs1Y,Obs2X, Obs2Y,Obs3X, Obs3Y,Obs4X, Obs4Y;
//Set racer and Obs pos
RacerX=SCREEN_WIDTH/2;
RacerY=SCREEN_HEIGH-racerHeight;
Obs1X=getRandomNumber(borderLeft,borderRight);
Obs2X=getRandomNumber(borderLeft,borderRight);
Obs3X=getRandomNumber(borderLeft,borderRight);
Obs4X=getRandomNumber(borderLeft,borderRight);
Obs1Y=0,Obs2Y=-100,Obs3Y=-200,Obs4Y=-300;
double BackgroundY1=0;
double BackgroundY2=-600;
//GAME LOOP
while (app.isOpen())
{
//Init and count score
stringscore="SCORE: "+to_string(score);
Text text(stringscore, myfont, 15);
text.setPosition(5,0);
//Set car position
Racer.setPosition(RacerX,RacerY);
Obs1.setPosition(Obs1X,Obs1Y);
Obs2.setPosition(Obs2X,Obs2Y);
Obs3.setPosition(Obs3X,Obs3Y);
Obs4.setPosition(Obs4X,Obs4Y);
//Creat scrolling background
Background.setPosition(0,BackgroundY1);
Background1.setPosition(0,BackgroundY2);
if (BackgroundY2>0)
{
BackgroundY1=0;
BackgroundY2=BackgroundY1-500;
}
BackgroundY1+=0.1;
BackgroundY2+=0.1;
//Set Obs LOOP
if (Obs1Y>SCREEN_HEIGH)
{Obs1Y=0;Obs1X=getRandomNumber(borderLeft,borderRight);} else {Obs1Y=Obs1Y+gameSpeed;}
if (Obs2Y>SCREEN_HEIGH)
{Obs2Y=0;Obs2X=getRandomNumber(borderLeft,borderRight);} else {Obs2Y=Obs2Y+gameSpeed;}
if (Obs3Y>SCREEN_HEIGH)
{Obs3Y=0;Obs3X=getRandomNumber(borderLeft,borderRight);} else {Obs3Y=Obs3Y+gameSpeed;}
if (Obs4Y>SCREEN_HEIGH)
{Obs4Y=0;Obs4X=getRandomNumber(borderLeft,borderRight); Obs4.setScale(1,1);} else {Obs4Y=Obs4Y+gameSpeed;}
//Game level
if(score>10 && score<40) {gameSpeed=0.4;}
if(score>40 && score<70) {gameSpeed=0.5;}
if(score>70 && score<100) {gameSpeed=0.6;}
//Create event to handle input from keyboard
Event event;
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-20;}}
if (event.key.code == sf::Keyboard::Right)
{if(RacerX<borderRight){RacerX=RacerX+20;}}
if (event.key.code == sf::Keyboard::Up)
{if(RacerY>0){RacerY=RacerY-20;}}
if (event.key.code == sf::Keyboard::Down)
{if(RacerY<SCREEN_HEIGH-70){RacerY=RacerY+20;}}
}
}
//Check if accident happen
if(Obs1.getGlobalBounds().intersects(Racer.getGlobalBounds()) == true
|| Obs2.getGlobalBounds().intersects(Racer.getGlobalBounds()) == true
|| Obs3.getGlobalBounds().intersects(Racer.getGlobalBounds()) == true )
{
gameOver();
};
if(RacerX < 140 || RacerX > 660)
{
gameOver();
};
if(Obs4.getGlobalBounds().intersects(Racer.getGlobalBounds()) == true)
{
Obs4.setScale(0,0);
score += 10;
};
if(score > 90)
{
win();
};
//Clear and redraw 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;
}
int startGame()
{
//TODO
}
//Game over
int gameOver()
{
Texture gameover;
gameover.loadFromFile("images/gameover.png");
Sprite Gameover(gameover);
while (app.isOpen())
{
Event event;
while (app.pollEvent(event))
{
if (event.type == Event::Closed)
app.close();
}
Font myfont;
myfont.loadFromFile("font/xirod.ttf");
stringscore="YOUR SCORE:"+to_string(score);
Text text(stringscore, myfont, 30);
text.setPosition(210,450);
app.clear();
app.draw(Gameover);
app.draw(text);
app.display();
}
}
//Game over
int win()
{
Texture wins;
wins.loadFromFile("images/wins.png");
Sprite Wins(wins);
while (app.isOpen())
{
Event event;
while (app.pollEvent(event))
{
if (event.type == Event::Closed)
app.close();
}
Font myfont;
myfont.loadFromFile("font/xirod.ttf");
stringscore="YOUR SCORE:"+to_string(score);
Text text(stringscore, myfont, 30);
text.setPosition(210,450);
app.clear();
app.draw(Wins);
app.draw(text);
app.display();
}
}
int getRandomNumber(int a, int b)
{
static bool first = true; if (first){srand( time(NULL) );first = false;}
int result=a + rand() % (( b + 1 ) - a);
result=(result/10)*10;
return result;
}