Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <time.h>
- #include <stdlib.h>
- #include <sstream>
- #include <vector>
- using namespace std;
- #define UP_KEY 'w'
- #define DOWN_KEY 's'
- #define LEFT_KEY 'a'
- #define RIGHT_KEY 'd'
- //Ascii art title and Menu to select gameplay
- void menu()
- {
- cout << R"(
- _______ _ _______ _ _______ _______ _______ _______ _______
- ( ____ \( ( /|( ___ )| \ /\( ____ \ ( ____ \( ___ )( )( ____ \
- | ( \/| \ ( || ( ) || \ / /| ( \/ | ( \/| ( ) || () () || ( \/
- | (_____ | \ | || (___) || (_/ / | (__ | | | (___) || || || || (__
- (_____ )| (\ \) || ___ || _ ( | __) | | ____ | ___ || |(_)| || __)
- ) || | \ || ( ) || ( \ \ | ( | | \_ )| ( ) || | | || (
- /\____) || ) \ || ) ( || / \ \| (____/\ | (___) || ) ( || ) ( || (____/\
- \_______)|/ )_)|/ \||_/ \/(_______/ (_______)|/ \||/ \|(_______/
- )" << std::endl;
- cout << "\n-------------------------------------------------\n";
- cout << "Select your gamemode:\n\n";
- cout << "\t-Enter 1 play the Snake Game\n";
- cout << "\t-Enter 2 to read the rules\n";
- cout << "\t-Enter 3 to exit the game\n";
- cout << "\n-------------------------------------------------\n";
- cout << "\tYour Choice: ";
- }
- void clearScreen(int newline)
- {
- for (int y = 0; y < newline; y++)
- {
- cout << "\n" << endl;
- }
- }
- /*//snake mouvement
- int playerMovement(char WASD, int v, int h)
- {
- char ok = 'y';
- //allows the player to move
- do
- {
- switch (WASD)
- {
- case UP_KEY:
- v = v - 1;
- ok = 'y';
- return v;
- break;
- case DOWN_KEY:
- v = v + 1;
- ok = 'y';
- return v;
- break;
- case LEFT_KEY:
- h = h - 1;
- ok = 'y';
- return h;
- break;
- case RIGHT_KEY:
- h = h + 1;
- ok = 'y';
- return h;
- break;
- default:
- cout << "\nPlease enter a vaild option: ";
- cin >> WASD;
- cout << '\n\n';
- ok = 'n';
- break;
- }
- } while (ok == 'n');
- }
- */
- //Contains vectors, the mouvements,
- class Snake //sneaky boy
- {
- public:
- Snake()
- {
- snakePos.push_back(make_pair(0, 0));
- }
- vector <pair<int, int >> getPos() const
- {
- return snakePos;
- }
- Snake(int x, int y)
- {
- snakePos.push_back(make_pair(x, y));
- }
- private:
- vector <pair<int, int>> snakePos;
- };
- /*//Contains food for Snake
- class Mouse
- {
- public:
- Mouse()
- {
- mousePos.push_back();
- }
- vector <int> getPos() const
- {
- return mousePos;
- }
- Mouse()
- {
- mousePos.push_back();
- }
- private:
- vector <int> mousePos;
- };
- */
- //Contains the empty grid, can make several grids
- class Board
- {
- public:
- //Default constructor
- Board()
- {
- width = 50;
- height = 50;
- int randX = (rand() % (width - 1 - 1 + 1)) + 1;
- int randY = (rand() % (height - 1 - 1 + 1)) + 1;
- snake = Snake(randX, randY);
- }
- //Overload constructor, change size of board if I have time
- Board(int defaultWidth, int defaultHeight)
- {
- height = defaultHeight;
- width = defaultWidth;
- int randX = (rand() % (width - 1 - 1 + 1)) + 1;
- int randY = (rand() % (height - 1 - 1 + 1)) + 1;
- snake = Snake(randX, randY);
- }
- //Height Getter and Setter
- void hSetter(int newHeight) { height = newHeight; }
- int hGetter() { return height; }
- //Function to draw everything
- void draw()
- {
- clearScreen(height);
- //Creates Rectangle and Generates Snake
- for (int i = 0; i < width; i++) //Loop
- {
- for (int j = 0; j < height; j++) //Iterates
- {
- for (int x = 0; x < snake.getPos().size(); ++x)
- {
- if (snake.getPos()[x].first == i && snake.getPos()[x].second == j)
- {
- cout << "o"; //draws the Head of Snake
- }
- //Draw walls if it detects, if not do nothing
- else if (i == 0 || i == width - 1 ||
- j == 0 || j == height - 1)
- {
- cout << "|"; //Adds wall if variable == to width or height
- }
- else
- cout << " ";
- }
- }
- cout << endl;
- }
- }
- //Always stay the same
- private:
- int width;
- int height;
- Snake snake;
- };
- //Everything is put together here
- int main()
- {
- //calls menu
- menu();
- //Current time in mm
- srand(time(NULL));
- //Prints board
- Board mainBoard;
- mainBoard.draw();
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement