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>
- #include <windows.h>
- #include <chrono>
- #include <thread>
- #include <cstdlib>
- using namespace std;
- #define UP_KEY 0x57 // W
- #define DOWN_KEY 0x53 // S
- #define LEFT_KEY 0x41 // A
- #define RIGHT_KEY 0x44 // D
- //Represent the state of Snake
- enum class direction
- {
- UP,
- DOWN,
- LEFT,
- RIGHT,
- };
- //Generates the mouse in the grid without generating under a snake
- pair<int, int> randMousePos(vector <pair<int, int >> snakePos, int width, int height)
- {
- for (;;)
- {
- int randX = (rand() % (width - 1 - 1 + 1)) + 1;
- int randY = (rand() % (height - 1 - 1 + 1)) + 1;
- if (find(snakePos.begin(), snakePos.end(), make_pair(randX, randY)) == snakePos.end())
- {
- return make_pair(randX, randY);
- }
- }
- }
- //Contains vectors, the mouvements,
- class Snake //sneaky boy
- {
- public:
- Snake()
- {
- snakePos.push_back(make_pair(0, 0));
- snakeMovement = direction::RIGHT;
- }
- vector <pair<int, int >> getPos() const
- {
- return snakePos;
- }
- Snake(int x, int y)
- {
- snakePos.push_back(make_pair(x, y));
- snakeMovement = direction::RIGHT;
- }
- void setMovement(direction move)
- {
- snakeMovement = move;
- }
- //Movement thread (runs at same time as the board)
- void processInputs()
- {
- if (GetKeyState(UP_KEY) & 0x8000)
- {
- setMovement(direction::UP);
- //cout << " Pressed up" << endl;
- }
- else if (GetKeyState(DOWN_KEY) & 0x8000)
- {
- setMovement(direction::DOWN);
- //cout << " Pressed down" << endl;
- }
- else if (GetKeyState(LEFT_KEY) & 0x8000)
- {
- setMovement(direction::LEFT);
- //cout << " Pressed left" << endl;
- }
- else if (GetKeyState(RIGHT_KEY) & 0x8000)
- {
- setMovement(direction::RIGHT);
- //cout << " Pressed right" << endl;
- }
- }
- private:
- vector <pair<int, int>> snakePos;
- direction snakeMovement;
- };
- //Contains food for Snake
- class Mouse
- {
- public:
- Mouse() { posY = 0; posX = 0; }
- Mouse(int height, int width)
- {
- posX = (rand() % (width - 1 - 1 + 1)) + 1; //counts from row 2 of the board to 47 (ignores the walls)
- posY = (rand() % (height - 1 - 1 + 1)) + 1;
- }
- int getPosY() const { return posY; }
- int getPosX() const { return posX; }
- private:
- int posY;
- int posX;
- };
- //Contains the empty grid, can make several grids
- class Board
- {
- public:
- //Default constructor
- Board()
- {
- width = 50;
- height = 50;
- int randX = (rand() % (width - 2 - 1 + 1)) + 1;
- int randY = (rand() % (height - 2 - 1 + 1)) + 1;
- snake = Snake(randX, randY);
- pair<int, int> mousePos = randMousePos(snake.getPos(), width, height);
- mouse = Mouse(mousePos.first, mousePos.second);
- }
- //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);
- pair<int, int> mousePos = randMousePos(snake.getPos(), width, height);
- mouse = Mouse(mousePos.first, mousePos.second);
- }
- //Returns snake object so it can be used
- Snake& getSnake()
- {
- return snake;
- }
- void process()
- {
- snake.processInputs();
- }
- //Height Getter and Setter
- void hSetter(int newHeight) { height = newHeight; }
- int hGetter() { return height; }
- //Function to draw everything
- void draw()
- {
- //clearScreen(height); (Not working?)
- //Creates Rectangle and Generates Snake
- for (int i = 0; i < width; i++) //Loop
- {
- for (int j = 0; j < height; j++) //Iterates
- {
- if (i == mouse.getPosX() && j == mouse.getPosY())
- {
- cout << "*";
- }
- else
- {
- for (int x = 0; x < snake.getPos().size(); ++x)
- {
- if (i == 0 || i == width - 1 ||
- j == 0 || j == height - 1)
- {
- cout << "#"; //Adds wall if variable == to width or height
- }
- else 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
- cout << " ";
- }
- }
- }
- cout << endl;
- }
- }
- private:
- int width;
- int height;
- Snake snake;
- Mouse mouse;
- };
- void instructions()
- {
- cout << R"(
- ______ __
- / ____/___ ____ _/ /____
- / / __/ __ \/ __ `/ / ___/
- / /_/ / /_/ / /_/ / (__ )
- \____/\____/\__,_/_/____/
- )" << endl;
- cout << "\n-------------------------------------------------\n";
- cout << " - Eat as many apples as you can" << endl;
- cout << " - Pay attention --> Do not distract yourself or get distracted" << endl;
- cout << " - Have a good time and enjoy the game!" << endl;
- cout << "\n-------------------------------------------------\n";
- cout << R"(
- ____ __ __ _
- / _/___ _____/ /________ _______/ /_(_)___ ____ _____
- / // __ \/ ___/ __/ ___/ / / / ___/ __/ / __ \/ __ \/ ___/
- _/ // / / (__ ) /_/ / / /_/ / /__/ /_/ / /_/ / / / (__ )
- /___/_/ /_/____/\__/_/ \__,_/\___/\__/_/\____/_/ /_/____/
- )" << endl;
- cout << "\n-------------------------------------------------\n";
- cout << " - Use WASD to move around the grid" << endl;
- cout << " - Do not hit a wall = Game Over" << endl;
- cout << " - Do not run into yourself = Game over" << endl;
- cout << "\n-------------------------------------------------\n";
- }
- //Ascii art title and Menu to select gameplay
- void menu()
- {
- cout << R"(
- _____ _ _____
- / ____| | | / ____|
- | (___ _ __ __ _| | _____ | | __ __ _ _ __ ___ ___
- \___ \| '_ \ / _` | |/ / _ \ | | |_ |/ _` | '_ ` _ \ / _ \
- ____) | | | | (_| | < __/ | |__| | (_| | | | | | | __/
- |_____/|_| |_|\__,_|_|\_\___| \_____|\__,_|_| |_| |_|\___|
- )" << endl;
- int choice;
- cout << "\n-------------------------------------------------\n";
- cout << "Select your gamemode:\n\n";
- cout << "\t-Enter 1 play the Snake Game\n";
- cout << "\t-Enter 2 to see the instructions and rules\n";
- cout << "\t-Enter 3 to exit the game\n";
- cout << "\n-------------------------------------------------\n";
- cout << "\tYour Choice: ";
- cin >> choice;
- switch (choice)
- {
- case 1:
- int 1 = ;
- break;
- case 2:
- instructions();
- break;
- case 3:
- exit(EXIT_SUCCESS);
- }
- }
- void clearScreen(int newline)
- {
- for (int y = 0; y < newline; y++)
- {
- cout << "\n" << endl;
- }
- }
- //Everything is put together here
- int main()
- {
- srand(time(NULL));
- //SetConsoleTitleW(L"Sneaky sneaky app"); **EXTRA**
- menu(); //calls menu
- Board mainBoard; //Prints board
- //Main Game loop
- int lastTime = time(NULL);
- while (true)
- {
- mainBoard.process();
- if (time(NULL) != lastTime)
- {
- mainBoard.draw();
- }
- lastTime = time(NULL);
- }
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement