Advertisement
Guest User

dsada

a guest
Jun 24th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <stdio.h>
  4. #include <dos.h>
  5. #include <Windows.h>
  6. #include <string.h>
  7. #include <conio.h>
  8. #include <cstdlib>
  9. #include <ctime>
  10. #include <thread>
  11. #include "function.h"
  12. #define MAX_SIZE_SNAKE 100
  13. #define MAX_SIZE_FOOD 4
  14. #define MAX_SPEED 3
  15. #define POINT_EACH_FOOD 10
  16. #define TOCDO 100
  17. using namespace std;
  18. // global variables
  19. POINT snake[MAX_SIZE_SNAKE];
  20. POINT food[MAX_SIZE_FOOD];
  21. POINT porch[5] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } };
  22. int CHAR_LOCK;
  23. int MOVING;
  24. int SPEED;
  25. int HEIGH_CONSOLE, WIDTH_CONSOLE;
  26. int FOOD_INDEX;
  27. int SIZE_SNAKE;
  28. int STATE;
  29. int x_coordinate, y_coordinate;
  30. int LEVEL = 1, SCORE;
  31. char name[50], ID[50], filename[50];
  32. int len, apple, initlen;
  33. char temp;
  34. int idx;
  35.  
  36. void DrawPorch(char c)
  37. {
  38. for (int i = 0; i < 5; i++)
  39. {
  40. gotoxy(porch[i].x, porch[i].y);
  41. printf("%c", c);
  42. }
  43. if (c == ' ')
  44. memset(porch, 0, sizeof(porch));
  45. }
  46. bool IsValid(int x, int y)
  47. {
  48. for (int i = 0; i < SIZE_SNAKE; i++)
  49. if (snake[i].x == x && snake[i].y == y)
  50. return false;
  51. return true;
  52. }
  53. bool isFoodPosition(int x, int y)
  54. {
  55. for (int i = 0; i < MAX_SIZE_FOOD; i++)
  56. {
  57. if (x == food[i].x && y == food[i].y)
  58. return true;
  59. }
  60. return false;
  61. }
  62. bool isVaLid_PorchPostiton(int x, int y)
  63. {
  64. if (!IsValid(x - 1, y - 1) || !IsValid(x, y - 1) || !IsValid(x + 1, y - 1)
  65. || !IsValid(x - 1, y) || !IsValid(x + 1, y))
  66. return false;
  67. return true;
  68. }
  69. void GenerateFood()
  70. {
  71. int x, y;
  72. for (int i = 0; i < MAX_SIZE_FOOD; i++)
  73. {
  74. do
  75. {
  76. x = x_coordinate + rand() % (WIDTH_CONSOLE - 4) + 2;
  77. y = y_coordinate + rand() % (HEIGH_CONSOLE - 4) + 2;
  78.  
  79. } while (IsValid(x, y) == false);
  80. food[i] = { x, y };
  81. }
  82. }
  83. // tao cong
  84. void CreatePorch()
  85. {
  86. int x, y;
  87. for (int i = 0;; i++)
  88. {
  89. x = x_coordinate + rand() % (WIDTH_CONSOLE - 6) + 2;
  90. y = y_coordinate + rand() % (HEIGH_CONSOLE - 6) + 2;
  91. if (IsValid(x - 1, y - 1) && IsValid(x, y - 1) && IsValid(x + 1, y - 1) && IsValid(x - 1, y) && IsValid(x + 1, y))
  92. {
  93. if (!isFoodPosition(x - 1, y - 1) && !isFoodPosition(x, y - 1) && !isFoodPosition(x + 1, y - 1) && !isFoodPosition(x-1,y)
  94. && !isFoodPosition(x+1,y))
  95. break;
  96. }
  97. }
  98. porch[0] = { x - 1, y };
  99. porch[1] = { x - 1, y - 1 };
  100. porch[2] = { x, y - 1 };
  101. porch[3] = { x + 1, y - 1 };
  102. porch[4] = { x + 1, y };
  103. }
  104. void ResetData()
  105. {
  106. // khoi tao gia tri toan cuc
  107. CHAR_LOCK = 'A', MOVING = 'D', SPEED = 1; FOOD_INDEX = 0, WIDTH_CONSOLE = 70, HEIGH_CONSOLE = 20, SIZE_SNAKE = 6;
  108. apple = 0; SCORE = 0; LEVEL = 1; idx = 0, len = initlen;
  109. // khoi tao gia tri mac dinh cho snake
  110. snake[0] = { 10+x_coordinate, 5+y_coordinate }; snake[1] = { 11+x_coordinate, 5+y_coordinate };
  111. snake[2] = { 12+x_coordinate, 5+y_coordinate }; snake[3] = { 13+x_coordinate, 5+y_coordinate };
  112. snake[4] = { 14+x_coordinate, 5+y_coordinate }; snake[5] = { 15+x_coordinate, 5+y_coordinate };
  113. GenerateFood();
  114. // tao mang thuc an food
  115. }
  116. void StartGame()
  117. {
  118. system("cls"); // xoa man hinh
  119. TextColor(9);
  120. DrawBoard(x_coordinate, y_coordinate, WIDTH_CONSOLE, HEIGH_CONSOLE);
  121. TextColor(14);
  122. gotoxy(x_coordinate+25, 1);
  123. printf("***** SNAKE GAME *****");
  124. gotoxy(x_coordinate + 18, 2);
  125. printf("------------------------------------");
  126. gotoxy(x_coordinate + WIDTH_CONSOLE - 10, 3);
  127. printf("LEVEL: %d", LEVEL);
  128. gotoxy(x_coordinate + WIDTH_CONSOLE - 10, 4);
  129. printf("SCORE: %d", SCORE);
  130. gotoxy(x_coordinate, 4);
  131. printf("SNAKE: %s", ID);
  132. gotoxy(x_coordinate + WIDTH_CONSOLE + 3, 5);
  133. printf("\t\t\tLuat choi");
  134. gotoxy(x_coordinate + WIDTH_CONSOLE + 3, 6);
  135. printf("%c 'W'-Len 'S'-Xuong 'A'-Trai 'D'-Phai", 254);
  136. gotoxy(x_coordinate + WIDTH_CONSOLE + 3, 7);
  137. printf("%c 'P'-Dung game 'Esc'-Thoat game", 254);
  138. gotoxy(x_coordinate + WIDTH_CONSOLE + 3, 8);
  139. printf("%c 'L'-Luu Game 'T'-Tai Game", 254);
  140. gotoxy(x_coordinate + WIDTH_CONSOLE + 3, 9);
  141. printf("%c Khi an het 4 thuc an se xuat hien 1 cong", 254);
  142. gotoxy(x_coordinate + WIDTH_CONSOLE + 3, 10);
  143. printf("SNAKE quay ve va tang cap do va toc do");
  144. TextColor(230);
  145. if (porch[0].x != 0 && porch[0].y != 0)
  146. {
  147. char c = (char)176;
  148. DrawPorch(c);
  149. }
  150. TextColor(7);
  151. STATE = 1; // bat dau cho thread chay
  152. }
  153. bool checktouchPorch() // kiem tra xem co dung cong khong
  154. {
  155. for (int i = 0; i < SIZE_SNAKE; i++)
  156. {
  157. for (int j = 0; j < 5; j++)
  158. {
  159. if (porch[j].x == snake[i].x && porch[j].y == snake[i].y)
  160. return true;
  161. }
  162. }
  163. return false;
  164. }
  165. void ExitGame(HANDLE t)
  166. {
  167. system("cls");
  168. TerminateThread(t, 0);
  169.  
  170. }
  171. void PauseGame(HANDLE t)
  172. {
  173. SuspendThread(t);
  174. }
  175.  
  176. void Eat()
  177. {
  178. apple++;
  179. // xu ly chuoi snack la MSSV
  180. name[len] = name[idx++];
  181. len++;
  182. name[len] = '\0';
  183. snake[SIZE_SNAKE] = food[FOOD_INDEX];
  184. SIZE_SNAKE++;
  185. if (FOOD_INDEX == MAX_SIZE_FOOD - 1)
  186. {
  187. FOOD_INDEX = 0;
  188. GenerateFood();
  189. CreatePorch();
  190. TextColor(230);
  191. char c = (char)176;
  192. DrawPorch(c);
  193. TextColor(7);
  194. }
  195. else
  196. FOOD_INDEX++;
  197. // updates score and level
  198. SCORE += POINT_EACH_FOOD;
  199. gotoxy(x_coordinate + WIDTH_CONSOLE - 3, 4);
  200. printf("%d", SCORE);
  201. }
  202. void DrawSnakeAndFood(char* str)
  203. {
  204. TextColor(7);
  205. gotoxy(food[FOOD_INDEX].x, food[FOOD_INDEX].y);
  206. if (strcmp(str, " ") == 0 || (porch[0].x != 0 && porch[0].y != 0))
  207. printf(" ");
  208. else
  209. printf("@");
  210. for (int i = 0; i < SIZE_SNAKE; i++)
  211. {
  212. gotoxy(snake[i].x, snake[i].y);
  213. if (i == SIZE_SNAKE - 1)
  214. TextColor(12);
  215. printf("%c", str[i]);
  216. }
  217. }
  218. void ProcessDead() {
  219. idx = 0;
  220. len = initlen;
  221. STATE = 0;
  222. gotoxy(35, 15);
  223. printf("GAME OVER!");
  224. int i = 1;
  225. while (i <= 15)
  226. {
  227. TextColor(i++);
  228. for (int j = 0; j < SIZE_SNAKE; j++)
  229. {
  230. gotoxy(snake[j].x, snake[j].y);
  231. printf("?");
  232. }
  233. Sleep(50);
  234. i++;
  235. }
  236. TextColor(7);
  237. gotoxy(0, HEIGH_CONSOLE + y_coordinate + 1);
  238. printf(" Nhan phim 'Y' de tiep tuc game ... hoac nhan Esc de thoat game.");
  239. memset(porch, 0, sizeof(porch));
  240. }
  241. bool touchItsBody() // Kiem tra Ran co cham vao than hoac duoi
  242. {
  243. for (int i = 0; i < SIZE_SNAKE-1; i++)
  244. {
  245. if (snake[SIZE_SNAKE - 1].x == snake[i].x
  246. && snake[SIZE_SNAKE - 1].y == snake[i].y)
  247. return true;
  248. }
  249. return false;
  250. }
  251. void MoveRight() {
  252. if (checktouchPorch() ||touchItsBody() ||snake[SIZE_SNAKE - 1].x + 1 == WIDTH_CONSOLE+x_coordinate)
  253. {
  254. ProcessDead();
  255. }
  256. else {
  257. if (porch[0].x == 0 && porch[0].y == 0 && snake[SIZE_SNAKE - 1].x + 1 == food[FOOD_INDEX].x && snake[SIZE_SNAKE- 1].y == food[FOOD_INDEX].y) {
  258. Eat();
  259. }
  260. for (int i = 0; i < SIZE_SNAKE - 1; i++) {
  261. snake[i].x = snake[i + 1].x;
  262. snake[i].y = snake[i + 1].y;
  263. }
  264. snake[SIZE_SNAKE - 1].x++;
  265. }
  266. }
  267.  
  268. void MoveLeft() {
  269. if (checktouchPorch() || touchItsBody() ||snake[SIZE_SNAKE - 1].x - 1 == x_coordinate) {
  270. ProcessDead();
  271. }
  272. else {
  273. if (porch[0].x == 0 && porch[0].y == 0 && snake[SIZE_SNAKE - 1].x - 1 == food[FOOD_INDEX].x && snake[SIZE_SNAKE - 1].y == food[FOOD_INDEX].y) {
  274. Eat();
  275. }
  276. for (int i = 0; i < SIZE_SNAKE - 1; i++) {
  277. snake[i].x = snake[i + 1].x;
  278. snake[i].y = snake[i + 1].y;
  279. }
  280. snake[SIZE_SNAKE - 1].x--;
  281. }
  282. }
  283.  
  284. void MoveDown() {
  285. if (checktouchPorch() || touchItsBody() ||snake[SIZE_SNAKE - 1].y + 1 == HEIGH_CONSOLE+y_coordinate) {
  286. ProcessDead();
  287. }
  288. else {
  289. if (porch[0].x == 0 && porch[0].y == 0 && snake[SIZE_SNAKE - 1].x == food[FOOD_INDEX].x && snake[SIZE_SNAKE - 1].y + 1 == food[FOOD_INDEX].y) {
  290. Eat();
  291. }
  292. for (int i = 0; i < SIZE_SNAKE - 1; i++) {
  293. snake[i].x = snake[i + 1].x;
  294. snake[i].y = snake[i + 1].y;
  295. }
  296. snake[SIZE_SNAKE - 1].y++;
  297. }
  298. }
  299.  
  300. void MoveUp() {
  301. if (checktouchPorch() ||touchItsBody() || snake[SIZE_SNAKE - 1].y - 1 == y_coordinate) {
  302. ProcessDead();
  303. }
  304. else {
  305. if (porch[0].x == 0 && porch[0].y == 0 && snake[SIZE_SNAKE - 1].x == food[FOOD_INDEX].x && snake[SIZE_SNAKE - 1].y - 1 == food[FOOD_INDEX].y) {
  306. Eat();
  307. }
  308. for (int i = 0; i < SIZE_SNAKE - 1; i++) {
  309. snake[i].x = snake[i + 1].x;
  310. snake[i].y = snake[i + 1].y;
  311. }
  312. snake[SIZE_SNAKE - 1].y--;
  313. }
  314. }
  315. // thu tuc cho thread
  316. void checkUpLevel()
  317. {
  318. if ((snake[SIZE_SNAKE - 1].x == (porch[0].x + 1)) && (snake[SIZE_SNAKE - 1].y == porch[0].y))
  319. {
  320.  
  321. SIZE_SNAKE--;
  322. if (SIZE_SNAKE == 0)
  323. {
  324. DrawPorch(' ');
  325. if (apple == MAX_SIZE_FOOD*MAX_SPEED)
  326. apple = 0;
  327. SIZE_SNAKE = 6 + apple;
  328. temp = 'D';
  329. MOVING = 'D';
  330. CHAR_LOCK = 'A';
  331. for (int i = 0; i < SIZE_SNAKE; i++)
  332. {
  333. snake[i].x = 10 + i + x_coordinate;
  334. snake[i].y = 5 + y_coordinate;
  335. }
  336. // updates level
  337. LEVEL++;
  338. gotoxy(x_coordinate + WIDTH_CONSOLE - 3, 3);
  339. printf("%d", LEVEL);
  340. if (SPEED == MAX_SPEED)
  341. {
  342. len = initlen;
  343. idx = 0;
  344. SPEED = 1;
  345. POINT p[6];
  346. for (int i = SIZE_SNAKE - 6, k = 0; i < SIZE_SNAKE; i++, k++)
  347. p[k] = snake[i];
  348. SIZE_SNAKE = 6;
  349. for (int i = 0; i < SIZE_SNAKE; i++)
  350. snake[i] = p[i];
  351. }
  352. else
  353. SPEED++;
  354. }
  355. }
  356. }
  357. void ThreadFunc()
  358. {
  359. Nocursortype();
  360. while (1)
  361. {
  362. if (STATE == 1)
  363. {// neu snake van con song
  364.  
  365. DrawSnakeAndFood(" ");
  366. switch (MOVING)
  367. {
  368. case 'A':
  369. {
  370. MoveLeft();
  371. break;
  372. }
  373. case 'D':
  374. {
  375. MoveRight();
  376. break;
  377. }
  378. case 'W':
  379. {
  380. MoveUp();
  381. break;
  382. }
  383. case 'S':
  384. {
  385. MoveDown();
  386. break;
  387. }
  388.  
  389. }
  390. /// kiem tra len level khi qua cong
  391. checkUpLevel();
  392. DrawSnakeAndFood(name);
  393. Sleep(TOCDO / SPEED);//ham ngu theo toc do speed
  394.  
  395. }
  396. }
  397. }
  398. //////////////////////////////////////
  399. void SaveGame(ofstream& infile)
  400. {
  401. TextColor(7);
  402. gotoxy(0, HEIGH_CONSOLE + y_coordinate + 1);
  403. printf(" SAVE GAME:\n Moi ban nhap ten file de luu game: ");
  404. fflush(stdin);
  405. gets_s(filename);
  406. fflush(stdin);
  407. infile.open(filename);
  408. if (!infile)
  409. return;
  410. infile << name << endl;
  411. infile << idx << endl;
  412. infile << initlen << endl;
  413. infile << len << endl;
  414. infile << LEVEL << endl;
  415. infile << SPEED << endl;
  416. infile << SCORE << endl;
  417. infile << SIZE_SNAKE << endl;
  418. for (int i = 0; i < SIZE_SNAKE; i++)
  419. infile << snake[i].x << " " << snake[i].y << endl;
  420. infile << FOOD_INDEX << endl;
  421. for (int i = FOOD_INDEX; i < MAX_SIZE_FOOD; i++)
  422. infile << food[i].x << " " << food[i].y << endl;
  423. infile << MOVING << endl;
  424. infile << CHAR_LOCK << endl;
  425. for (int i = 0; i < 5; i++)
  426. infile << porch[i].x << " " << porch[i].y << endl;
  427. infile << apple << endl;
  428. //
  429. infile.close();
  430. }
  431. void LoadGame(ifstream& infile)
  432. {
  433. infile.getline(name, 50);
  434. infile >> idx;
  435. infile >> initlen;
  436. infile >> len;
  437. infile >> LEVEL;
  438. infile >> SPEED;
  439. infile >> SCORE;
  440. infile >> SIZE_SNAKE;
  441. int x, y;
  442. for (int i = 0; i < SIZE_SNAKE; i++)
  443. {
  444. infile >> x >> y;
  445. snake[i].x = x;
  446. snake[i].y = y;
  447. }
  448. infile >> FOOD_INDEX;
  449. for (int i = FOOD_INDEX; i < MAX_SIZE_FOOD; i++)
  450. {
  451. infile >> x >> y;
  452. food[i].x = x;
  453. food[i].y = y;
  454. }
  455. infile >> MOVING;
  456. infile >> CHAR_LOCK;
  457. for (int i = 0; i < 5; i++)
  458. infile >> porch[i].x >> porch[i].y;
  459. infile >> apple;
  460. infile.close();
  461. }
  462. void read()
  463. {
  464. TextColor(14);
  465. ifstream fp("welcome.txt");
  466. char ch;
  467. while (!fp.eof())
  468. {
  469. ch = fp.get();
  470. cout << ch;
  471. Sleep(20);
  472. }
  473. Sleep(2000); // pause 2s
  474. TextColor(7);
  475. system("cls");
  476. }
  477. int main()
  478. {
  479. FixConsoleWindow();
  480. // gioi thieu game
  481. resizeConsole(1200, 600);
  482. read();
  483. // thiet lap man hinh console
  484. WIDTH_CONSOLE = 70;
  485. HEIGH_CONSOLE = 20;
  486. x_coordinate = 5; // toa do tam
  487. y_coordinate = 5;
  488. ofstream savefile;
  489. ifstream openfile;
  490. // man hinh console co 80 cot, 50 hang
  491. printf("\tMoi ban nhap phim:\n\tPhim 'T' de mo game da luu\n\tPhim bat ky de choi game moi:...? ");
  492. srand((unsigned int)time(0));
  493. temp = toupper(_getch());
  494. if (temp == 'T')
  495. {
  496. printf("\nNhap ten file de tai game da luu: ");
  497. do
  498. {
  499. fflush(stdin);
  500. gets_s(filename);
  501. fflush(stdin);
  502. openfile.open(filename);
  503. if (!openfile)
  504. printf("\n Khong ton tai tap tin!!!\nMoi ban nhap lai: ");
  505. } while (!openfile);
  506. LoadGame(openfile);
  507. MOVING = 0;
  508. }
  509. else
  510. {
  511. system("cls");
  512. printf("Moi nhap ten nguoi choi (6 ki tu): ");
  513. gets_s(name);
  514. fflush(stdin);
  515. //
  516. ResetData();
  517. }
  518. strcpy_s(ID, name);
  519. len = strlen(name);
  520. initlen = len;
  521. system("cls");
  522. StartGame();
  523. thread t1(ThreadFunc); //Tao thread cho snake
  524. HANDLE handle_t1 = t1.native_handle(); //Lay handle cua thread
  525. while (1)
  526. {
  527. temp = toupper(_getch());
  528. if (STATE == 1)
  529. {
  530. if (temp == 'P'){
  531. PauseGame(handle_t1);
  532.  
  533. }
  534. else if (temp == 'L'){
  535. PauseGame(handle_t1);
  536. DrawSnakeAndFood(name);
  537. SaveGame(savefile);
  538. TextColor(7);
  539. gotoxy(0, x_coordinate + HEIGH_CONSOLE + 3);
  540. printf("Ban muon tiep tuc choi game hay thoat game (Yes/No): ");
  541. char t;
  542. cin >> t;
  543. if (t == 'n')
  544. {
  545. t1.detach();
  546. break;
  547. }
  548. else
  549. {
  550. system("cls");
  551. StartGame();
  552. DrawSnakeAndFood(name);
  553. }
  554. }
  555. else if (temp == 'T'){
  556. PauseGame(handle_t1);
  557. DrawSnakeAndFood(name);
  558. TextColor(7);
  559. gotoxy(x_coordinate+WIDTH_CONSOLE+3, 12);
  560. printf("%c LOAD GAME: Moi ban nhap ten file da luu game: ", 254);
  561. do
  562. {
  563. fflush(stdin);
  564. gets_s(filename);
  565. fflush(stdin);
  566. openfile.open(filename);
  567. if (!openfile)
  568. {
  569. gotoxy(x_coordinate + WIDTH_CONSOLE + 3, 13);
  570. printf("! Moi ban nhap lai: ");
  571. }
  572. } while (!openfile);
  573. LoadGame(openfile);
  574. system("cls");
  575. StartGame();
  576. DrawSnakeAndFood(name);
  577.  
  578. }
  579. else if (temp == 27)
  580. {
  581. ExitGame(handle_t1);
  582. t1.detach();
  583. break;
  584. }
  585. else
  586. {
  587. ResumeThread(handle_t1);
  588. if ((temp != CHAR_LOCK) && (temp == 'D' || temp == 'A' || temp == 'W' || temp == 'S'))
  589. {
  590. if (temp == 'D')
  591. CHAR_LOCK = 'A';
  592. else if (temp == 'W')
  593. CHAR_LOCK = 'S';
  594. else if (temp == 'S')
  595. CHAR_LOCK = 'W';
  596. else
  597. CHAR_LOCK = 'D';
  598. MOVING = temp;
  599. }
  600. }
  601. }
  602. else
  603. {
  604. if (temp == 27) // nhan phim ESC de thoat game
  605. {
  606. ExitGame(handle_t1);
  607. t1.detach();
  608. break;
  609. }
  610. else if (temp == 'Y')
  611. {
  612. ResetData();
  613. StartGame();
  614. }
  615. }
  616. }
  617. system("pause");
  618. return 0;
  619. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement