fork(3) download
  1. /*
  2.  * File: Snake.cpp
  3.  * Creator: H3Sjunior
  4.  *
  5.  * Tro choi con ran san moi
  6.  */
  7.  
  8.  
  9. // Y tuong:
  10. // - con ran di chuyen trong phan gioi han, an thuc an, tranh cac chuong ngai vat
  11. // - dung danh sach lien ket
  12. // - moi nut cua danh sach lien ket la mot nut cua con ran: du lieu ve vi tri, con tro chi ve nut phia sau cua no
  13. // - cach thuc di chuyen: in tat ca vi tri cac nut, thoi gian cho nguoi dung nhap huong di chuyen, xoa man hinh, in tat ca vi tri cac nut sau khi di chuyen
  14.  
  15.  
  16. #include <iostream>
  17. #include <conio.h>
  18. #include <windows.h>
  19. #include <ctime>
  20. #include <cstdlib>
  21.  
  22. // Dinh nghia cac phim di chuyen tren ban phim: a, d, w, s
  23. #define KEY_A 0x41
  24. #define KEY_D 0x44
  25. #define KEY_W 0x57
  26. #define KEY_S 0x53
  27.  
  28. using namespace std;
  29.  
  30. // duong bien gioi han con ran co the di chuyen
  31. const int TUNG_DO = 20;
  32. const int HOANG_DO = 60;
  33.  
  34.  
  35. // tao vi tri moi
  36. struct viTri {
  37. int x;
  38. int y;
  39. };
  40.  
  41. // danh sach lien ket cac nut cua con ran
  42. struct snake{
  43. viTri data;
  44. snake * next;
  45. };
  46.  
  47. void gotoxy(short x, short y); // ham gotoxy: dua con tro toi vi tri (x, y)
  48. viTri newNoc (int x, int y); // tao vi tri cua 1 nut
  49. void addHead (snake *& head, int x, int y); // tao nut con ran moi
  50. void addEnd (snake *& head, int x, int y); // chep du lieu vi tri vao nut moi cua con ran ( khi an duoc thuc an)
  51. int length (snake *& head); // do dai cua ran
  52. void copy (snake *& head1, snake *& head2); // sao chep vi tri nut 2 vao nut 1
  53. void background (snake *& head); // tao phong nen de choi game
  54. void atime (long & doKho, int & control1, int & control2); // thoi gian dung cua con ran, nut con control la A, W, S, D de chi huong di chuyen
  55. void movieSnake (snake *& head, int & control1, int & control2) ; // di chuyen con ran = di chuyen lan luot tung nut cua con ran (*)
  56. void print (snake *& head); // in ca con ran sau khi xoa man hinh
  57. bool checkKey(int key); // trong time chay kiem tra co an nut di chuyen hay ko
  58. void xoaMH (); // xoa man hinh
  59. bool isMovie(snake *& head, snake *& ghost, int & score); // dieu kien di chuyen: ko dam trung duoi, ko dam trung tuong
  60. void theEnd (snake *& head, int & score); // in man hinh ket thuc game
  61. bool newFood (viTri & food); // tao thuc an moi
  62. void checkEat (snake *& head, snake *& ghost, viTri & food, int & score); // Kiem tra ran co an duoc thuc an hay khong
  63. void level (long & doKho); // Chia do kho cua game, tuy theo toc do di chuyen cua con ran
  64. void ghost (snake *& ghost, int & runGhost); // tao chuong ngai vat di chuyen tu tren xuong, ran dam trung thi se thua
  65. void printGhost (snake *& ghost); // in chuong ngai vat
  66. void deleteSnake (snake *& head); // xoa con ran sau khi game ket thuc, ham nay dang hoan thien
  67.  
  68. int main ()
  69. {
  70. int playAgain = 0;
  71. int control1 ; // lenh dieu khien truoc do
  72. int control2; // lenh dieu khien sau
  73. int i = 0, end = 0, score = 0, runGhost = 0;
  74. viTri food;
  75. long doKho;
  76. int bestScore = 0;
  77. do
  78. {
  79. snake *Snake; // con ran
  80. snake *Ghost; // chuong ngai vat
  81. Snake = NULL;
  82. Ghost = NULL;
  83. addHead (Snake, 2, 5);
  84. addHead (Snake, 3, 5);
  85. addHead (Snake, 4, 5);
  86. addHead (Snake, 5, 5);
  87. addHead (Snake, 6, 5);
  88.  
  89. background (Snake); // tao khung nen cua chuong trinh
  90.  
  91. score = length(Snake);
  92. control1 = control2 = 3;
  93. xoaMH ();
  94. level (doKho);
  95. print (Snake);
  96. newFood (food);
  97. while (1) {
  98. if (isMovie (Snake, Ghost, score)) {
  99. gotoxy(1, 23);
  100. system ("pause");
  101. break;
  102. }
  103.  
  104. checkEat (Snake, Ghost, food, score);
  105. atime (doKho, control1, control2);
  106. movieSnake (Snake, control2, control1);
  107. control1 = control2;
  108. xoaMH ();
  109. print (Snake);
  110. ghost (Ghost, runGhost);
  111. printGhost (Ghost);
  112. runGhost++;
  113. }
  114. xoaMH ();
  115. gotoxy (20, 5);
  116. cout << "1. Play again";
  117. gotoxy (20, 6);
  118. cout << "2. Back";
  119. gotoxy (14, 8);
  120. cout << "Please choose number (1->2): ";
  121. cin >> playAgain;
  122.  
  123. if ( playAgain == 1) {
  124. deleteSnake(Snake);
  125. deleteSnake(Ghost);
  126. }
  127. }
  128. while ( playAgain == 1);
  129.  
  130. gotoxy(1, 23);
  131. system("pause");
  132. return 0;
  133. }
  134.  
  135.  
  136.  
  137. // ham gotoxy: dua con tro toi vi tri (x, y)
  138. void gotoxy(short x, short y)
  139. {
  140. HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
  141. COORD pos;
  142. pos.X = x-1;
  143. pos.Y = y-1;
  144. SetConsoleCursorPosition(hCon, pos);
  145. }
  146.  
  147. // tao vi tri cua 1 nut
  148. viTri newNoc (int x, int y) {
  149. viTri result;
  150. result.x = x;
  151. result.y = y;
  152. return result;
  153. }
  154.  
  155. // tao nut con ran moi
  156. void addHead (snake *& head, int x, int y) {
  157. snake *tmp = new snake;
  158. (tmp->data).x = x;
  159. (tmp->data).y = y;
  160. if (head == NULL) {
  161. head = tmp;
  162. }
  163. else {
  164. tmp->next = head;
  165. head = tmp;
  166. }
  167. tmp = NULL;
  168. }
  169.  
  170. // chep du lieu vi tri vao nut moi cua con ran ( khi an duoc thuc an)
  171. void addEnd (snake *& head, int x, int y) {
  172. snake *tmp = new snake;
  173. snake *copy = new snake;
  174. copy = head;
  175. (tmp->data).x = x;
  176. (tmp->data).y = y;
  177. tmp->next = NULL;
  178. if (head == NULL) {
  179. head = tmp;
  180. }
  181. else {
  182. while (copy->next != NULL)
  183. copy = copy->next;
  184. copy->next = tmp;
  185. }
  186. }
  187.  
  188. // do dai cua ran
  189. int length (snake *& head) {
  190. int i = 0;
  191. snake *q = new snake;
  192. q = head;
  193. while (q != NULL) {
  194. q = q->next;
  195. i++;
  196. }
  197. return i;
  198. }
  199.  
  200. // sao chep vi tri nut 2 vao nut 1
  201. void copy (snake *& head1, snake *& head2) {
  202. (head1->data).x = (head2->data).x;
  203. (head1->data).y = (head2->data).y;
  204. }
  205.  
  206. // tao phong nen de choi game
  207. void background (snake *& head) {
  208. //tao khung tuong
  209. for (int k = 1; k <= TUNG_DO; k++) {
  210. gotoxy(1, k);
  211. cout << "+";
  212. }
  213. for (int k = 1; k <= HOANG_DO; k++) {
  214. gotoxy(k, 1);
  215. cout << "+";
  216. }
  217. for (int k = 1; k <= TUNG_DO; k++) {
  218. gotoxy(HOANG_DO, k);
  219. cout << "+";
  220. }
  221. for (int k = 1; k <= HOANG_DO; k++) {
  222. gotoxy(k, TUNG_DO);
  223. cout << "+";
  224. }
  225.  
  226. gotoxy (62, 3);
  227. cout << "SNAKE GAME";
  228. gotoxy(62, 5);
  229. cout << "Score:";
  230. gotoxy(69, 5);
  231. cout << length (head);
  232. }
  233.  
  234. // thoi gian dung cua con ran, nut con control la A, W, S, D de chi huong di chuyen
  235. void atime (long & doKho, int & control1, int & control2) {
  236. // cin.ignore();
  237. int mid = control2;
  238. for (int j = 0; j < doKho; j++) {
  239. if (checkKey(VK_LEFT) || checkKey(KEY_A))
  240. control2 = 1;
  241. if (checkKey(VK_UP) || checkKey(KEY_W))
  242. control2 = 2;
  243. if (checkKey(VK_RIGHT) || checkKey(KEY_D))
  244. control2 = 3;
  245. if (checkKey(VK_DOWN) || checkKey(KEY_S))
  246. control2 = 4;
  247. }
  248. if (control2 == (control1 - 2) || control2 == (control1 + 2))
  249. control2 = mid;
  250. }
  251.  
  252. // di chuyen con ran = di chuyen lan luot tung nut cua con ran (*)
  253. void movieSnake (snake *& head, int & control1, int & control2) {
  254. // di chuyen cac nut sau
  255. snake *q = new snake;
  256. q = head;
  257. while ( q->next != NULL ) {
  258. q = q->next;
  259. }
  260. (q->data).y = 1;
  261.  
  262. // copy data cua nut sau vao nut truoc
  263. snake *k = new snake;
  264. while ( 1 ) {
  265. k = head;
  266. while (q != k->next) {
  267. k = k->next;
  268. }
  269. copy(q, k);
  270. q = k;
  271. if (q == head) break;
  272. k = NULL;
  273. }
  274. q = NULL;
  275.  
  276. // di chuyen nut 1, theo huong a, w, d, s
  277. if (control2 == 1) // a
  278. (head->data).x -= 1;
  279. if (control2 == 2) // w
  280. (head->data).y -= 1;
  281. if (control2 == 3) // d
  282. (head->data).x += 1;
  283. if (control2 == 4) // s
  284. (head->data).y += 1;
  285. }
  286.  
  287. // in ca con ran sau khi xoa man hinh
  288. void print (snake *& head) {
  289. snake *q = new snake;
  290. q = head;
  291.  
  292. // nut 1
  293. gotoxy ((q->data).x, (q->data).y);
  294. cout << "*";
  295. q = q->next;
  296.  
  297. // cac nut sau
  298. while ( q != NULL) {
  299. gotoxy ((q->data).x, (q->data).y);
  300. cout << "+";
  301. q = q->next;
  302. }
  303. q = NULL;
  304. }
  305.  
  306. // trong time chay kiem tra co an nut di chuyen hay ko
  307. bool checkKey(int key)
  308. {
  309. return GetAsyncKeyState(key);
  310. }
  311.  
  312.  
  313. // xoa man hinh
  314. void xoaMH () {
  315. for (int i = 2; i < TUNG_DO; i++) {
  316. gotoxy (2, i);
  317. cout << " ";
  318. }
  319. }
  320.  
  321. // dieu kien di chuyen: ko dam trung duoi, ko dam trung tuong
  322. bool isMovie(snake *& head, snake *& ghost, int & score) {
  323. snake *q = new snake;
  324. q = head;
  325.  
  326. while (q->next != NULL) {
  327. // xem co dam trung ghost hay ko
  328. if (ghost != NULL) {
  329. snake *k = new snake;
  330. k = ghost;
  331. while (k->next != NULL) {
  332. if ((q->data).x == (k->data).x && (q->data).y == (k->data).y) {
  333. theEnd (head, score);
  334. return 1;
  335. }
  336. k = k->next;
  337. }
  338. k = NULL;
  339. }
  340.  
  341. q = q->next;
  342. if ((q->data).x == (head->data).x && (q->data).y == (head->data).y) {
  343. theEnd (head, score);
  344. return 1;
  345. }
  346.  
  347.  
  348. }
  349.  
  350. if ((head->data).x == 1) {
  351. theEnd (head, score);
  352. return 1;
  353. }
  354.  
  355. if ((head->data).y == 1) {
  356. theEnd (head, score);
  357. return 1;
  358. }
  359. if ((head->data).x == HOANG_DO) {
  360. theEnd (head, score);
  361. return 1;
  362. }
  363.  
  364. if ((head->data).y == TUNG_DO) {
  365. theEnd (head, score);
  366. return 1;
  367. }
  368.  
  369. return 0;
  370. }
  371.  
  372. // in man hinh ket thuc game
  373. void theEnd (snake *& head, int & score) {
  374. system("cls");
  375. gotoxy (30, 5);
  376. cout << "GAME OVER";
  377. gotoxy(29, 8);
  378. cout << "Score:";
  379. gotoxy(38, 8);
  380. cout << score;
  381. }
  382.  
  383. // tao thuc an moi
  384. bool newFood (viTri & food) {
  385. srand (time (0));
  386. food.x = (rand())%(HOANG_DO - 2) + 2;
  387. food.y = (rand())%(TUNG_DO - 2) + 2;
  388. gotoxy(food.x, food.y);
  389. cout << "O";
  390. return 1;
  391. }
  392.  
  393. void checkEat (snake *& head, snake *& ghost, viTri & food, int & score) {
  394. // truong hop an duoc food
  395. if ((head->data).x == food.x && (head->data).y == food.y)
  396. {
  397. newFood (food); // tao new food
  398. // tao nut moi o duoi con ran
  399. snake *q = new snake;
  400. q = head;
  401. while (q->next != NULL)
  402. q = q->next;
  403. addEnd(head, (q->data).x, (q->data).y);
  404.  
  405. // them diem so
  406. gotoxy(69, 5);
  407. score += 15;
  408. cout << score;
  409.  
  410. //tao ghost
  411. int ghost_x = (rand())%(HOANG_DO - 2) + 2;
  412. addHead(ghost, ghost_x, 2);
  413. }
  414.  
  415. else {
  416. gotoxy(food.x, food.y);
  417. cout << "O";
  418. }
  419. }
  420.  
  421. // Chia do kho cua game, tuy theo toc do di chuyen cua con ran
  422. void level (long & doKho) {
  423. int result;
  424. do {
  425. gotoxy (20, 5);
  426. cout << "1. Easy";
  427. gotoxy (20, 6);
  428. cout << "2. Medium";
  429. gotoxy (20, 7);
  430. cout << "3. Dificult";
  431. gotoxy (14, 9);
  432. cout << "Please choose level (1->3): ";
  433. cin >> result;
  434. }
  435. while (result > 3 || result < 0);
  436. if (result == 1) doKho = 8e4;
  437. if (result == 2) doKho = 4e4;
  438. if (result == 3) doKho = 2e4;
  439. }
  440.  
  441. // tao chuong ngai vat di chuyen tu tren xuong, ran dam trung thi se thua
  442. void ghost (snake *& ghost, int & runGhost) {
  443. if ( ghost == NULL) return;
  444. //xoa ghost neu ra khoi man hinh
  445. if ((ghost->data).y >= TUNG_DO)
  446. ghost = ghost->next;
  447.  
  448. snake *q = new snake;
  449. q = ghost;
  450. while ( q->next != NULL ) {
  451. if ((q->next->data).y >= TUNG_DO) {
  452. q->next = q->next->next;
  453. break;
  454. }
  455. q = q->next;
  456. }
  457.  
  458. // cho ghost di xuong
  459. q = ghost;
  460.  
  461. if (runGhost%4 == 0) {
  462. while ( q->next != NULL ) {
  463. (q->data).y += 1;
  464. q = q->next;
  465. }
  466. q = NULL;
  467. }
  468. }
  469.  
  470. // in chuong ngai vat
  471. void printGhost (snake *& ghost) {
  472. if ( ghost == NULL) return;
  473. snake *q = new snake;
  474. q = ghost;
  475. while ( q->next != NULL ) {
  476. gotoxy ((q->data).x, (q->data).y);
  477. cout << "*";
  478. q = q->next;
  479. }
  480. }
  481.  
  482. // xoa con ran sau khi game ket thuc, ham nay dang hoan thien
  483. void deleteSnake (snake *& head) {
  484. delete [] head;
  485. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:17:19: fatal error: conio.h: No such file or directory
compilation terminated.
stdout
Standard output is empty