Advertisement
Guest User

snake.cpp

a guest
Dec 26th, 2015
789
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include "snake.h"
  4. #include <time.h>
  5.  
  6. // Hàm thay đổi kích cỡ của khung cmd.
  7. void resizeConsole(int width, int height)
  8. {
  9.     HWND console = GetConsoleWindow();
  10.     RECT r;
  11.     GetWindowRect(console, &r);
  12.     MoveWindow(console, r.left, r.top, width, height, TRUE);
  13. }
  14.  
  15. // Hàm tô màu.
  16. void textcolor(int x)
  17. {
  18.     HANDLE mau;
  19.     mau = GetStdHandle(STD_OUTPUT_HANDLE);
  20.     SetConsoleTextAttribute(mau, x);
  21. }
  22.  
  23. // Hàm dịch chuyển con trỏ đến tọa độ x, y.
  24. void gotoxy(int x, int y)
  25. {
  26.     HANDLE hConsoleOutput;
  27.     COORD Cursor_an_Pos = { x - 1, y - 1 };
  28.     hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
  29.     SetConsoleCursorPosition(hConsoleOutput, Cursor_an_Pos);
  30. }
  31.  
  32. // Hàm xóa màn hình.
  33. void XoaManHinh()
  34. {
  35.     HANDLE hOut;
  36.     COORD Position;
  37.     hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  38.     Position.X = 0;
  39.     Position.Y = 0;
  40.     SetConsoleCursorPosition(hOut, Position);
  41. }
  42.  
  43.  
  44. snake::snake()
  45. {
  46.     srand(time(NULL));
  47.     head.x = X0 + WIDTH / 2;
  48.     head.y = Y0 + HEIGHT / 2;
  49.  
  50.     tao.x = X0 + 1 +  rand() % (WIDTH - 1);
  51.     tao.y = Y0 + 1 + rand() % (HEIGHT - 1);
  52.  
  53.     prev = RIGHT;
  54.     gameOver = false;
  55.     score = 0;
  56.     soDuoi = 1;
  57.  
  58.     tail.push_back(head);
  59. }
  60.  
  61.  
  62. snake::~snake()
  63. {
  64. }
  65.  
  66. void snake::veKhung(){
  67.     resizeConsole(800, 600);
  68.     SetConsoleTitle("Programmer: Tuan Nguyen Dinh - ndinhtuan15@gmail.com");
  69.     veTao();
  70.     veDiem();
  71.     veRan();
  72.    
  73.  
  74.     for (int i = X0; i <= X0 + WIDTH; i++){
  75.         textcolor(5);
  76.         gotoxy(i, Y0);
  77.         std::cout << char(CHAR);
  78.  
  79.         gotoxy(i, Y0 + HEIGHT);
  80.         std::cout << char(CHAR);
  81.  
  82.     }
  83.  
  84.     for (int i = Y0; i <= Y0 + HEIGHT; i++){
  85.         gotoxy(X0, i);
  86.         std::cout << char(CHAR);
  87.  
  88.         gotoxy(X0 + WIDTH, i);
  89.         std::cout << char(CHAR);
  90.     }
  91.     _getch();
  92. }
  93.  
  94. void snake::veRan(){
  95.    
  96.     textcolor(4);
  97.    
  98.     for (int i = 0; i < tail.size(); i++){
  99.         if (i == 0){
  100.             gotoxy(tail[0].x, tail[0].y);
  101.             std::cout << "@";
  102.             //gotoxy(1, 1 + i); std::cout << tail[i].x0 << ";" << tail[i].y0 << "|" << tail[i].x << ";" << tail[i].y;
  103.         }
  104.         else{
  105.             initPrev(tail[i]);
  106.  
  107.             gotoxy(tail[i].x, tail[i].y); std::cout << " ";
  108.  
  109.             tail[i].x = tail[i - 1].x0;
  110.             tail[i].y = tail[i - 1].y0;
  111.  
  112.             gotoxy(tail[i].x, tail[i].y); std::cout << "c";
  113.             //gotoxy(1, 1 + i); std::cout << tail[i].x0 << ";" << tail[i].y0 << "|" << tail[i].x << ";" << tail[i].y;
  114.         }
  115.     }
  116.     Sleep(60 + speed);
  117. }
  118.  
  119. void snake::dieuKhien(){
  120.     Sleep(15);
  121.     initPrev(tail[0]);
  122.     xoaDiem(head.x, head.y);
  123.     if (GetAsyncKeyState(VK_LEFT) && prev != RIGHT){
  124.         head.x--;
  125.         prev = LEFT;
  126.     }
  127.     else if (GetAsyncKeyState(VK_RIGHT) && prev != LEFT){
  128.         //xoaDiem(head.x, head.y);
  129.         head.x++;
  130.         prev = RIGHT;
  131.     }
  132.     else if (GetAsyncKeyState(VK_UP) && prev != DOWN){
  133.         //xoaDiem(head.x, head.y);
  134.         head.y--;
  135.         prev = UP;
  136.     }
  137.     else if (GetAsyncKeyState(VK_DOWN) && prev != UP){
  138.         //xoaDiem(head.x, head.y);
  139.         head.y++;
  140.         prev = DOWN;
  141.     }
  142.     else if (GetAsyncKeyState(32)){
  143.         gotoxy(2, 2);
  144.         system("pause");
  145.         gotoxy(2, 2);
  146.         std::cout << "                                                                      ";
  147.     }
  148.     else{
  149.         switch (prev){
  150.         case LEFT:
  151.             head.x--;
  152.             break;
  153.         case RIGHT:
  154.             head.x++;
  155.             break;
  156.         case UP:
  157.             head.y--;
  158.             break;
  159.         case DOWN:
  160.             head.y++;
  161.             break;
  162.         }
  163.     }
  164.  
  165.     tail[0].x = head.x;
  166.     tail[0].y = head.y;
  167.    
  168. }
  169.  
  170. void snake::xoaDiem(int x, int y){
  171.     gotoxy(x, y);
  172.     std::cout << " ";
  173. }
  174.  
  175. void snake::kiemTraThua(){
  176.     if (head.x <= X0 || head.x >= X0 + WIDTH){
  177.         gameOver = true;
  178.     }
  179.     if (head.y <= Y0 || head.y >= Y0 + HEIGHT){
  180.         gameOver = true;
  181.     }
  182.  
  183.     if (gameOver){
  184.         gotoxy(X0 + WIDTH / 2 - 6, Y0 + HEIGHT / 2);
  185.         textcolor(9);
  186.         std::cout << "GAME OVER !";
  187.  
  188.         std::cin.ignore(1);
  189.         //_getch();
  190.         exit(1);
  191.     }
  192. }
  193.  
  194. void snake::veTao(){
  195.     textcolor(14);
  196.     gotoxy(tao.x, tao.y);
  197.     std::cout << "0";
  198. }
  199.  
  200. void snake::veDiem(){
  201.     textcolor(15);
  202.     gotoxy(4 + WIDTH / 2, Y0 - 4);
  203.     std::cout << "Diem : " << score;
  204. }
  205.  
  206. void snake::kiemTraAn(){
  207.     if (head.x == tao.x && head.y == tao.y){
  208.         score += 10;
  209.         soDuoi++;
  210.  
  211.         if (score > 100){
  212.             speed = 0;
  213.         }
  214.  
  215.         ToaDo t;
  216.         tail.push_back(t);
  217.  
  218.         veDiem();
  219.         gotoxy(tao.x, tao.y);
  220.         std::cout << " ";
  221.         srand(time(NULL));
  222.         tao.x = X0 + 1 + rand() % (WIDTH - 1);
  223.         tao.y = Y0 + 1 + rand() % (HEIGHT - 1);
  224.         veTao();
  225.     }
  226. }
  227.  
  228. void snake::initPrev(ToaDo& td){
  229.     td.x0 = td.x;
  230.     td.y0 = td.y;
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement