Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #include <conio.h>
- #include <windows.h>
- using namespace std;
- //Enumeration for direction
- enum EDirecton
- {
- Stop = 0,
- Left,
- Right,
- Up,
- Down
- };
- struct Position
- {
- int Row;
- int Col;
- };
- //Declare variables
- bool gameOver;
- const int width = 40;
- const int height = 20;
- Position head;
- Position food;
- int score;
- Position snake[200];
- int snakeSize;
- EDirecton direction;
- Position obstacle;
- void SetUp()
- {
- gameOver = false;
- direction = Stop;
- head.Col = width / 2;
- head.Row = height / 2;
- food.Col = rand() % width;
- food.Row = rand() % height;
- score = 0;
- obstacle.Col = rand() % width;
- obstacle.Row = rand() % height;
- }
- //Function for changing the cursor coordinates
- void SetCursorPointer(int col, int row)
- {
- static HANDLE hStdout = NULL;
- COORD coord;
- coord.X = col;
- coord.Y = row;
- if (!hStdout)
- {
- hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
- }
- SetConsoleCursorPosition(hStdout,coord);
- }
- void Draw()
- {
- //Draw upper border
- SetCursorPointer(0, 0);
- for (int col = 0; col < width + 2; col++)
- {
- cout << "#";
- }
- cout << endl;
- //Draw field
- for (int row = 0; row < height; row ++)
- {
- for (int col = 0; col < width; col++)
- {
- if (col == 0)
- {
- cout << "#";
- }
- //Draw snake head
- if (row == head.Row && col == head.Col)
- {
- cout << "O";
- }
- //Draw food
- else if (row == food.Row && col == food.Col)
- {
- cout << "@";
- }
- //Draw Obstacle
- else if (row == obstacle.Row && col == obstacle.Col)
- {
- cout << "X";
- }
- else
- {
- //Draw snake body
- bool print = false;
- for (int index = 0; index < snakeSize; index++)
- {
- if (snake[index].Col == col && snake[index].Row == row)
- {
- cout << "o";
- print = true;
- }
- }
- if (!print)
- {
- cout << " ";
- }
- }
- if (col == width - 1)
- {
- cout << "#";
- }
- }
- cout << endl;
- }
- //Draw bottom border
- for (int col = 0; col < width + 2; col++)
- {
- cout << "#";
- }
- cout << endl;
- SetCursorPointer(15, 22);
- cout << "Score:" << score << endl;
- }
- //Function for changing direction
- void Input()
- {
- //The direction is changed when a key is pressed
- if (_kbhit())
- {
- switch (_getch())
- {
- case 'a':
- direction = Left;
- break;
- case 'd':
- direction = Right;
- break;
- case 'w':
- direction = Up;
- break;
- case 's':
- direction = Down;
- break;
- case 'x':
- gameOver = true;
- break;
- }
- }
- }
- //Implement thе snake movement
- void Logic()
- {
- Position previous = snake[0];
- Position next;
- snake[0].Col = head.Col;
- snake[0].Row = head.Row;
- for (int index = 1; index < snakeSize; index++)
- {
- next.Col = snake[index].Col;
- next.Row = snake[index].Row;
- snake[index].Col = previous.Col;
- snake[index].Row = previous.Row;
- previous.Col = next.Col;
- previous.Row = next.Row;
- }
- switch (direction)
- {
- case Left:
- head.Col--;
- break;
- case Right:
- head.Col++;
- break;
- case Up:
- head.Row--;
- break;
- case Down:
- head.Row++;
- break;
- default:
- break;
- }
- //Check if the snake have hit the border
- if (head.Col >= width || head.Col < 0 || head.Row >= height || head.Row < 0)
- {
- gameOver = true;
- }
- //Check if the snake has eaten its body
- for (int index = 0; index < snakeSize; index++)
- {
- if (snake[index].Col == head.Col & snake[index].Row == head.Row)
- {
- gameOver = true;
- }
- }
- //Check if the snake has hit an obstacle
- if (obstacle.Col == head.Col & obstacle.Row == head.Row)
- {
- gameOver = true;
- }
- //Check if the snake has eaten the food
- if (head.Col == food.Col && head.Row == food.Row)
- {
- score += 10;
- food.Col = rand() % width;
- food.Row = rand() % height;
- snakeSize++;
- }
- }
- //Main function where everything starts
- int main()
- {
- SetUp();
- while (!gameOver)
- {
- Draw();
- Input();
- Logic();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement