Advertisement
Guest User

Snake Game

a guest
Nov 16th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.82 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #include <stdlib.h>
  4.  
  5. #include <conio.h>
  6.  
  7. #include <windows.h>
  8.  
  9. #define FRAME_RATE 40
  10.  
  11. using namespace std;
  12. void clearScreen() {
  13.     HANDLE hOut;
  14.     COORD Position;
  15.     hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  16.     Position.X = 0;
  17.     Position.Y = 0;
  18.     SetConsoleCursorPosition(hOut, Position);
  19. }
  20. void hidecursor() {
  21.     HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
  22.     CONSOLE_CURSOR_INFO info;
  23.     info.dwSize = 100;
  24.     info.bVisible = FALSE;
  25.     SetConsoleCursorInfo(consoleHandle, &info);
  26. }
  27. bool gameOver;
  28. const int width = 20;
  29. const int height = 20;
  30. int x, y, fruitX, fruitY, score;
  31. int tailX[100], tailY[100];
  32. int nTail;
  33. enum eDirecton {
  34.     STOP = 0, LEFT, RIGHT, UP, DOWN
  35. };
  36. eDirecton dir;
  37. void Setup() {
  38.     hidecursor();
  39.     gameOver = false;
  40.     dir = STOP;
  41.     x = width / 2;
  42.     y = height / 2;
  43.     fruitX = rand() % width;
  44.     fruitY = rand() % height;
  45.     score = 0;
  46. }
  47. void Draw() {
  48.     clearScreen(); //system("clear");
  49.     for (int i = 0; i < width + 2; i++)
  50.         printf("#");
  51.     printf("\n");
  52.     for (int i = 0; i < height; i++) {
  53.         for (int j = 0; j < width; j++) {
  54.             if (j == 0)
  55.                 printf("#");
  56.             if (i == y && j == x)
  57.                 printf("O");
  58.             else if (i == fruitY && j == fruitX)
  59.                 printf("F");
  60.             else {
  61.                 bool print = false;
  62.                 for (int k = 0; k < nTail; k++) {
  63.                     if (tailX[k] == j && tailY[k] == i) {
  64.                         printf("o");
  65.                         print = true;
  66.                     }
  67.                 }
  68.                 if (!print)
  69.                     printf(" ");
  70.             }
  71.             if (j == width - 1)
  72.                 printf("#");
  73.         }
  74.         printf("\n");
  75.     }
  76.     for (int i = 0; i < width + 2; i++)
  77.         printf("#");
  78.     printf("\n");
  79.     printf("Score: %i", score);
  80. }
  81. void Input() {
  82.     if (_kbhit()) {
  83.         switch (_getch()) {
  84.         case 'a':
  85.             dir = LEFT;
  86.             break;
  87.         case 'd':
  88.             dir = RIGHT;
  89.             break;
  90.         case 'w':
  91.             dir = UP;
  92.             break;
  93.         case 's':
  94.             dir = DOWN;
  95.             break;
  96.         case 'x':
  97.             gameOver = true;
  98.             break;
  99.         }
  100.     }
  101. }
  102. void Logic() {
  103.     int prevX = tailX[0];
  104.     int prevY = tailY[0];
  105.     int prev2X, prev2Y;
  106.     tailX[0] = x;
  107.     tailY[0] = y;
  108.     for (int i = 1; i < nTail; i++) {
  109.         prev2X = tailX[i];
  110.         prev2Y = tailY[i];
  111.         tailX[i] = prevX;
  112.         tailY[i] = prevY;
  113.         prevX = prev2X;
  114.         prevY = prev2Y;
  115.     }
  116.     switch (dir) {
  117.     case LEFT:
  118.         x--;
  119.         break;
  120.     case RIGHT:
  121.         x++;
  122.         break;
  123.     case UP:
  124.         y--;
  125.         break;
  126.     case DOWN:
  127.         y++;
  128.         break;
  129.     default:
  130.         break;
  131.     }
  132.     //if (x > width || x < 0 || y > height || y < 0)
  133.     // gameOver = true;
  134.     if (x >= width) x = 0;
  135.     else if (x < 0) x = width - 1;
  136.     if (y >= height) y = 0;
  137.     else if (y < 0) y = height - 1;
  138.     for (int i = 0; i < nTail; i++)
  139.         if (tailX[i] == x && tailY[i] == y)
  140.             gameOver = true;
  141.     if (x == fruitX && y == fruitY) {
  142.         score += 10;
  143.         fruitX = rand() % width;
  144.         fruitY = rand() % height;
  145.         nTail++;
  146.     }
  147. }
  148. int main() {
  149.     Setup();
  150.     while (!gameOver) {
  151.         Draw();
  152.         Input();
  153.         Logic();
  154.         Sleep(FRAME_RATE); //sleep(40);
  155.     }
  156.     return 0;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement