Advertisement
joaquinflorespalao

SNAKE C++

May 27th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. #include <windows.h>
  2. #include <iostream>
  3. #include <stdlib.h>
  4. #include <conio.h>
  5.  
  6. #define ARRIBA 72
  7. #define IZQUIERDA 75
  8. #define DERECHA 77
  9. #define ABAJO 80
  10. #define ESC 27
  11.  
  12. int cuerpo[200][2];
  13. int n = 1, tam = 10, dir = 3;
  14. int x = 10, y = 12;
  15. int xc = 30, yc = 15;
  16. int velocidad = 60;
  17. char tecla;
  18.  
  19. void gotoxy(int x, int y)
  20. {
  21. HANDLE hCon;
  22. COORD dwPos;
  23.  
  24. dwPos.X = x;
  25. dwPos.Y = y;
  26. hCon = GetStdHandle(STD_OUTPUT_HANDLE);
  27. SetConsoleCursorPosition(hCon, dwPos);
  28. }
  29. void OcultaCursor() {
  30. CONSOLE_CURSOR_INFO cci = { 100, FALSE };
  31.  
  32. SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci);
  33. }
  34. void pintar() {
  35. for (int i = 2; i < 78; i++) {
  36. gotoxy(i, 3); printf("%c", 205);
  37. gotoxy(i, 23); printf("%c", 205);
  38. }
  39. for (int v = 4; v < 23; v++) {
  40. gotoxy(2, v); printf("%c", 186);
  41. gotoxy(77, v); printf("%c", 186);
  42. }
  43. gotoxy(2, 3); printf("%c", 201);
  44. gotoxy(2, 23); printf("%c", 200);
  45. gotoxy(77, 3); printf("%c", 187);
  46. gotoxy(77, 23); printf("%c", 188);
  47. }
  48. void guardar_posicion() {
  49. cuerpo[n][0] = x;
  50. cuerpo[n][1] = y;
  51. n++;
  52. if (n == tam) n = 1;
  53. }
  54. void dibujar_cuerpo() {
  55. for (int i = 1; i < tam; i++) {
  56. gotoxy(cuerpo[i][0], cuerpo[i][1]); printf("*");
  57. }
  58. }
  59. void borrar_cuerpo() {
  60. gotoxy(cuerpo[n][0], cuerpo[n][1]); printf(" ");
  61. }
  62. void teclear() {
  63. if (_kbhit()) {
  64. tecla = _getch();
  65. switch (tecla) {
  66. case ARRIBA: if (dir != 2) dir = 1; break;
  67. case ABAJO: if (dir != 1) dir = 2; break;
  68. case DERECHA: if (dir != 4) dir = 3; break;
  69. case IZQUIERDA: if (dir != 3) dir = 4; break;
  70. }
  71. }
  72. }
  73. void comida()
  74. {
  75. if (x == xc && y == yc)
  76. {
  77. xc = (rand() % 73) + 4;
  78. yc = (rand() % 19) + 4;
  79.  
  80. tam++;
  81. gotoxy(xc, yc); printf("%c", 4);
  82. }
  83. }
  84. bool game_over()
  85. {
  86. if (y == 3 || y == 23 || x == 2 || x == 77) return false;
  87. for (int j = tam - 1; j > 0; j--) {
  88. if (cuerpo[j][0] == x && cuerpo[j][1] == y)
  89. return false;
  90. }
  91. return true;
  92. }
  93. int main()
  94. {
  95. OcultaCursor();
  96.  
  97. pintar();
  98. gotoxy(xc, yc); printf("%c", 4);
  99.  
  100. while (tecla != ESC && game_over())
  101. {
  102. borrar_cuerpo();
  103. guardar_posicion();
  104. dibujar_cuerpo();
  105. comida();
  106. teclear();
  107. teclear();
  108.  
  109. if (dir == 1) y--;
  110. if (dir == 2) y++;
  111. if (dir == 3) x++;
  112. if (dir == 4) x--;
  113.  
  114. Sleep(velocidad);
  115. }
  116. pintar();
  117.  
  118. system("pause>null");
  119. return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement