Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <chrono>
- #include <thread>
- #include <windows.h>
- #include <conio.h>
- #include <cstdlib>
- using namespace std;
- //For the menu
- int choice;
- //The XY coordinate for the snake's head
- int headPosX, headPosY;
- int mouseX, mouseY;
- //The height and width of the board
- const int height = 50;
- const int width = 50;
- //Boolean so game runs on a loop and breaks once game is over (exits game)
- bool gameOver;
- //To hold several variables (left = 1 right = 2 etc)
- enum eDirection : int { STOP = 0, LEFT, RIGHT, UP, DOWN};
- enum eDirection dir;
- //Array for tail with fixed max length
- int tailX[100], tailY[100];
- int nTail;
- //Main setup for putting the snake and mouse on grid
- void setup()
- {
- gameOver = false;
- dir = STOP;
- //random snake pos on grid
- headPosX = width / 2;
- headPosY = height / 2;
- //random generates the mosue
- mouseX = rand() % width;
- mouseY = rand() % height;
- }
- void draw()
- {
- system("cls"); //Clear screen function
- for (int i = 0; i < width + 2; i++)
- cout << "#";
- cout << endl;
- //draws the board
- for (int i = 0; i < height; i++) //draws side boarders of grid
- {
- for (int j = 0; j < width; j++) //draws top boarders of grid
- {
- if (j == 0)
- cout << "#";
- //prints the head on grid
- if (i == headPosY && j == headPosX)
- cout << "O";
- //prints the mouse on grid
- else if (i == mouseY && j == mouseX)
- cout << "*";
- else
- {
- bool draw = false;
- //Tail will be made once snake eats mouse
- for (int k = 0; k < nTail; k++)
- {
- if (tailX[k] == j && tailY[k] == i)
- {
- cout << "o";
- draw = true;
- }
- }
- //if there is nothing to draw, put space
- if (!draw)
- cout << " ";
- }
- if (j == width - 1)
- cout << "#";
- }
- cout << endl;
- }
- for (int i = 0; i < width + 2; i++) //use the width + 2 since its missing it in the grid.
- cout << "#";
- cout << endl;
- }
- //Snake movement
- void input()
- {
- if(_kbhit()) //if a key is pressed
- {
- switch(_getch()) //go find the key that corresponds with dir
- {
- //Movement keys on keyboard
- case 'a':
- dir = LEFT;
- break;
- case 'd':
- dir = RIGHT;
- break;
- case 'w':
- dir = UP;
- break;
- case 's':
- dir = DOWN;
- break;
- case 'x':
- gameOver; //boolean of "gameOver" is called so game ends
- break;
- }
- }
- }
- void logic()
- {
- //2 D array for tail
- //first element of tail
- int prevX = tailX[0];
- int prevY = tailY[0];
- int prev2X, prev2Y;
- tailX[0] = headPosX;
- tailY[0] = headPosY;
- for (int i = 1; i < nTail; i++) //adds to body
- {
- //look at first segment ((CURRENT POS))
- prev2X = tailX[i];
- prev2Y = tailY[i];
- tailX[i] = prevX;
- tailY[i] = prevY;
- //update the snake length
- prevX = prev2X;
- prevY = prev2Y;
- }
- switch (dir)
- {
- case LEFT:
- headPosX--;
- break;
- case RIGHT:
- headPosX++;
- break;
- case UP:
- headPosY--;
- break;
- case DOWN:
- headPosY++;
- break;
- default:
- break;
- }
- //if (x > width || x < 0 || y > height || y < 0)
- gameOver = true;
- if (headPosX >= width) headPosX = 0;
- else if (headPosX < 0) headPosX = width - 1;
- if (headPosY >= height) headPosY = 0;
- else if (headPosY < 0) headPosY = height - 1;
- //if snake hits itself
- for (int i = 0; i < nTail; i++)
- if (tailX[i] == headPosX && tailY[i] == headPosY)
- gameOver = true;
- //Once snake eats mouse, it will randomly go somewhere else and relocate
- if (headPosX == mouseX && headPosY == mouseY)
- {
- mouseX = rand() % width;
- mouseY = rand() % height;
- nTail++;
- }
- }
- //Main instructions and goals of the game
- void instructions()
- {
- cout << " ____ __ __ _ " << endl;
- cout << " / _/___ _____/ /________ _______/ /_(_)___ ____ _____" << endl;
- cout << " / // __ \/ ___/ __/ ___/ / / / ___/ __/ / __ \/ __ \/ ___/" << endl;
- cout << " _/ // / / (__ ) /_/ / / /_/ / /__/ /_/ / /_/ / / / (__ )" << endl;
- cout << " /___/_/ /_/____/\__/_/ \__,_/\___/\__/_/\____/_/ /_/____/" << endl;
- cout << "*****************************************************************************" << endl;
- cout << " - Use the WASD keys (make sure caps lock is off) to move around on the grid " << endl;
- cout << " - Do not hit a wall " << endl;
- cout << " - Do not hit your own body " << endl;
- cout << "*****************************************************************************" << endl;
- cout << " ______ __ " << endl;
- cout << " / ____/___ ____ _/ /____ " << endl;
- cout << " / / __/ __ \/ __ `/ / ___/ " << endl;
- cout << " / /_/ / /_/ / /_/ / (__ ) " << endl;
- cout << " \____/\____/\__,_/_/____/ " << endl;
- cout << "*****************************************************************************" << endl;
- cout << " - Go around the grid and try to eat as many mice as you can " << endl;
- cout << " - Do not get frustrated with yourself " << endl;
- cout << " - Have fun playing the game" << endl;
- cout << "*****************************************************************************" << endl;
- }
- //Where the user will be prompted at the beginning
- void menu()
- {
- cout << " _____ _ _____ " <<endl;
- cout << " / ____| | | / ____|" << endl;
- cout << " | (___ _ __ __ _| |_____ | | __ __ _ _ __ ___ ___" << endl;
- cout << " \___ \| '_ \ / _` | |/ / _ \ | | |_ |/ _` | '_ ` _ \ / _ \ " << endl;
- cout << " ____) | | | | (_| | < __/ | |__| | (_| | | | | | | __/" << endl;
- cout << " |_____/|_| |_|\__,_|_|\_\___| \_____|\__,_|_| |_| |_|\___|" << endl;
- cout << " \t\nCreated by Olivia Frith " << endl;
- cout << "*************************************************" << endl;
- cout << " Choose 1 to play the Snake Game " << endl;
- cout << " Choose 2 to see the game instructions and goals " << endl;
- cout << " Choose 3 to exit the game " << endl;
- cout << "*************************************************" << endl;
- cout << " Your choice:";
- cin >> choice;
- cout << "\n\n\n\n";
- //Shows instructions and game goals
- if(choice == 2)
- {
- instructions();
- system("pause");
- }
- //Exits game
- if(choice == 3)
- {
- exit (EXIT_SUCCESS);
- }
- }
- int main()
- {
- setup();
- menu();
- do
- {
- draw();
- input();
- logic();
- this_thread::sleep_for(std::chrono::milliseconds(10));
- }while(!gameOver == false);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement