-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
302 lines (259 loc) · 9.09 KB
/
main.cpp
File metadata and controls
302 lines (259 loc) · 9.09 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
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <SDL2/SDL_ttf.h>
#include <sstream>
struct OpponentCar
{
SDL_Rect rect;
int speed;
};
int main(int argc, char *argv[])
{
const int WINDOW_WIDTH = 500;
const int WINDOW_HEIGHT = 800;
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
std::cout << "Failed to initialize SDL: " << SDL_GetError() << std::endl;
return 1;
}
int imgFlags = IMG_INIT_PNG;
if (!(IMG_Init(imgFlags) & imgFlags))
{
std::cout << "Failed to initialize SDL_image: " << IMG_GetError() << std::endl;
SDL_Quit();
return 1;
}
SDL_Window *window = SDL_CreateWindow("Car Avoider", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
if (window == nullptr)
{
std::cout << "Failed to create window: " << SDL_GetError() << std::endl;
IMG_Quit();
SDL_Quit();
return 1;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == nullptr)
{
std::cout << "Failed to create renderer: " << SDL_GetError() << std::endl;
SDL_DestroyWindow(window);
IMG_Quit();
SDL_Quit();
return 1;
}
TTF_Init();
TTF_Font *font = TTF_OpenFont("opensansregular.ttf", 24);
if (!font)
{
std::cout << "Failed to load font: " << TTF_GetError() << std::endl;
return 1;
}
SDL_Surface *imageSurface = IMG_Load("roadforgame.png");
if (imageSurface == nullptr)
{
std::cout << "Failed to load image: " << IMG_GetError() << std::endl;
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
IMG_Quit();
SDL_Quit();
return 1;
}
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, imageSurface);
SDL_FreeSurface(imageSurface);
if (texture == nullptr)
{
std::cout << "Failed to create texture: " << SDL_GetError() << std::endl;
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
IMG_Quit();
SDL_Quit();
return 1;
}
SDL_Surface *carSurface = IMG_Load("playercar.png");
if (carSurface == nullptr)
{
std::cout << "Failed to load car image: " << IMG_GetError() << std::endl;
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
IMG_Quit();
SDL_Quit();
return 1;
}
SDL_Texture *carTexture = SDL_CreateTextureFromSurface(renderer, carSurface);
SDL_FreeSurface(carSurface);
if (carTexture == nullptr)
{
std::cout << "Failed to create car texture: " << SDL_GetError() << std::endl;
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
IMG_Quit();
SDL_Quit();
return 1;
}
SDL_Surface *opponentCarSurface = IMG_Load("opponentcar.png");
if (opponentCarSurface == nullptr)
{
std::cout << "Failed to load opponent car image: " << IMG_GetError() << std::endl;
SDL_DestroyTexture(carTexture);
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
IMG_Quit();
SDL_Quit();
return 1;
}
SDL_Texture *opponentCarTexture = SDL_CreateTextureFromSurface(renderer, opponentCarSurface);
SDL_FreeSurface(opponentCarSurface);
if (opponentCarTexture == nullptr)
{
std::cout << "Failed to create opponent car texture: " << SDL_GetError() << std::endl;
SDL_DestroyTexture(carTexture);
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
IMG_Quit();
SDL_Quit();
return 1;
}
int playerCarX = 215;
int playerCarSpeed = 200;
const int numberOfOpponentCars = 3;
const int opponentCarWidth = 81;
const int opponentCarHeight = 157;
const int constantSpeed = 20;
const int frameRate = 60;
const int frameDelay = 1000 / frameRate;
Uint32 startTime = SDL_GetTicks();
int score = 0;
int lastscore = 0;
std::vector<OpponentCar> opponentCars;
const int positions[3] = {15, 215, 415};
std::srand(std::time(nullptr));
OpponentCar currentOpponentCar;
currentOpponentCar.rect = {positions[std::rand() % 3], -(std::rand() % 800 + 200), opponentCarWidth, opponentCarHeight};
currentOpponentCar.speed = constantSpeed;
opponentCars.push_back(currentOpponentCar);
bool quit = false;
SDL_Event event;
bool isGameOver = false;
while (!quit)
{
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
quit = true;
}
else if (event.type == SDL_KEYDOWN)
{
if (event.key.keysym.sym == SDLK_a && playerCarX > 15)
{
playerCarX -= playerCarSpeed;
}
else if (event.key.keysym.sym == SDLK_d && playerCarX < 415)
{
playerCarX += playerCarSpeed;
}
}
}
Uint32 currentTime = SDL_GetTicks();
Uint32 elapsedTime = (currentTime - startTime) / 1000;
if (!isGameOver)
{
lastscore = score;
score = elapsedTime;
bool isCollision = false;
SDL_Rect playerCarBoundingBox = {playerCarX, 620, 81, 157};
for (OpponentCar &car : opponentCars)
{
SDL_Rect opponentCarBoundingBox = car.rect;
if (SDL_HasIntersection(&playerCarBoundingBox, &opponentCarBoundingBox))
{
isCollision = true;
break;
}
}
if (isCollision)
{
isGameOver = true;
}
else
{
for (OpponentCar &car : opponentCars)
{
car.rect.y += car.speed;
if (car.rect.y > 800)
{
car.rect.y = -(std::rand() % 800 + 200);
car.rect.x = positions[std::rand() % 3];
}
}
}
}
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, nullptr, nullptr);
SDL_Rect playerCarRect = {playerCarX, 620, 81, 157};
SDL_RenderCopy(renderer, carTexture, nullptr, &playerCarRect);
for (OpponentCar &car : opponentCars)
{
SDL_RenderCopy(renderer, opponentCarTexture, nullptr, &car.rect);
}
SDL_Color textColor = {255, 255, 255};
std::stringstream scoreText;
scoreText << "Score: " << score;
SDL_Surface *textSurface = TTF_RenderText_Solid(font, scoreText.str().c_str(), textColor);
SDL_Texture *textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
int textWidth = textSurface->w;
int textHeight = textSurface->h;
SDL_FreeSurface(textSurface);
int scoreX = WINDOW_WIDTH - textWidth - 10;
int scoreY = 10;
SDL_Rect textRect = {scoreX, scoreY, textWidth, textHeight};
SDL_RenderCopy(renderer, textTexture, nullptr, &textRect);
SDL_DestroyTexture(textTexture);
if (isGameOver)
{
SDL_Surface *gameOverSurface = IMG_Load("gameover.png");
if (gameOverSurface != nullptr)
{
SDL_Texture *gameOverTexture = SDL_CreateTextureFromSurface(renderer, gameOverSurface);
SDL_Rect gameOverRect = {0, 0, 500, 800};
SDL_RenderCopy(renderer, gameOverTexture, nullptr, &gameOverRect);
SDL_DestroyTexture(gameOverTexture);
SDL_FreeSurface(gameOverSurface);
}
std::stringstream lastScoreText;
lastScoreText << "Your Score: " << lastscore;
SDL_Surface *lastScoreSurface = TTF_RenderText_Solid(font, lastScoreText.str().c_str(), textColor);
SDL_Texture *lastScoreTexture = SDL_CreateTextureFromSurface(renderer, lastScoreSurface);
int lastScoreTextWidth = lastScoreSurface->w;
int lastScoreTextHeight = lastScoreSurface->h;
SDL_FreeSurface(lastScoreSurface);
int lastScoreX = (500 - lastScoreTextWidth) / 2;
int lastScoreY = 500;
SDL_Rect lastScoreRect = {lastScoreX, lastScoreY, lastScoreTextWidth, lastScoreTextHeight};
SDL_RenderCopy(renderer, lastScoreTexture, nullptr, &lastScoreRect);
SDL_DestroyTexture(lastScoreTexture);
SDL_RenderPresent(renderer);
SDL_Delay(3000);
return 1;
}
SDL_RenderPresent(renderer);
SDL_Delay(frameDelay);
}
SDL_DestroyTexture(opponentCarTexture);
SDL_DestroyTexture(carTexture);
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_CloseFont(font);
TTF_Quit();
IMG_Quit();
SDL_Quit();
return 0;
}