-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
586 lines (510 loc) · 17.2 KB
/
Copy pathmain.cpp
File metadata and controls
586 lines (510 loc) · 17.2 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
#include <iostream>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <ctime>
#include <fstream>
#include "game.h"
#include "TextObject.h"
using namespace std;
const int SCREEN_WIDTH = 1400;
const int SCREEN_HEIGHT = 800;
/*int step = 20;*/
const int x_central = SCREEN_WIDTH/2.0;
const int y_central = SCREEN_HEIGHT/2.0;
TTF_Font* font = NULL;
/*enum Action{
LEFT, RIGHT, UP, DOWN, NONE, QUIT, PAUSE
};
Action getUserAction() {
SDL_Event e;
if (SDL_PollEvent(&e) == 0) return NONE;
if (e.type == SDL_QUIT) return QUIT;
if (e.type == SDL_KEYDOWN) {
switch (e.key.keysym.sym) {
case SDLK_ESCAPE: return QUIT;
case SDLK_LEFT: return LEFT;
case SDLK_RIGHT: return RIGHT;
case SDLK_DOWN: return DOWN;
case SDLK_UP: return UP;
case SDLK_PAUSE: return PAUSE;
default: return NONE;
}
}
return NONE;
}*/
struct point {
int x_,y_;
Uint32 time_point;
void get_point(){
x_ = rand()%SCREEN_WIDTH;
y_ = rand()%SCREEN_HEIGHT;
time_point = SDL_GetTicks();
}
int size = 15;
void render (SDL_Renderer* renderer, SDL_Rect* prect){
SDL_Rect rectpoint;
rectpoint.x = x_;
rectpoint.y = y_;
rectpoint.w = size;
rectpoint.h = size;
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 1);
SDL_RenderFillRect(renderer,&rectpoint);
if (eaten(prect)){
cerr << "Score : " << score << endl;
get_point();
}
else if (SDL_GetTicks() - time_point >= 20000){
get_point();
}
}
bool eaten (SDL_Rect* prect){
int x_check = x_ + size/2;
int y_check = y_ + size/2;
if ((x_check >= prect->x && x_check <= prect->x + prect->w) && (y_check >= prect->y && y_check <= prect->y + prect->h)){
score ++;
return 1;
}
return 0;
}
int score = 0;
};
struct Laser{
Uint32 time_;
Uint32 getTime(){
return SDL_GetTicks();
}
int size = 3;
int step = 15;
int x[6],y[6];
bool gameOver = 0;
void init1 (){
x[0] = - x_central ; y[0] = -y_central; // Tâm màn hình tại [0,0]
x[1] = - SCREEN_HEIGHT / 2.0 ; y[1] = - x_central;
x[2] = 0 ; y[2] = - x_central;
x[3] = SCREEN_HEIGHT / 2.0 ; y[3] = - x_central;
x[4] = x_central ; y[4] = - x_central;
x[5] = x_central ; y[5] = 0;
}
int side = 1;
/*SDL_Rect getRect (int x_, int y_){
SDL_Rect rect;
rect.h = 500;
rect.w = 500;
rect.x = x_;
rect.y =y_;
return rect;
}*/
/*SDL_Rect rectgame2 = getRect(100,100);*/
SDL_Rect* threat;
int move = 0;
bool reached = 0;
void move2 (){
if (reached || SDL_GetTicks() - time_ == 100){
int a = rand()%4;
while (a == move){
a = rand()%4;
}
move = a;
reached = 0;
time_ = getTime();
}
switch (move){
case 0: moveUp(threat); break;
case 1: moveDown(threat); break;
case 2: moveLeft(threat) ; break;
case 3: moveRight(threat) ; break;
}
}
void render2(SDL_Renderer* renderer, SDL_Rect* player,bool game2){
/*SDL_SetRenderDrawColor(renderer,255,255,120,1);
SDL_RenderFillRect(renderer,threat);*/
if (inside2(player) && game2){
gameOver = 1;
}
}
void moveUp (SDL_Rect* prect){
if (prect->y > step + 30) prect->y -= step;
else reached = 1;
}
void moveDown (SDL_Rect* prect){
if (prect->y + prect->h < SCREEN_HEIGHT - step - 30) prect->y += step;
else reached = 1;
}
void moveLeft (SDL_Rect* prect){
if (prect->x > step + 30) prect->x -= step;
else reached = 1;
}
void moveRight (SDL_Rect* prect){
if (prect->x + prect->w < SCREEN_WIDTH - step - 30) prect->x += step;
else reached = 1;
}
/*bool inside1(SDL_Rect* prect){
if (prect->x <= 100 || prect->x >= SCREEN_WIDTH - 100) return 0;
if (prect->y <= 100 || prect->y >= SCREEN_HEIGHT - 100) return 0;
return 1;
}*/
bool inside2(SDL_Rect* prect1){
if (prect1->x <= threat->x || prect1->x >= threat->x + threat->w) return 0;
if (prect1->y <= threat->y || prect1->y >= threat->y + threat->h) return 0;
if (prect1->x + prect1->w <= threat->x || prect1->x + prect1->w >= threat->x + threat->w) return 0;
if (prect1->y + prect1->h <= threat->y || prect1->y + prect1->h >= threat->y + threat->h) return 0;
return 1;
}
void roll(SDL_Rect* prect){
for (int i = 0 ; i < 6 ; i++){
int new_x = x[i]*0.9999 - y[i]*0.0141*side;
int new_y = x[i]*0.0141*side + y[i]*0.9999;
if (new_x >= 0){
if (new_y >= 0){
new_x ++;
new_y ++;
}
else {
new_x ++;
new_y --;
}
}
else{
if (new_y >= 0){
new_x --;
new_y ++;
}
else {
new_x --;
new_y --;
}
}
x[i] = new_x;
y[i] = new_y;
if (touched(prect,x[i],y[i])) gameOver = 1;
}
}
bool touched (SDL_Rect* prect,int x_, int y_){
int x1_rect = prect->x - x_central;
int x2_rect = prect->x + prect->w - x_central;
int y1_rect = prect->y - y_central;
int y2_rect = prect->y + prect->h - y_central;
if (x_ == 0){
if (x1_rect * x2_rect <= 0) return 1;
}
else {
float k = 1.0 * y_/x_;
if (((y1_rect - k*x1_rect)*(y2_rect - k*x2_rect) <= 0) ||
((y1_rect - k*x2_rect)*(y2_rect - k*x1_rect) <= 0))
return 1;
}
return 0;
}
void render (SDL_Renderer* renderer){
SDL_SetRenderDrawColor(renderer,255,255,120,1);
for (int i = 0 ; i < 6 ; i++){
SDL_RenderDrawLine(renderer, x[i]+x_central, y[i]+y_central, x_central-x[i], y_central-y[i]);
}
}
};
void printMenu (SDL_Renderer* renderer,TTF_Font* font){
SDL_SetRenderDrawColor(renderer,255,255,255,255);
SDL_RenderClear(renderer);
TextObject menu;
menu.setColor(TextObject::Black_Text);
std::string str_start = "Press SPACE to play";
std::string str_exit = "Press ESCAPE to exit";
std::string str_stop = "While playing, press P to pause";
std::string str_ins = "INSTRUCTION";
menu.setText(str_start);
menu.loadFromRenderText(font,renderer);
menu.render(renderer, 300, 100);
menu.setText(str_exit);
menu.loadFromRenderText(font,renderer);
menu.render(renderer, 300, 300);
menu.setText(str_stop);
menu.loadFromRenderText(font,renderer);
menu.render(renderer, 300, 500);
menu.setColor(TextObject::Red_Text);
menu.setText(str_ins);
menu.loadFromRenderText(font,renderer);
menu.render(renderer, 100, 100);
SDL_RenderPresent(renderer);
menu.free();
}
/*void moveUp (SDL_Rect* prect){
if (prect->y > step) prect->y -= step;
}
void moveDown (SDL_Rect* prect){
if (prect->y + prect->h < SCREEN_HEIGHT - step) prect->y += step;
}
void moveLeft (SDL_Rect* prect){
if (prect->x > step) prect->x -= step;
}
void moveRight (SDL_Rect* prect){
if (prect->x + prect->w < SCREEN_WIDTH - step) prect->x += step;
}*/
int main(int argc, char* argv[])
{
srand(time(0));
SDL_Window* window;
SDL_Renderer* renderer;
initSDL(window, renderer);
if (TTF_Init() < 0)
{
SDL_Log("%s", TTF_GetError());
return -1;
}
font = TTF_OpenFont("Font//Calibri Light.ttf",20);
TextObject score_text;
score_text.setColor(TextObject::White_Text);
TextObject red_text;
red_text.setColor(TextObject::Red_Text);
TextObject black_text;
black_text.setColor(TextObject::Black_Text);
SDL_Event event;
printMenu(renderer,font);
SDL_RenderPresent(renderer);
SDL_Delay(3000);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderClear(renderer);
SDL_Texture* GameOver = loadTexture("GameOver.png", renderer);
SDL_Texture* background = loadTexture("Background 1.png", renderer);
SDL_RenderCopy(renderer, background, NULL, NULL);
SDL_Texture* pic = loadTexture("slime.png", renderer);
SDL_Rect rect;
SDL_QueryTexture (pic, NULL, NULL, &rect.w, &rect.h);
SDL_QueryTexture (pic, NULL, NULL, &rect.w, &rect.h);
rect.w /= 11.0;
rect.h /= 11.0;
rect.x = x_central;
rect.y = y_central;
SDL_RenderCopy(renderer, pic, NULL, &rect);
SDL_RenderPresent(renderer);
SDL_Texture* pic2 = loadTexture("slime.png", renderer);
SDL_Rect rect2;
SDL_QueryTexture (pic, NULL, NULL, &rect2.w, &rect2.h);
rect2.x = 100;
rect2.y = 100;
Laser laser;
laser.init1();
point Point;
Point.get_point();
laser.threat = &rect2;
Uint32 time = SDL_GetTicks();
std::string str_switch = " SWITCH ";
bool quit = false;
bool paused = 0;
bool exit = 0;
bool game1 = 0;
bool game1_done = 0;
bool game2 = 0;
int oldScore = 0;
while (!quit){
if (SDL_PollEvent(&event) == 0) {
continue;
}
else if (event.type == SDL_QUIT) break;
else if (event.type == SDL_KEYDOWN){
switch (event.key.keysym.sym){
case SDLK_ESCAPE: quit = true; break;
case SDLK_SPACE:{
if (game1_done){
game2 = 1;
}
else game1 = 1;
break;
}
default : break;
}
}
else if (event.type == SDL_MOUSEMOTION){
int x_mouse,y_mouse;
SDL_GetMouseState (&x_mouse,&y_mouse);
rect.x = x_mouse - rect.w/2;
rect.y = y_mouse - rect.h/2;
}
//if (!game1_done) background = loadTexture("Background 2.jpeg", renderer);
SDL_RenderCopy(renderer, background, NULL, NULL);
SDL_RenderCopy(renderer, pic, NULL, &rect);
SDL_RenderPresent(renderer);
if (!game1_done) laser.render(renderer);
SDL_RenderPresent(renderer);
while (game1){
if (paused){
if (SDL_PollEvent(&event) == 0) {
continue;
}
if (event.type == SDL_KEYDOWN){
if (event.key.keysym.sym == SDLK_p){
paused = !paused;
}
else if (event.key.keysym.sym == SDLK_ESCAPE){
game1 = 0;
quit = 1;
}
}
continue;
}
if (SDL_GetTicks() - time >= 30000){
laser.side *= -1;
time = SDL_GetTicks();
}
laser.roll(&rect);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, background, NULL, NULL);
SDL_RenderCopy(renderer, pic, NULL, &rect);
if (!game1_done) laser.render(renderer);
Point.render(renderer,&rect);
if (SDL_GetTicks() - time >= 28000){
red_text.setText(str_switch);
red_text.loadFromRenderText(font, renderer);
red_text.render(renderer,SCREEN_WIDTH/2 - 200, SCREEN_HEIGHT - 200);
SDL_RenderPresent(renderer);
//SDL_Delay(50);
}
//Show score
std::string str_score = "Score: ";
std::string str_val = std::to_string(Point.score);
str_score += str_val;
score_text.setText(str_score);
score_text.loadFromRenderText(font, renderer);
score_text.render(renderer,SCREEN_WIDTH - 200, 15);
SDL_RenderPresent(renderer);
if (SDL_PollEvent(&event) == 0) {
SDL_Delay(50);
continue;
}
else if (event.type == SDL_QUIT) break;
else if (event.type == SDL_KEYDOWN){
switch (event.key.keysym.sym){
case SDLK_ESCAPE: quit = 1; game1 = 0 ; break;
case SDLK_p:{
paused = !paused;
break;
}
default : break;
}
}
else if (event.type == SDL_MOUSEMOTION){
int x_mouse,y_mouse;
SDL_GetMouseState (&x_mouse,&y_mouse);
rect.x = x_mouse - rect.w/2;
rect.y = y_mouse - rect.h/2;
}
if (laser.gameOver) {
SDL_RenderCopy(renderer, GameOver, NULL, NULL);
font = TTF_OpenFont("Font//Calibri Light.ttf",60);
black_text.setText(str_score);
black_text.loadFromRenderText(font, renderer);
black_text.render(renderer,SCREEN_WIDTH/2 -100, SCREEN_HEIGHT -200);
quit = 1;
SDL_RenderPresent(renderer);
quit = 1;
game1 = 0;
}
if (Point.score - oldScore == 3){
game1_done = 1;
game1 = 0;
oldScore = Point.score;
}
}
if (game1_done) {
SDL_RenderCopy(renderer, pic2, NULL, laser.threat);
laser.render2(renderer,&rect,game2);
SDL_RenderPresent(renderer);
laser.time_ = laser.getTime();
}
while (game2){
if (paused){
if (SDL_PollEvent(&event) == 0) {
continue;
}
if (event.type == SDL_KEYDOWN){
if (event.key.keysym.sym == SDLK_p){
paused = !paused;
}
else if (event.key.keysym.sym == SDLK_ESCAPE){
game2 = 0;
quit = 1;
}
}
continue;
}
if (SDL_GetTicks() - time >= 30000){
laser.side *= -1;
time = SDL_GetTicks();
}
laser.move2();
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, background, NULL, NULL);
SDL_RenderCopy(renderer, pic, NULL, &rect);
SDL_RenderCopy(renderer, pic2, NULL, laser.threat);
laser.render2(renderer,&rect,game2);
Point.render(renderer,&rect);
/*if (SDL_GetTicks() - time >= 28000){
red_text.setText(str_switch);
red_text.loadFromRenderText(font, renderer);
red_text.render(renderer,SCREEN_WIDTH/2 - 200, SCREEN_HEIGHT - 200);
SDL_RenderPresent(renderer);
//SDL_Delay(50);
}*/
//Show score
std::string str_score = "Score: ";
std::string str_val = std::to_string(Point.score);
str_score += str_val;
score_text.setText(str_score);
score_text.loadFromRenderText(font, renderer);
score_text.render(renderer,SCREEN_WIDTH - 200, 15);
SDL_RenderPresent(renderer);
if (SDL_PollEvent(&event) == 0) {
SDL_Delay(50);
continue;
}
else if (event.type == SDL_QUIT) break;
else if (event.type == SDL_KEYDOWN){
switch (event.key.keysym.sym){
case SDLK_ESCAPE: quit = 1; game2 = 0 ; break;
case SDLK_p:{
paused = !paused;
break;
}
default : break;
}
}
else if (event.type == SDL_MOUSEMOTION){
int x_mouse,y_mouse;
SDL_GetMouseState (&x_mouse,&y_mouse);
rect.x = x_mouse - rect.w/2;
rect.y = y_mouse - rect.h/2;
}
if (laser.gameOver) {
SDL_RenderCopy(renderer, GameOver, NULL, NULL);
font = TTF_OpenFont("Font//Calibri Light.ttf",60);
black_text.setText(str_score);
black_text.loadFromRenderText(font, renderer);
black_text.render(renderer,SCREEN_WIDTH/2 -100, SCREEN_HEIGHT -200);
quit = 1;
SDL_RenderPresent(renderer);
quit = 1;
game2 = 0;
}
if (Point.score - oldScore == 3){
game2 = 0;
game1_done = 0;
oldScore = Point.score;
}
}
}
SDL_RenderPresent (renderer);
waitUntilKeyPressed();
SDL_Delay(100);
// Your drawing code here
// use SDL_RenderPresent(renderer) to show it
SDL_RenderPresent (renderer);
waitUntilKeyPressed();
quitSDL(window, renderer);
score_text.free();
black_text.free();
red_text.free();
TTF_Quit();
return 0;
}