Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 45.91 KB | None | 0 0
  1. //Using SDL, SDL_image, standard IO, and strings
  2. #include <SDL.h>
  3. #include <SDL_image.h>
  4. #include <stdio.h>
  5. #include <string>
  6. #include <iostream>
  7. #include <Windows.h>
  8. #include <cstdlib>
  9. #include <ctime>
  10. #include <SDL_ttf.h>
  11. #include <SDL_mixer.h>
  12. #include <fstream>
  13. using namespace std;
  14. //Screen dimension constants
  15. const int SCREEN_WIDTH = 1200;
  16. const int SCREEN_HEIGHT = 690;
  17. const int rankListMax = 10;
  18. //Starts up SDL and creates window
  19. bool init();
  20.  
  21. //Loads media
  22. bool loadMedia();
  23.  
  24. //Frees media and shuts down SDL
  25. void close();
  26.  
  27. //Loads individual image as texture
  28. SDL_Texture* loadTexture( std::string path );
  29.  
  30. //The window we'll be rendering to
  31. SDL_Window* gWindow = NULL;
  32.  
  33. //The window renderer
  34. SDL_Renderer* gRenderer = NULL;
  35.  
  36. //Current displayed texture
  37. SDL_Texture* gTransparent = NULL;
  38. SDL_Texture* gTexture = NULL;
  39. SDL_Texture* gKhung = NULL;
  40. SDL_Texture* gText = NULL;
  41. SDL_Texture* gRankList = NULL;
  42. SDL_Texture* gInput = NULL;
  43. TTF_Font* gFont = NULL;
  44. SDL_Surface* gSurface = NULL;
  45. SDL_Texture* gBackground = NULL;
  46. SDL_Texture* gEffectMo = NULL;
  47. SDL_Texture* gMenu_play = NULL;
  48. SDL_Texture* gMenu_setting = NULL;
  49. SDL_Texture* gMenu_guide = NULL;
  50. SDL_Texture* gMenu_rank = NULL;
  51. SDL_Texture* gMenu_quit = NULL;
  52.  
  53. SDL_Texture* gMenu_play_choose = NULL;
  54. SDL_Texture* gMenu_setting_choose = NULL;
  55. SDL_Texture* gMenu_guide_choose = NULL;
  56. SDL_Texture* gMenu_rank_choose = NULL;
  57. SDL_Texture* gMenu_quit_choose = NULL;
  58. SDL_Texture* gExitGame = NULL;
  59. SDL_Texture* gGameover = NULL;
  60. SDL_Texture* gHeart = NULL;
  61. SDL_Texture* gPause = NULL;
  62. //sounds
  63.  
  64. Mix_Chunk* gMenu_click = NULL;
  65. Mix_Chunk* gEat = NULL;
  66. Mix_Chunk* gOver = NULL;
  67. Mix_Chunk* gDie = NULL;
  68. Mix_Chunk* gCongratulation = NULL;
  69. //rank
  70.  
  71. SDL_Texture* gRank_top_1 = NULL;
  72. SDL_Texture* gRank_top_2 = NULL;
  73. SDL_Texture* gRank_top_3 = NULL;
  74.  
  75. //Snake
  76. SDL_Texture* gDot = NULL;
  77. SDL_Texture* gDot_ngangtrai = NULL;
  78. SDL_Texture* gDot_ngangphai = NULL;
  79. SDL_Texture* gDot_doctren = NULL;
  80. SDL_Texture* gDot_docduoi = NULL;
  81. SDL_Texture* gDot_left_top = NULL;
  82. SDL_Texture* gDot_left_bot = NULL;
  83. SDL_Texture* gDot_right_top = NULL;
  84. SDL_Texture* gDot_right_bot = NULL;
  85. SDL_Texture* gDot_top_left = NULL;
  86. SDL_Texture* gDot_top_right = NULL;
  87. SDL_Texture* gDot_bot_left = NULL;
  88. SDL_Texture* gDot_bot_right = NULL;
  89. SDL_Texture* gDau = NULL;
  90. SDL_Texture* gDau_right = NULL;
  91. SDL_Texture* gDau_left = NULL;
  92. SDL_Texture* gDau_down = NULL;
  93. SDL_Texture* gDau_up = NULL;
  94. SDL_Texture* gTarget = NULL;
  95. SDL_Texture* gTarget_2 = NULL;
  96. SDL_Texture* gDuoi = NULL;
  97. SDL_Texture* gDuoi_ngangtrai = NULL;
  98. SDL_Texture* gDuoi_ngangphai = NULL;
  99. SDL_Texture* gDuoi_doctren = NULL;
  100. SDL_Texture* gDuoi_docduoi = NULL;
  101. fstream rank_in;
  102. fstream rank_out;
  103.  
  104. SDL_Event e;
  105. bool quit = false;
  106. time_t target_1,target_2;
  107. enum trangthai{LEFT,UP,RIGHT,DOWN};
  108. int mouse_x, mouse_y;
  109. bool returnmenu = false;
  110. struct RankPerson{
  111. int score;
  112. string name;
  113. };
  114.  
  115. RankPerson rankPerson[rankListMax];
  116. struct Snake{
  117. int x;
  118. int y;
  119. int dodai;
  120. int kichthuoc;
  121. SDL_Rect dot[1000];
  122. trangthai tt;
  123. int score;
  124. bool die;
  125. int life;
  126. };
  127.  
  128. Snake snake;
  129. SDL_Rect target;
  130. bool init()
  131. {
  132. //Initialization flag
  133. bool success = true;
  134.  
  135. //Initialize SDL
  136. if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
  137. {
  138. printf( "SDL could not initialize! SDL Error: %s\n", SDL_GetError() );
  139. success = false;
  140. }
  141. else
  142. {
  143. //Set texture filtering to linear
  144. if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) )
  145. {
  146. printf( "Warning: Linear texture filtering not enabled!" );
  147. }
  148.  
  149. //Create window
  150. gWindow = SDL_CreateWindow( "SNAKE C++", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
  151. if( gWindow == NULL )
  152. {
  153. printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() );
  154. success = false;
  155. }
  156. else
  157. {
  158. //Create renderer for window
  159. gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED );
  160. if( gRenderer == NULL )
  161. {
  162. printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
  163. success = false;
  164. }
  165. else
  166. {
  167. //Initialize renderer color
  168. SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
  169.  
  170. //Initialize PNG loading
  171. int imgFlags = IMG_INIT_PNG;
  172. if( !( IMG_Init( imgFlags ) & imgFlags ) )
  173. {
  174. printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
  175. success = false;
  176. }
  177.  
  178. //Initialize SDL_ttf
  179. if( TTF_Init() == -1 )
  180. {
  181. printf( "SDL_ttf could not initialize! SDL_ttf Error: %s\n", TTF_GetError() );
  182. success = false;
  183. }
  184. //Initialize SDL_mixer
  185. if( Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 2048 ) < 0 )
  186. {
  187. printf( "SDL_mixer could not initialize! SDL_mixer Error: %s\n", Mix_GetError() );
  188. success = false;
  189. }
  190. }
  191. }
  192. }
  193.  
  194. return success;
  195. }
  196.  
  197. bool loadMedia()
  198. {
  199. //Loading success flag
  200. bool success = true;
  201. rank_in.open("rank/rank.txt", ios :: in);
  202. if(rank_in.is_open()){
  203. int tmp = 0;
  204. while(!rank_in.eof() && tmp < rankListMax){
  205. rank_in >> rankPerson[tmp].score;
  206. getline(rank_in, rankPerson[tmp].name);
  207. tmp++;
  208. }
  209. }
  210. else{
  211. cout << "Can't open file rank.txt";
  212. success = false;
  213. }
  214. rank_in.close();
  215. //rank_out.close();
  216. //Load texture
  217.  
  218. gPause = loadTexture( "img/pause.png");
  219. if(gPause == NULL){
  220. printf( "Failed to load texture image!\n" );
  221. success = false;
  222. }
  223. gGameover = loadTexture( "img/gameover.png");
  224. if(gGameover == NULL){
  225. printf( "Failed to load texture image!\n" );
  226. success = false;
  227. }
  228. gBackground = loadTexture( "img/background.png" );
  229. if( gBackground == NULL )
  230. {
  231. printf( "Failed to load texture image!\n" );
  232. success = false;
  233. }
  234. gTransparent = loadTexture( "img/transparent.png" );
  235. if( gTransparent == NULL )
  236. {
  237. printf( "Failed to load texture image!\n" );
  238. success = false;
  239. }
  240. gEffectMo = loadTexture( "img/effectMo.png" );
  241. if( gEffectMo == NULL )
  242. {
  243. printf( "Failed to load texture image!\n" );
  244. success = false;
  245. }
  246. gRankList = loadTexture( "img/rank.png");
  247. if( gRankList == NULL){
  248. printf( "Failed to load texture image!\n" );
  249. success = false;
  250. }
  251. gInput = loadTexture( "img/input.png");
  252. if( gInput == NULL){
  253. printf( "Failed to load texture image!\n" );
  254. success = false;
  255. }
  256. // Load menu_play
  257. gMenu_play = loadTexture( "img/menu_play.png" );
  258. if( gMenu_play == NULL )
  259. {
  260. printf( "Failed to load texture image!\n" );
  261. success = false;
  262. }
  263. gMenu_play_choose = loadTexture( "img/menu_play_choose.png" );
  264. if( gMenu_play_choose == NULL )
  265. {
  266. printf( "Failed to load texture image!\n" );
  267. success = false;
  268. }
  269. //load menu_setting
  270. gMenu_setting = loadTexture( "img/menu_setting.png" );
  271. if( gMenu_setting == NULL )
  272. {
  273. printf( "Failed to load texture image!\n" );
  274. success = false;
  275. }
  276. gMenu_setting_choose = loadTexture( "img/menu_setting_choose.png" );
  277. if( gMenu_setting_choose == NULL )
  278. {
  279. printf( "Failed to load texture image!\n" );
  280. success = false;
  281. }
  282. //load menu_rank
  283. gMenu_rank = loadTexture( "img/menu_rank.png" );
  284. if( gMenu_rank == NULL )
  285. {
  286. printf( "Failed to load texture image!\n" );
  287. success = false;
  288. }
  289. gMenu_rank_choose = loadTexture( "img/menu_rank_choose.png" );
  290. if( gMenu_rank_choose == NULL )
  291. {
  292. printf( "Failed to load texture image!\n" );
  293. success = false;
  294. }
  295. //load menu_guide
  296. gMenu_guide = loadTexture( "img/menu_guide.png" );
  297. if( gMenu_guide == NULL )
  298. {
  299. printf( "Failed to load texture image!\n" );
  300. success = false;
  301. }
  302. gMenu_guide_choose = loadTexture( "img/menu_guide_choose.png" );
  303. if( gMenu_guide_choose == NULL )
  304. {
  305. printf( "Failed to load texture image!\n" );
  306. success = false;
  307. }
  308. //load menu_quit
  309. gMenu_quit = loadTexture( "img/menu_quit.png" );
  310. if( gMenu_quit == NULL )
  311. {
  312. printf( "Failed to load texture image!\n" );
  313. success = false;
  314. }
  315. gMenu_quit_choose = loadTexture( "img/menu_quit_choose.png" );
  316. if( gMenu_quit_choose == NULL )
  317. {
  318. printf( "Failed to load texture image!\n" );
  319. success = false;
  320. }
  321. //load exitGame
  322. gExitGame = loadTexture( "img/exitGame.png" );
  323. if( gExitGame == NULL )
  324. {
  325. printf( "Failed to load texture image!\n" );
  326. success = false;
  327. }
  328. //load sounds
  329. gMenu_click = Mix_LoadWAV( "sounds/click.wav" );
  330. if( gMenu_click == NULL )
  331. {
  332. printf( "Failed to load scratch sound effect! SDL_mixer Error: %s\n", Mix_GetError() );
  333. success = false;
  334. }
  335. gEat = Mix_LoadWAV( "sounds/eat.wav" );
  336. if( gEat == NULL )
  337. {
  338. printf( "Failed to load scratch sound effect! SDL_mixer Error: %s\n", Mix_GetError() );
  339. success = false;
  340. }
  341. gDie = Mix_LoadWAV( "sounds/die.wav" );
  342. if( gDie == NULL )
  343. {
  344. printf( "Failed to load scratch sound effect! SDL_mixer Error: %s\n", Mix_GetError() );
  345. success = false;
  346. }
  347. gOver = Mix_LoadWAV( "sounds/over.wav" );
  348. if( gOver == NULL )
  349. {
  350. printf( "Failed to load scratch sound effect! SDL_mixer Error: %s\n", Mix_GetError() );
  351. success = false;
  352. }
  353. gCongratulation = Mix_LoadWAV( "sounds/congratulation.wav" );
  354. if( gCongratulation == NULL )
  355. {
  356. printf( "Failed to load scratch sound effect! SDL_mixer Error: %s\n", Mix_GetError() );
  357. success = false;
  358. }
  359.  
  360. //load snake
  361. gDau_up = loadTexture( "img/dau_up.png" );
  362. if( gDau_up == NULL )
  363. {
  364. printf( "Failed to load texture image!\n" );
  365. success = false;
  366. }
  367. gDau_down = loadTexture( "img/dau_down.png" );
  368. if( gDau_down == NULL )
  369. {
  370. printf( "Failed to load texture image!\n" );
  371. success = false;
  372. }
  373. gDau_left = loadTexture( "img/dau_left.png" );
  374. if( gDau_left == NULL )
  375. {
  376. printf( "Failed to load texture image!\n" );
  377. success = false;
  378. }
  379. gDau_right = loadTexture( "img/dau_right.png" );
  380. if( gDau_right == NULL )
  381. {
  382. printf( "Failed to load texture image!\n" );
  383. success = false;
  384. }
  385. gKhung = loadTexture( "img/khung.png" );
  386. if( gKhung == NULL )
  387. {
  388. printf( "Failed to load texture image!\n" );
  389. success = false;
  390. }
  391. //rank top
  392. gRank_top_1 = loadTexture( "img/rank_top_1.png" );
  393. if( gRank_top_1 == NULL )
  394. {
  395. printf( "Failed to load texture image!\n" );
  396. success = false;
  397. }
  398. gRank_top_2 = loadTexture( "img/rank_top_2.png" );
  399. if( gRank_top_2 == NULL )
  400. {
  401. printf( "Failed to load texture image!\n" );
  402. success = false;
  403. }
  404. gRank_top_3 = loadTexture( "img/rank_top_3.png" );
  405. if( gRank_top_3 == NULL )
  406. {
  407. printf( "Failed to load texture image!\n" );
  408. success = false;
  409. }
  410. //Load dot
  411. gDot_ngangphai = loadTexture( "img/dot_ngangphai.png" );
  412. if( gDot_ngangphai == NULL )
  413. {
  414. printf( "Failed to load texture image!\n" );
  415. success = false;
  416. }
  417. gDot_ngangtrai = loadTexture( "img/dot_ngangtrai.png" );
  418. if( gDot_ngangtrai == NULL )
  419. {
  420. printf( "Failed to load texture image!\n" );
  421. success = false;
  422. }
  423. gDot_docduoi = loadTexture( "img/dot_docduoi.png" );
  424. if( gDot_docduoi == NULL )
  425. {
  426. printf( "Failed to load texture image!\n" );
  427. success = false;
  428. }
  429. gDot_doctren = loadTexture( "img/dot_doctren.png" );
  430. if( gDot_doctren == NULL )
  431. {
  432. printf( "Failed to load texture image!\n" );
  433. success = false;
  434. }
  435. gDot_left_bot = loadTexture( "img/dot_left_bot.png" );
  436. if( gDot_left_bot == NULL )
  437. {
  438. printf( "Failed to load texture image!\n" );
  439. success = false;
  440. }
  441. gDot_left_top = loadTexture( "img/dot_left_top.png" );
  442. if( gDot_left_top == NULL )
  443. {
  444. printf( "Failed to load texture image!\n" );
  445. success = false;
  446. }
  447. gDot_right_bot = loadTexture( "img/dot_right_bot.png" );
  448. if( gDot_right_bot == NULL )
  449. {
  450. printf( "Failed to load texture image!\n" );
  451. success = false;
  452. }
  453. gDot_right_top = loadTexture( "img/dot_right_top.png" );
  454. if( gDot_right_top == NULL )
  455. {
  456. printf( "Failed to load texture image!\n" );
  457. success = false;
  458. }
  459. gDot_bot_left = loadTexture( "img/dot_bot_left.png" );
  460. if( gDot_bot_left == NULL )
  461. {
  462. printf( "Failed to load texture image!\n" );
  463. success = false;
  464. }
  465. gDot_bot_right = loadTexture( "img/dot_bot_right.png" );
  466. if( gDot_bot_right == NULL )
  467. {
  468. printf( "Failed to load texture image!\n" );
  469. success = false;
  470. }
  471. gDot_top_left = loadTexture( "img/dot_top_left.png" );
  472. if( gDot_top_left == NULL )
  473. {
  474. printf( "Failed to load texture image!\n" );
  475. success = false;
  476. }
  477. gDot_top_right = loadTexture( "img/dot_top_right.png" );
  478. if( gDot_top_right == NULL )
  479. {
  480. printf( "Failed to load texture image!\n" );
  481. success = false;
  482. }
  483.  
  484. gHeart = loadTexture( "img/heart.png" );
  485. if( gHeart == NULL )
  486. {
  487. printf( "Failed to load texture image!\n" );
  488. success = false;
  489. }
  490.  
  491. //Load target
  492. gTarget = loadTexture( "img/target.png" );
  493. if( gTarget == NULL )
  494. {
  495. printf( "Failed to load texture image!\n" );
  496. success = false;
  497. }
  498. gTarget_2 = loadTexture( "img/target_2.png" );
  499. if( gTarget_2 == NULL )
  500. {
  501. printf( "Failed to load texture image!\n" );
  502. success = false;
  503. }
  504.  
  505. gDuoi_docduoi = loadTexture( "img/duoi_docduoi.png");
  506. if( gDuoi_docduoi == NULL )
  507. {
  508. printf( "Failed to load texture image!\n" );
  509. success = false;
  510. }
  511. gDuoi_doctren = loadTexture( "img/duoi_doctren.png");
  512. if( gDuoi_doctren == NULL )
  513. {
  514. printf( "Failed to load texture image!\n" );
  515. success = false;
  516. }
  517. gDuoi_ngangphai = loadTexture( "img/duoi_ngangphai.png");
  518. if( gDuoi_ngangphai == NULL )
  519. {
  520. printf( "Failed to load texture image!\n" );
  521. success = false;
  522. }
  523. gDuoi_ngangtrai = loadTexture( "img/duoi_ngangtrai.png");
  524. if( gDuoi_ngangtrai == NULL )
  525. {
  526. printf( "Failed to load texture image!\n" );
  527. success = false;
  528. }
  529. //Nothing to load
  530. return success;
  531. }
  532.  
  533. void close()
  534. {
  535.  
  536. //Free loaded image
  537. SDL_DestroyTexture( gTexture );
  538. gTexture = NULL;
  539. SDL_DestroyTexture( gTransparent );
  540. gTransparent = NULL;
  541. SDL_DestroyTexture( gKhung );
  542. gKhung = NULL;
  543. SDL_DestroyTexture( gText );
  544. gText = NULL;
  545. SDL_DestroyTexture( gRankList );
  546. gRankList = NULL;
  547. SDL_DestroyTexture( gGameover);
  548. gGameover = NULL;
  549. SDL_DestroyTexture( gPause);
  550. gPause = NULL;
  551. SDL_DestroyTexture( gRank_top_1 );
  552. gRank_top_1 = NULL;
  553. SDL_DestroyTexture( gRank_top_2 );
  554. gRank_top_2 = NULL;
  555. SDL_DestroyTexture( gRank_top_3 );
  556. gRank_top_3 = NULL;
  557. //Free global font
  558. TTF_CloseFont( gFont );
  559. gFont = NULL;
  560. //free menu
  561. SDL_DestroyTexture( gBackground );
  562. gBackground = NULL;
  563. SDL_DestroyTexture( gEffectMo );
  564. gEffectMo = NULL;
  565. SDL_DestroyTexture( gMenu_guide );
  566. gMenu_guide = NULL;
  567. SDL_DestroyTexture( gMenu_play );
  568. gMenu_play = NULL;
  569. SDL_DestroyTexture( gMenu_setting );
  570. gMenu_setting = NULL;
  571. SDL_DestroyTexture( gMenu_quit );
  572. gMenu_quit = NULL;
  573. SDL_DestroyTexture( gMenu_rank );
  574. gMenu_rank = NULL;
  575. //free sounds
  576. Mix_FreeChunk( gMenu_click );
  577. gMenu_click = NULL;
  578. Mix_FreeChunk( gEat );
  579. gEat = NULL;
  580. Mix_FreeChunk( gOver);
  581. gOver = NULL;
  582. Mix_FreeChunk( gDie );
  583. gDie = NULL;
  584. Mix_FreeChunk( gCongratulation );
  585. gCongratulation = NULL;
  586.  
  587. SDL_DestroyTexture( gMenu_guide_choose );
  588. gMenu_guide_choose = NULL ;
  589. SDL_DestroyTexture( gMenu_play_choose );
  590. gMenu_play_choose = NULL;
  591. SDL_DestroyTexture( gMenu_setting_choose );
  592. gMenu_setting_choose = NULL;
  593. SDL_DestroyTexture( gMenu_quit_choose );
  594. gMenu_quit_choose = NULL;
  595. SDL_DestroyTexture( gMenu_rank_choose );
  596. gMenu_rank_choose = NULL;
  597. SDL_DestroyTexture( gExitGame );
  598. gExitGame = NULL;
  599. SDL_DestroyTexture( gInput );
  600. gInput = NULL;
  601. SDL_DestroyTexture( gHeart );
  602. gHeart = NULL;
  603. //
  604. SDL_DestroyTexture( gDot );
  605. gDot = NULL;
  606. SDL_DestroyTexture( gDot_doctren );
  607. gDot_doctren = NULL;
  608. SDL_DestroyTexture( gDot_docduoi );
  609. gDot_docduoi = NULL;
  610. SDL_DestroyTexture( gDot_ngangtrai );
  611. gDot_ngangtrai = NULL;
  612. SDL_DestroyTexture( gDot_ngangphai );
  613. gDot_ngangphai = NULL;
  614. SDL_DestroyTexture( gDot_left_bot );
  615. gDot_left_bot = NULL;
  616. SDL_DestroyTexture( gDot_left_top );
  617. gDot_left_top = NULL;
  618. SDL_DestroyTexture( gDot_right_bot );
  619. gDot_right_bot = NULL;
  620. SDL_DestroyTexture( gDot_right_top );
  621. gDot_right_top = NULL;
  622. SDL_DestroyTexture( gDot_top_left );
  623. gDot_top_left = NULL;
  624. SDL_DestroyTexture( gDot_top_right );
  625. gDot_top_right = NULL;
  626. SDL_DestroyTexture( gDot_bot_left );
  627. gDot_bot_left = NULL;
  628. SDL_DestroyTexture( gDot_bot_right );
  629. gDot_bot_right = NULL;
  630. SDL_DestroyTexture( gDau );
  631. gDau = NULL;
  632. SDL_DestroyTexture( gDau_down );
  633. gDau_down = NULL;
  634. SDL_DestroyTexture( gDau_left );
  635. gDau_left = NULL;
  636. SDL_DestroyTexture( gDau_right );
  637. gDau_right = NULL;
  638. SDL_DestroyTexture( gDau_up );
  639. gDau_up = NULL;
  640. SDL_DestroyTexture( gDuoi );
  641. gDuoi = NULL;
  642. SDL_DestroyTexture( gDuoi_ngangtrai );
  643. gDuoi_ngangtrai = NULL;
  644. SDL_DestroyTexture( gDuoi_ngangphai );
  645. gDuoi_ngangphai = NULL;
  646. SDL_DestroyTexture( gDuoi_doctren );
  647. gDuoi_doctren = NULL;
  648. SDL_DestroyTexture( gDuoi_docduoi );
  649. gDuoi_docduoi = NULL;
  650. SDL_DestroyTexture( gTarget );
  651. gTarget = NULL;
  652. SDL_DestroyTexture( gTarget_2 );
  653. gTarget_2 = NULL;
  654. //Destroy window
  655. SDL_DestroyRenderer( gRenderer );
  656. SDL_DestroyWindow( gWindow );
  657. gWindow = NULL;
  658. gRenderer = NULL;
  659.  
  660. //Quit SDL subsystems
  661. TTF_Quit();
  662. IMG_Quit();
  663. SDL_Quit();
  664. Mix_Quit();
  665. }
  666.  
  667. SDL_Texture* loadTexture( std::string path )
  668. {
  669. //The final texture
  670. SDL_Texture* newTexture = NULL;
  671.  
  672. //Load image at specified path
  673. SDL_Surface* loadedSurface = IMG_Load( path.c_str() );
  674. if( loadedSurface == NULL )
  675. {
  676. printf( "Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError() );
  677. }
  678. else
  679. {
  680. //Create texture from surface pixels
  681. newTexture = SDL_CreateTextureFromSurface( gRenderer, loadedSurface );
  682. if( newTexture == NULL )
  683. {
  684. printf( "Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError() );
  685. }
  686.  
  687. //Get rid of old loaded surface
  688. SDL_FreeSurface( loadedSurface );
  689. }
  690.  
  691. return newTexture;
  692. }
  693.  
  694. char numToChar(int n){
  695. return char(48 + n);
  696. }
  697.  
  698. int charToNum(char c){
  699. return c - 48;
  700. }
  701.  
  702. string numToString(int n){
  703. string s = "";
  704. do{
  705. int tmp = n % 10;
  706. s.insert(0, 1, numToChar(tmp));
  707. n /= 10;
  708. }while(n > 0);
  709. return s;
  710. }
  711.  
  712. void SizeText(int sizetext){
  713. gFont = TTF_OpenFont("koverwatch.ttf", sizetext);
  714. }
  715.  
  716. void vietChu(string text, int posx, int posy, SDL_Color fg){
  717. gSurface = TTF_RenderText_Solid(gFont, text.c_str(), fg);
  718. gText = SDL_CreateTextureFromSurface(gRenderer, gSurface);
  719. SDL_FreeSurface(gSurface);
  720.  
  721. SDL_Rect srcRest;
  722. SDL_Rect desRect;
  723. TTF_SizeText(gFont, text.c_str(), &srcRest.w, &srcRest.h);
  724.  
  725. srcRest.x = 0;
  726. srcRest.y = 0;
  727.  
  728. desRect.x = posx;
  729. desRect.y = posy;
  730.  
  731. desRect.w = srcRest.w;
  732. desRect.h = srcRest.h;
  733. SDL_RenderCopy(gRenderer, gText, &srcRest, &desRect);
  734. }
  735.  
  736. void veKhung(){
  737. //Khung
  738. SDL_Rect khung;
  739. khung.x = 0;
  740. khung.y = 0;
  741. khung.w = SCREEN_WIDTH;
  742. khung.h = SCREEN_HEIGHT;
  743. SDL_RenderSetViewport( gRenderer, &khung );
  744.  
  745. //Render texture to screen
  746. SDL_RenderCopy( gRenderer, gKhung, NULL, NULL );
  747. }
  748. void randomTarget(){
  749. bool check = false;
  750. while(!check){
  751. target.x = (1 + rand() % (SCREEN_WIDTH - snake.kichthuoc) / snake.kichthuoc) * snake.kichthuoc;
  752. target.y = (3 + rand() % (((SCREEN_HEIGHT - snake.kichthuoc) / snake.kichthuoc) - 3 )) * snake.kichthuoc;
  753. target.w = snake.kichthuoc;
  754. target.h = snake.kichthuoc;
  755. for(int i = 0; i < snake.dodai; i++){
  756. if(snake.dot[i].x != target.x || snake.dot[i].y != target.y){
  757. check = true;
  758. break;
  759. }
  760. }
  761. }
  762. }
  763. void khoiTao(){
  764. snake.score = 0;
  765. snake.x = 30;
  766. snake.y = 120;
  767. snake.dodai = 3;
  768. snake.tt = RIGHT;
  769. snake.kichthuoc = 30;
  770. snake.life = 3;
  771. snake.die = false;
  772. //dot[0] == dau
  773. snake.dot[0].x = snake.x + snake.kichthuoc + snake.kichthuoc;
  774. snake.dot[0].y = snake.y;
  775. snake.dot[0].w = snake.kichthuoc;
  776. snake.dot[0].h = snake.kichthuoc;
  777. //dot[1]
  778. snake.dot[1].x = snake.x + snake.kichthuoc;
  779. snake.dot[1].y = snake.y;
  780. snake.dot[1].w = snake.kichthuoc;
  781. snake.dot[1].h = snake.kichthuoc;
  782. //dot[2]
  783. snake.dot[2].x = snake.x;
  784. snake.dot[2].y = snake.y;
  785. snake.dot[2].w = snake.kichthuoc;
  786. snake.dot[2].h = snake.kichthuoc;
  787. randomTarget();
  788. }
  789.  
  790. void hienThi(){
  791. //Clear screen
  792. SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
  793. SDL_RenderClear( gRenderer );
  794. //vekhung
  795. veKhung();
  796.  
  797. string Score = "SCORE ";
  798. SDL_Color fg = { 10, 170, 210 };
  799. int Score_x = 800;
  800. SizeText(40);
  801. vietChu(Score, Score_x, 10, fg);
  802. string score = numToString(snake.score);
  803. vietChu(score, Score_x + 150, 10, fg);
  804. SDL_RenderSetViewport( gRenderer, &target );
  805. //Render texture to screen
  806. target_1 = clock();
  807. if(target_1 % 2 == 0)
  808. SDL_RenderCopy( gRenderer, gTarget, NULL, NULL );
  809. else
  810. SDL_RenderCopy( gRenderer, gTarget_2, NULL, NULL );
  811. SDL_Rect heart[snake.life];
  812. heart[0].x = 25;
  813. heart[0].y = 20;
  814. heart[0].w = 70;
  815. heart[0].h = 45;
  816. for(int i = 1; i < snake.life; i++){
  817. heart[i].x = heart[i - 1].x + 80;
  818. heart[i].y = heart[i - 1].y;
  819. heart[i].w = heart[i - 1].w;
  820. heart[i].h = heart[i - 1].h;
  821. }
  822. for(int i = 0; i < snake.life; i++){
  823. SDL_RenderSetViewport( gRenderer, &heart[i]);
  824. SDL_RenderCopy( gRenderer, gHeart, NULL, NULL);
  825. }
  826. SDL_RenderSetViewport( gRenderer, &snake.dot[0] );
  827. //Render texture to screen
  828. //cai dau
  829.  
  830. if(snake.tt == UP)
  831. gDau = gDau_up;
  832. else if(snake.tt == RIGHT)
  833. gDau = gDau_right;
  834. else if(snake.tt == DOWN)
  835. gDau = gDau_down;
  836. else if(snake.tt == LEFT)
  837. gDau = gDau_left;
  838. SDL_RenderCopy( gRenderer, gDau, NULL, NULL );
  839. for(int i = 1; i < snake.dodai - 1; i++){
  840. SDL_RenderSetViewport( gRenderer, &snake.dot[i] );
  841. if(snake.dot[i].x < snake.dot[i - 1].x && snake.dot[i].y == snake.dot[i - 1].y)
  842. gDot = gDot_ngangphai;
  843. else if(snake.dot[i].x > snake.dot[i - 1].x && snake.dot[i].y == snake.dot[i - 1].y)
  844. gDot = gDot_ngangtrai;
  845. else if(snake.dot[i].x == snake.dot[i - 1].x && snake.dot[i].y > snake.dot[i - 1].y)
  846. gDot = gDot_doctren;
  847. else if(snake.dot[i].x == snake.dot[i - 1].x && snake.dot[i].y < snake.dot[i - 1].y)
  848. gDot = gDot_docduoi;
  849. //Render texture to screen
  850. if(snake.dot[i].x == snake.dot[i - 1].x && snake.dot[i].y == snake.dot[i + 1].y){//dang di ngang
  851. if(snake.dot[i].x > snake.dot[i + 1].x){ // di ngang tu trai sang
  852. if(snake.dot[i].y == 90 && snake.dot[i - 1].y == SCREEN_HEIGHT - snake.kichthuoc)
  853. gDot = gDot_left_top;
  854. else if(snake.dot[i].y == SCREEN_HEIGHT - snake.kichthuoc && snake.dot[i - 1].y == 90)
  855. gDot = gDot_left_bot;
  856. else if(snake.dot[i].y > snake.dot[i - 1].y) //di tu trai len tren
  857. gDot = gDot_left_top;
  858. else if(snake.dot[i].y < snake.dot[i - 1].y)//di tu trai xuong duoi
  859. gDot = gDot_left_bot;
  860.  
  861.  
  862. }
  863. else if(snake.dot[i].x < snake.dot[i + 1].x){//di ngang tu phai sang
  864. if(snake.dot[i].y == 90 && snake.dot[i - 1].y == SCREEN_HEIGHT - snake.kichthuoc)
  865. gDot = gDot_right_top;
  866. else if(snake.dot[i].y == SCREEN_HEIGHT - snake.kichthuoc && snake.dot[i - 1].y == 90)
  867. gDot = gDot_right_bot;
  868. else if(snake.dot[i].y > snake.dot[i - 1].y)// di tu phai len tren
  869. gDot = gDot_right_top;
  870. else if(snake.dot[i].y < snake.dot[i - 1].y)//di tu phai xuong duoi
  871. gDot = gDot_right_bot;
  872. }
  873. }
  874. else if(snake.dot[i].x == snake.dot[i + 1].x && snake.dot[i].y == snake.dot[i - 1].y){//dang di doc
  875. if(snake.dot[i].y > snake.dot[i + 1].y){ // di tu tren xuong
  876. if(snake.dot[i].x == 0 && snake.dot[i - 1].x == SCREEN_WIDTH - snake.kichthuoc)
  877. gDot = gDot_top_left;
  878. else if(snake.dot[i].x == SCREEN_WIDTH - snake.kichthuoc && snake.dot[i - 1].x == 0)
  879. gDot = gDot_top_right;
  880. else if(snake.dot[i].x > snake.dot[i - 1].x)//di tu tren sang trai
  881. gDot = gDot_top_left;
  882. else if(snake.dot[i].x < snake.dot[i - 1].x)//di tu tren sang phai
  883. gDot = gDot_top_right;
  884. }
  885. else if(snake.dot[i].y < snake.dot[i + 1].y){//di tu duoi len
  886. if(snake.dot[i].x == 0 && snake.dot[i - 1].x == SCREEN_WIDTH - snake.kichthuoc)
  887. gDot = gDot_bot_left;
  888. else if(snake.dot[i].x == SCREEN_WIDTH - snake.kichthuoc && snake.dot[i - 1].x == 0)
  889. gDot = gDot_bot_right;
  890. else if(snake.dot[i].x > snake.dot[i - 1].x)//di tu duoi sang trai
  891. gDot = gDot_bot_left;
  892. else if(snake.dot[i].x < snake.dot[i - 1].x)//di tu duoi sang phai
  893. gDot = gDot_bot_right;
  894. }
  895. }
  896. SDL_RenderCopy( gRenderer, gDot, NULL, NULL );
  897. }
  898. //cai duoi
  899. SDL_RenderSetViewport( gRenderer, &snake.dot[snake.dodai - 1]);
  900. if(snake.dot[snake.dodai - 1].x < snake.dot[snake.dodai - 2].x && snake.dot[snake.dodai - 1].y == snake.dot[snake.dodai - 2].y)
  901. gDuoi = gDuoi_ngangphai;
  902. else if(snake.dot[snake.dodai - 1].x > snake.dot[snake.dodai - 2].x && snake.dot[snake.dodai - 1].y == snake.dot[snake.dodai - 2].y)
  903. gDuoi = gDuoi_ngangtrai;
  904. else if(snake.dot[snake.dodai - 1].x == snake.dot[snake.dodai - 2].x && snake.dot[snake.dodai - 1].y > snake.dot[snake.dodai - 2].y)
  905. gDuoi = gDuoi_doctren;
  906. else if(snake.dot[snake.dodai - 1].x == snake.dot[snake.dodai - 2].x && snake.dot[snake.dodai - 1].y < snake.dot[snake.dodai - 2].y)
  907. gDuoi = gDuoi_docduoi;
  908. SDL_RenderCopy( gRenderer, gDuoi, NULL, NULL);
  909. SDL_RenderPresent( gRenderer );
  910. }
  911. void afterDie();
  912. void xuLy(){
  913. //xu ly trang thai len, xuong, trai, phai
  914. for(int i = snake.dodai - 1; i > 0; i--){
  915. snake.dot[i] = snake.dot[i - 1];
  916. }
  917. switch(snake.tt){
  918. case UP:
  919. snake.dot[0].y -= snake.kichthuoc;
  920. break;
  921. case DOWN:
  922. snake.dot[0].y += snake.kichthuoc;
  923. break;
  924. case RIGHT:
  925. snake.dot[0].x += snake.kichthuoc;
  926. break;
  927. case LEFT:
  928. snake.dot[0].x -= snake.kichthuoc;
  929. break;
  930. }
  931. switch(snake.tt){
  932. case RIGHT:
  933. if(e.type == SDL_KEYDOWN){
  934. if(e.key.keysym.sym == SDLK_UP){
  935. snake.tt = UP;
  936. }else if(e.key.keysym.sym == SDLK_DOWN){
  937. snake.tt = DOWN;
  938. }
  939. }
  940. break;
  941. case LEFT:
  942. if(e.type == SDL_KEYDOWN){
  943. if(e.key.keysym.sym == SDLK_UP){
  944. snake.tt = UP;
  945. }else if(e.key.keysym.sym == SDLK_DOWN){
  946. snake.tt = DOWN;
  947. }
  948. }
  949. break;
  950. case UP:
  951. if(e.type == SDL_KEYDOWN){
  952. if(e.key.keysym.sym == SDLK_RIGHT){
  953. snake.tt = RIGHT;
  954. }else if(e.key.keysym.sym == SDLK_LEFT){
  955. snake.tt = LEFT;
  956. }
  957. }
  958. break;
  959. case DOWN:
  960. if(e.type == SDL_KEYDOWN){
  961. if(e.key.keysym.sym == SDLK_RIGHT){
  962. snake.tt = RIGHT;
  963. }else if(e.key.keysym.sym == SDLK_LEFT){
  964. snake.tt = LEFT;
  965. }
  966. }
  967. break;
  968. }
  969. //xu li bien
  970. if(snake.dot[0].x < 0)
  971. snake.dot[0].x = SCREEN_WIDTH - snake.kichthuoc;
  972. else if(snake.dot[0].x >= SCREEN_WIDTH)
  973. snake.dot[0].x = 0;
  974. else if(snake.dot[0].y < 90)
  975. snake.dot[0].y = SCREEN_HEIGHT - snake.kichthuoc;
  976. else if(snake.dot[0].y >= SCREEN_HEIGHT)
  977. snake.dot[0].y = 90;
  978. //xu li an muc tieu
  979. if(snake.dot[0].x == target.x && snake.dot[0].y == target.y){
  980. Mix_PlayChannel( -1, gEat, 0);
  981. snake.dot[snake.dodai] = snake.dot[snake.dodai - 1];
  982. snake.dodai++;
  983. snake.score += 10;
  984. randomTarget();
  985. }
  986. //xu li chet
  987. for(int i = 1; i < snake.dodai; i++){
  988. if(snake.dot[0].x == snake.dot[i].x && snake.dot[0].y == snake.dot[i].y){
  989. snake.life--;
  990. if(snake.life == 0)
  991. {
  992. snake.die = true;
  993. break;
  994. }
  995.  
  996. Mix_PlayChannel( -1, gEat, 0);
  997.  
  998. snake.dodai = i;
  999. }
  1000. }
  1001.  
  1002.  
  1003. }
  1004.  
  1005. void exitGame(){
  1006. bool check = false;
  1007. SDL_Rect rect_Exit;
  1008. rect_Exit.x = 0;
  1009. rect_Exit.y = 0;
  1010. rect_Exit.h = SCREEN_HEIGHT;
  1011. rect_Exit.w = SCREEN_WIDTH;
  1012. SDL_Rect rect_Ef;
  1013. rect_Ef.x = 0;
  1014. rect_Ef.y = 0;
  1015. rect_Ef.h = SCREEN_HEIGHT;
  1016. rect_Ef.w = SCREEN_WIDTH;
  1017. //SDL_RenderSetViewport( gRenderer, &rect_Ef );
  1018. //SDL_RenderCopy( gRenderer, gEffectMo, NULL, NULL );
  1019. SDL_RenderSetViewport( gRenderer, &rect_Exit );
  1020. SDL_RenderCopy( gRenderer, gExitGame, NULL, NULL );
  1021. SDL_RenderPresent( gRenderer );
  1022. //Render texture to screen
  1023. while(!quit && !check)
  1024. {
  1025. //User requests quit
  1026. while( SDL_PollEvent( &e ) != 0 )
  1027. {
  1028. //User requests quit
  1029. if( e.type == SDL_QUIT )
  1030. {
  1031. quit = true;
  1032. }
  1033. SDL_GetMouseState( &mouse_x, &mouse_y );
  1034. if(mouse_x >= 325 && mouse_x <= 470 && mouse_y >= 365 && mouse_y <= 395){
  1035. if( SDL_GetMouseState ( NULL , NULL ) & SDL_BUTTON(SDL_BUTTON_LEFT )){
  1036. Mix_PlayChannel( -1, gMenu_click, 0);
  1037. quit = true;
  1038. close();
  1039. exit(1);
  1040. }
  1041. }
  1042. if(e.key.keysym.sym == SDLK_RETURN){
  1043. Mix_PlayChannel( -1, gMenu_click, 0);
  1044. quit = true;
  1045. close();
  1046. exit(1);
  1047. }
  1048. if(e.key.keysym.sym == SDLK_ESCAPE){
  1049. Mix_PlayChannel( -1, gMenu_click, 0);
  1050. check = true;
  1051. break;
  1052. }
  1053. if(mouse_x >= 725 && mouse_x <= 870 && mouse_y >= 365 && mouse_y <= 395){
  1054. if( SDL_GetMouseState ( NULL , NULL ) & SDL_BUTTON(SDL_BUTTON_LEFT )){
  1055. Mix_PlayChannel( -1, gMenu_click, 0);
  1056. check = true;
  1057. break;
  1058. }
  1059. }
  1060. }
  1061.  
  1062. }
  1063. }
  1064.  
  1065. void rankList(){
  1066. bool quit1 = false;
  1067. for(int i = 0; i < rankListMax - 1; i++){
  1068. for(int j = i + 1; j < rankListMax; j++){
  1069. if(rankPerson[i].score < rankPerson[j].score){
  1070. RankPerson tmp = rankPerson[i];
  1071. rankPerson[i] = rankPerson[j];
  1072. rankPerson[j] = tmp;
  1073. }
  1074. }
  1075. }
  1076. rank_out.open("rank/rank.txt", ios :: out);
  1077. int tmp = 0;
  1078. while(tmp < rankListMax){
  1079. rank_out << rankPerson[tmp].score << rankPerson[tmp].name << endl ;
  1080. tmp++;
  1081. }
  1082. //rank_out << rankPerson[tmp].name;
  1083. rank_out.close();
  1084. SDL_Rect rect_rank;
  1085.  
  1086. rect_rank.x = 0;
  1087. rect_rank.y = 0;
  1088. rect_rank.w = SCREEN_WIDTH;
  1089. rect_rank.h = SCREEN_HEIGHT;
  1090.  
  1091. SizeText(30);
  1092. SDL_Color fgstt{223, 242, 233};
  1093. int posxstt = 350;
  1094. int posystt = 150;
  1095. int stt = 4;
  1096.  
  1097. //rank_top
  1098. SDL_Rect rect_rank_top_1;
  1099.  
  1100. rect_rank_top_1.x = posxstt - 10;
  1101. rect_rank_top_1.y = posystt;
  1102. rect_rank_top_1.w = 30;
  1103. rect_rank_top_1.h = 30;
  1104. SDL_Rect rect_rank_top_2;
  1105.  
  1106. rect_rank_top_2.x = posxstt - 10;
  1107. rect_rank_top_2.y = posystt + 45;
  1108. rect_rank_top_2.w = 30;
  1109. rect_rank_top_2.h = 30;
  1110.  
  1111. SDL_Rect rect_rank_top_3;
  1112.  
  1113. rect_rank_top_3.x = posxstt - 10;
  1114. rect_rank_top_3.y = posystt + 90;
  1115. rect_rank_top_3.w = 30;
  1116. rect_rank_top_3.h = 30;
  1117. posystt += 135;
  1118.  
  1119. while(!quit && !quit1){
  1120. while(SDL_PollEvent( &e) != 0){
  1121. if(e.type == SDL_QUIT){
  1122. exitGame();
  1123. }
  1124. if(e.key.keysym.sym == SDLK_ESCAPE){
  1125. quit1 = true;
  1126. break;
  1127. }else{
  1128. SDL_GetMouseState( &mouse_x, &mouse_y );
  1129. if(mouse_x >= 870 && mouse_x <= 900 && mouse_y >= 70 && mouse_y <= 100){
  1130. if(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT)){
  1131. Mix_PlayChannel( -1, gMenu_click, 0 );
  1132. quit1 = true;
  1133. break;
  1134. }
  1135. }
  1136. }
  1137. }
  1138. SDL_RenderSetViewport( gRenderer, &rect_rank);
  1139. SDL_RenderCopy( gRenderer, gRankList, NULL, NULL);
  1140. SDL_RenderSetViewport( gRenderer, &rect_rank_top_1);
  1141. SDL_RenderCopy( gRenderer, gRank_top_1, NULL, NULL);
  1142. SDL_RenderSetViewport( gRenderer, &rect_rank_top_2);
  1143. SDL_RenderCopy( gRenderer, gRank_top_2, NULL, NULL);
  1144. SDL_RenderSetViewport( gRenderer, &rect_rank_top_3);
  1145. SDL_RenderCopy( gRenderer, gRank_top_3, NULL, NULL);
  1146. SDL_RenderSetViewport( gRenderer, &rect_rank);
  1147. for(int i = 0; i < rankListMax; i++){
  1148. string s = numToString(stt);
  1149. vietChu(s, posxstt, posystt, fgstt);
  1150. posystt += 45;
  1151. stt++;
  1152. if(stt == rankListMax + 1)
  1153. stt = 4;
  1154. if(posystt == 600)
  1155. posystt = 150 + 135;
  1156. }
  1157. SDL_Color fgname[rankListMax];
  1158. fgname[0] = { 251, 253, 43};
  1159. fgname[1] = { 146, 246, 220};
  1160. fgname[2] = { 49, 12, 26};
  1161. for(int i = 3; i < rankListMax; i++){
  1162. fgname[i] = { 223, 242, 233};
  1163. }
  1164. int posyname = 150;
  1165. int posxname = 400;
  1166. for(int i = 0; i < rankListMax; i++){
  1167. string s = rankPerson[i].name;
  1168. vietChu(s, posxname , posyname, fgname[i]);
  1169. posyname += 45;
  1170. if(posyname == 600)
  1171. posyname = 150;
  1172. }
  1173. int posyscore = 150;
  1174. int posxscore = 800;
  1175.  
  1176. for(int i = 0; i < rankListMax; i++){
  1177. string s = numToString(rankPerson[i].score);
  1178. vietChu(s, posxscore , posyscore, fgstt);
  1179. posyscore += 45;
  1180. if(posyscore == 600)
  1181. posyscore = 150;
  1182. }
  1183. SDL_RenderPresent( gRenderer );
  1184. }
  1185.  
  1186. }
  1187. void playAGame();
  1188.  
  1189. void afterDie(){
  1190. Mix_PlayChannel( -1, gDie, 0);
  1191. SDL_Rect after;
  1192. after.x = 0;
  1193. after.y = 0;
  1194. after.w = SCREEN_WIDTH;
  1195. after.h = SCREEN_HEIGHT;
  1196. bool high = false;
  1197. if(snake.score > rankPerson[rankListMax - 1].score)
  1198. high = true;
  1199. if(high){
  1200. Mix_PlayChannel( -1, gCongratulation, 0);
  1201. int posHigh;
  1202. string name = " ";
  1203. bool quit1 = false;
  1204. for(int i = rankListMax - 1; i >= 0; i--){
  1205. if(rankPerson[i].score < snake.score)
  1206. posHigh = i;
  1207. if(rankPerson[i].score > snake.score)
  1208. break;
  1209. }
  1210. SDL_StartTextInput();
  1211. while(!quit && !quit1){
  1212.  
  1213. while(SDL_PollEvent( &e) != 0){
  1214. if(e.type == SDL_QUIT){
  1215. exitGame();
  1216. }else if( e.type == SDL_KEYDOWN){
  1217. if( e.key.keysym.sym == SDLK_BACKSPACE && name.length() > 0 ){
  1218. //lop off character
  1219. name.erase(name.length() - 1, 1);
  1220. }
  1221. else if( e.key.keysym.sym == SDLK_c && SDL_GetModState() & KMOD_CTRL )
  1222. {
  1223. SDL_SetClipboardText( name.c_str() );
  1224. }
  1225. //Handle paste
  1226. else if( e.key.keysym.sym == SDLK_v && SDL_GetModState() & KMOD_CTRL )
  1227. {
  1228. name = SDL_GetClipboardText();
  1229. }
  1230. }
  1231. else if( e.type == SDL_TEXTINPUT )
  1232. {
  1233. //Not copy or pasting
  1234. if( !( ( e.text.text[ 0 ] == 'c' || e.text.text[ 0 ] == 'C' ) && ( e.text.text[ 0 ] == 'v' || e.text.text[ 0 ] == 'V' ) && SDL_GetModState() & KMOD_CTRL ) )
  1235. {
  1236. //Append character
  1237. name += e.text.text;
  1238. }
  1239. }
  1240. if(e.key.keysym.sym == SDLK_RETURN){
  1241. quit1 = true;
  1242. }
  1243. else {
  1244. SDL_GetMouseState( &mouse_x, &mouse_y);
  1245. if(mouse_x >= 580 && mouse_x <= 620 && mouse_y >= 415 && mouse_y <= 505){
  1246. if(e.type = SDL_GetMouseState( NULL, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT)){
  1247. Mix_PlayChannel( - 1, gMenu_click, 0);
  1248. quit1 = true;
  1249. }
  1250. }
  1251. }
  1252. }
  1253. SDL_RenderSetViewport( gRenderer, &after);
  1254. SDL_RenderCopy( gRenderer, gInput, NULL, NULL);
  1255. SizeText(50);
  1256. SDL_Color fg = { 223, 242, 233};
  1257. string s = numToString(snake.score);
  1258. int posx = 550;
  1259. int posy = 315;
  1260. if(snake.score < 100)
  1261. posx += 30;
  1262. else if(snake.score < 1000)
  1263. posx += 20;
  1264. vietChu(s, posx, posy, fg);
  1265. SizeText(20);
  1266. SDL_RenderSetViewport( gRenderer, &after);
  1267. vietChu(name, 525, 442, fg);
  1268. SDL_RenderPresent( gRenderer);
  1269. }
  1270. SDL_StopTextInput();
  1271. for(int i = rankListMax - 1; i > posHigh; i--){
  1272. rankPerson[i] = rankPerson[i - 1];
  1273. }
  1274. rankPerson[posHigh].score = snake.score;
  1275. rankPerson[posHigh].name = name;
  1276. rankList();
  1277. }else{
  1278. Mix_PlayChannel( -1, gOver, 0);
  1279. bool quit1 = false;
  1280. while(!quit && !quit1){
  1281. while(SDL_PollEvent(&e) != 0){
  1282. if(e.type == SDL_QUIT)
  1283. exitGame();
  1284. SDL_GetMouseState( &mouse_x, &mouse_y);
  1285. if(mouse_x >= 490 && mouse_x <= 710){
  1286. if(mouse_y >= 442 && mouse_y <= 478){
  1287. if(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT))
  1288. {
  1289. Mix_PlayChannel( -1, gMenu_click, 0);
  1290. playAGame();
  1291. }
  1292. }
  1293. if(mouse_y >= 497 && mouse_y <= 534){
  1294. if(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT))
  1295. {
  1296.  
  1297. Mix_PlayChannel( -1, gMenu_click, 0);
  1298. quit1 = true;
  1299. }
  1300. }
  1301. }
  1302. }
  1303. SDL_RenderSetViewport( gRenderer, &after);
  1304. SDL_RenderCopy( gRenderer, gGameover, NULL, NULL);
  1305. SDL_Color fg = { 223, 242, 233};
  1306. SizeText(50);
  1307. string s = numToString(snake.score);
  1308. int posx = 550;
  1309. int posy = 350;
  1310. if(snake.score < 100)
  1311. posx += 30;
  1312. else if(snake.score < 1000)
  1313. posx += 20;
  1314. vietChu(s, posx, posy, fg);
  1315. SDL_RenderPresent( gRenderer);
  1316. }
  1317.  
  1318. }
  1319.  
  1320. }
  1321.  
  1322. void pause(){
  1323. bool check1 = false;
  1324. while(!quit && !check1 && !returnmenu){
  1325. while(SDL_PollEvent( &e) != 0){
  1326. if(e.type == SDL_QUIT){
  1327. exitGame();
  1328. }
  1329. SDL_GetMouseState( &mouse_x, &mouse_y);
  1330. if(mouse_x >= 500 && mouse_x <= 700 && mouse_y >= 280 && mouse_y <= 333){
  1331. if(SDL_GetMouseState( NULL, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT)){
  1332. Mix_PlayChannel( -1, gMenu_click, 0);
  1333. check1 = true;
  1334. break;
  1335. }
  1336. }
  1337. if(mouse_x >= 500 && mouse_x <= 700 && mouse_y >= 356 && mouse_y <= 408){
  1338. if(SDL_GetMouseState( NULL, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT)){
  1339. Mix_PlayChannel( -1, gMenu_click, 0);
  1340. returnmenu = true;
  1341. break;
  1342. }
  1343. }
  1344. }
  1345. SDL_Rect rect_pause;
  1346. rect_pause.x = 0;
  1347. rect_pause.y = 0;
  1348. rect_pause.w = SCREEN_WIDTH;
  1349. rect_pause.h = SCREEN_HEIGHT;
  1350. SDL_RenderSetViewport( gRenderer, &rect_pause);
  1351. SDL_RenderCopy( gRenderer, gPause, NULL, NULL);
  1352. SDL_RenderPresent( gRenderer);
  1353. }
  1354. }
  1355.  
  1356. void playAGame(){
  1357. khoiTao();
  1358. //Event handler
  1359. //While application is running
  1360. while( !quit )
  1361. {
  1362. //Handle events on queue
  1363. while( SDL_PollEvent( &e ) != 0 )
  1364. {
  1365. //User requests quit
  1366. if( e.type == SDL_QUIT )
  1367. {
  1368. exitGame();
  1369. }
  1370. SDL_GetMouseState( &mouse_x, &mouse_y);
  1371. if(mouse_x >= 1116 && mouse_x <= 1455 && mouse_y >= 30 && mouse_y <= 70){
  1372. if(SDL_GetMouseState( NULL, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT)){
  1373. pause();
  1374. }
  1375. }
  1376. }
  1377.  
  1378. hienThi();
  1379. xuLy();
  1380. if(snake.die)
  1381. break;
  1382. if(returnmenu)
  1383. break;
  1384. Sleep(70);
  1385.  
  1386. }
  1387. if(snake.die)
  1388. afterDie();
  1389. }
  1390.  
  1391.  
  1392. void Menu(){
  1393. //Menu
  1394. SDL_Rect menu;
  1395. menu.x = 0;
  1396. menu.y = 0;
  1397. menu.w = SCREEN_WIDTH;
  1398. menu.h = SCREEN_HEIGHT;
  1399. //play
  1400. SDL_Rect menu_play;
  1401. menu_play.x = 30;
  1402. menu_play.y = 250;
  1403. menu_play.w = 300;
  1404. menu_play.h = 100;
  1405.  
  1406. SDL_Rect menu_play_choose;
  1407. menu_play_choose.x = 70;
  1408. menu_play_choose.y = 250;
  1409. menu_play_choose.w = 300;
  1410. menu_play_choose.h = 100;
  1411. //setting
  1412. SDL_Rect menu_setting;
  1413. menu_setting.x = 30;
  1414. menu_setting.y = 400;
  1415. menu_setting.w = 148;
  1416. menu_setting.h = 50;
  1417.  
  1418. SDL_Rect menu_setting_choose;
  1419. menu_setting_choose.x = 25;
  1420. menu_setting_choose.y = 395;
  1421. menu_setting_choose.w = 148;
  1422. menu_setting_choose.h = 50;
  1423. //rank
  1424. SDL_Rect menu_rank;
  1425. menu_rank.x = 30;
  1426. menu_rank.y = 470;
  1427. menu_rank.w = 120;
  1428. menu_rank.h = 50;
  1429.  
  1430. SDL_Rect menu_rank_choose;
  1431. menu_rank_choose.x = 25;
  1432. menu_rank_choose.y = 465;
  1433. menu_rank_choose.w = 120;
  1434. menu_rank_choose.h = 50;
  1435. //guide
  1436. SDL_Rect menu_guide;
  1437. menu_guide.x = 30;
  1438. menu_guide.y = 550;
  1439. menu_guide.w = 300;
  1440. menu_guide.h = 50;
  1441.  
  1442. SDL_Rect menu_guide_choose;
  1443. menu_guide_choose.x = 25;
  1444. menu_guide_choose.y = 545;
  1445. menu_guide_choose.w = 300;
  1446. menu_guide_choose.h = 50;
  1447. //quit
  1448. SDL_Rect menu_quit;
  1449. menu_quit.x = 30;
  1450. menu_quit.y = 620;
  1451. menu_quit.w = 100;
  1452. menu_quit.h = 50;
  1453.  
  1454. SDL_Rect menu_quit_choose;
  1455. menu_quit_choose.x = 25;
  1456. menu_quit_choose.y = 615;
  1457. menu_quit_choose.w = 100;
  1458. menu_quit_choose.h = 50;
  1459. bool check1 = false;
  1460. while(!quit){
  1461. while( SDL_PollEvent( &e ) != 0 )
  1462. {
  1463. if( e.type == SDL_QUIT )
  1464. {
  1465. exitGame();
  1466. }
  1467. //User requests quit if( e.type == SDL_MOUSEMOTION )
  1468.  
  1469. SDL_GetMouseState( &mouse_x, &mouse_y );
  1470.  
  1471.  
  1472. if(mouse_x <= 330 && mouse_y <= 350 && mouse_x >= 30 && mouse_y >= 250){
  1473. if( SDL_GetMouseState ( NULL , NULL ) & SDL_BUTTON(SDL_BUTTON_LEFT)){
  1474. Mix_PlayChannel( -1, gMenu_click, 0 );
  1475. playAGame();
  1476. }
  1477. }
  1478. if(mouse_x <= 110 && mouse_y <= 510 && mouse_x >= 30 && mouse_y >= 460){
  1479. if( SDL_GetMouseState( NULL, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT)){
  1480. Mix_PlayChannel( -1, gMenu_click, 0);
  1481. rankList();
  1482. }
  1483. }
  1484. if(mouse_x <= 90 && mouse_y <= 650 && mouse_x >= 30 && mouse_y >= 610){
  1485. if( SDL_GetMouseState ( NULL , NULL ) & SDL_BUTTON(SDL_BUTTON_LEFT)){
  1486. Mix_PlayChannel( -1, gMenu_click, 0 );
  1487. exitGame();
  1488. }
  1489. }
  1490. if(e.key.keysym.sym == SDLK_ESCAPE){
  1491. Mix_PlayChannel( -1, gMenu_click, 0);
  1492. check1 = true;
  1493. break;
  1494. }
  1495. }
  1496. SDL_RenderSetViewport( gRenderer, &menu );
  1497. //Render texture to screen
  1498. //play
  1499. SDL_RenderCopy( gRenderer, gBackground, NULL, NULL );
  1500. if(mouse_x <= 330 && mouse_y <= 350 && mouse_x >= 30 && mouse_y >= 250){
  1501. SDL_RenderSetViewport( gRenderer, &menu_play_choose );
  1502. //Render texture to screen
  1503. SDL_RenderCopy( gRenderer, gMenu_play_choose, NULL, NULL );
  1504. }else{
  1505. SDL_RenderSetViewport( gRenderer, &menu_play );
  1506. //Render texture to screen
  1507. SDL_RenderCopy( gRenderer, gMenu_play, NULL, NULL );
  1508. }
  1509. //setting
  1510. if(mouse_x <= 140 && mouse_y <= 450 && mouse_x >= 30 && mouse_y >= 400){
  1511. SDL_RenderSetViewport( gRenderer, &menu_setting_choose );
  1512. //Render texture to screen
  1513. SDL_RenderCopy( gRenderer, gMenu_setting_choose, NULL, NULL );
  1514. }else{
  1515. SDL_RenderSetViewport( gRenderer, &menu_setting );
  1516. //Render texture to screen
  1517. SDL_RenderCopy( gRenderer, gMenu_setting, NULL, NULL );
  1518. }
  1519. //rank
  1520. if(mouse_x <= 110 && mouse_y <= 510 && mouse_x >= 30 && mouse_y >= 460){
  1521. SDL_RenderSetViewport( gRenderer, &menu_rank_choose );
  1522. //Render texture to screen
  1523. SDL_RenderCopy( gRenderer, gMenu_rank_choose, NULL, NULL );
  1524. }else{
  1525. SDL_RenderSetViewport( gRenderer, &menu_rank );
  1526. //Render texture to screen
  1527. SDL_RenderCopy( gRenderer, gMenu_rank, NULL, NULL );
  1528. }
  1529. //guide
  1530. if(mouse_x <= 240 && mouse_y <= 590 && mouse_x >= 50 && mouse_y >= 540){
  1531. SDL_RenderSetViewport( gRenderer, &menu_guide_choose );
  1532. //Render texture to screen
  1533. SDL_RenderCopy( gRenderer, gMenu_guide_choose, NULL, NULL );
  1534. }else{
  1535. SDL_RenderSetViewport( gRenderer, &menu_guide );
  1536. //Render texture to screen
  1537. SDL_RenderCopy( gRenderer, gMenu_guide, NULL, NULL );
  1538. }
  1539. //SDL_RenderPresent( gRenderer );
  1540. //quit
  1541. if(mouse_x <= 90 && mouse_y <= 650 && mouse_x >= 30 && mouse_y >= 610){
  1542. SDL_RenderSetViewport( gRenderer, &menu_quit_choose );
  1543. //Render texture to screen
  1544. SDL_RenderCopy( gRenderer, gMenu_quit_choose, NULL, NULL );
  1545. }else{
  1546. SDL_RenderSetViewport( gRenderer, &menu_quit );
  1547. //Render texture to screen
  1548. SDL_RenderCopy( gRenderer, gMenu_quit, NULL, NULL );
  1549. }
  1550. SDL_RenderPresent( gRenderer );
  1551. }
  1552. //SDL_RenderPresent( gRenderer );
  1553. }
  1554.  
  1555.  
  1556. int main( int argc, char* args[] )
  1557. {
  1558. srand(time(NULL));
  1559. //Start up SDL and create window
  1560. if( !init() )
  1561. {
  1562. printf( "Failed to initialize!\n" );
  1563. }
  1564. else
  1565. {
  1566. //Load media
  1567. if( !loadMedia() )
  1568. {
  1569. printf( "Failed to load media!\n" );
  1570. }
  1571. else
  1572. {
  1573. //Main loop flag
  1574. while(!quit)
  1575. Menu();
  1576. }
  1577. }
  1578.  
  1579. //Free resources and close SDL
  1580. close();
  1581.  
  1582. return 0;
  1583. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement