Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef GAME_HPP
- #define GAME_HPP
- #include <iostream>
- #include <cstdlib>
- #include <conio.h>
- #include <windows.h>
- class game;
- class snake
- {
- public:
- snake();
- ~snake();
- void addScore();
- void addTail();
- int returnScore();
- int returnTail();
- private:
- int score;
- int tail;
- };
- class game
- {
- public:
- game();
- void Graphics(); //Funciton to render the game board
- void GameLogic(); //Function to compute how to operatore the snake
- void Apple(); //Function to generate a new apple randomly on the map
- void Clear(); //Function to clear the screen
- bool appleTouched(); //Function to determine if the apple was consumed by the snake
- bool GameOver(); //Function to determine wether the game has ended or not
- private:
- snake snake;
- int width;
- int height;
- int x, y, fruitx, fruity;
- bool gameStatus;
- bool appleExists;
- friend class snake;
- };
- /*-------------------------------------------------------------------Snake------------------------------------------------------------------------------------*/
- snake::snake()
- {
- score = 0;
- tail = 1;
- }
- snake::~snake()
- {}
- void snake::addScore()
- {
- score += 10;
- }
- void snake::addTail()
- {
- tail += 1;
- }
- int snake::returnScore()
- {
- return score;
- }
- int snake::returnTail()
- {
- return tail;
- }
- /*-------------------------------------------------------------------Game-------------------------------------------------------------------------------------*/
- game::game()
- {
- width = 20;
- height = 20;
- x = width / 2;
- y = height / 2;
- gameStatus = false;
- appleExists = true;
- fruitx = rand() % width;
- fruity = rand() % height;
- }
- void game::Graphics()
- {
- system("cls");
- //Printing the board top
- for (int i = 0; i < width + 1; i++)
- {
- std::cout << "#";
- }
- std::cout << std::endl;
- for (int i = 0; i < height; i++)
- {
- for (int j = 0; j < width; j++)
- {
- //Print out sides of the board
- if (j == 0 || j == 19)
- {
- std::cout << "#";
- }
- //Print out Snake
- if (i == y && j == x)
- {
- std::cout << "O";
- }
- //Print out the fruit
- else if (i == fruitx && j == fruity)
- {
- std::cout << "F";
- }
- else
- {
- std::cout << " ";
- }
- };
- std::cout << std::endl;
- }
- //Print out board bottom
- for (int i = 0; i < width + 1; i++)
- {
- std::cout << "#";
- }
- std::cout << std::endl;
- //Print out score and tail length
- std::cout << "Score: " << snake.returnScore();
- std::cout << std::endl;
- std::cout << "Tail Length: " << snake.returnTail();
- std::cout << std::endl;
- }
- void game::GameLogic()
- {
- if (_kbhit())
- {
- switch (_getch())
- {
- case 'w':
- y--;
- break;
- case 'd':
- x++;
- break;
- case 's':
- y++;
- break;
- case 'a':
- x--;
- break;
- }
- }
- if (x > width || x < 0 || y > height || y < 0)
- {
- gameStatus = true;
- }
- if (x == fruitx && y == fruity)
- {
- snake.addScore();
- snake.addTail();
- }
- }
- void game::Apple()
- {
- if(appleExists == false)
- {
- fruitx = rand() % width;
- fruity = rand() % height;
- appleExists = true;
- }
- else if (appleTouched())
- {
- appleExists == false;
- }
- }
- bool game::appleTouched()
- {
- if (x == fruitx && y == fruity)
- {
- snake.addScore();
- snake.addTail();
- return true;
- }
- }
- bool game::GameOver()
- {
- return gameStatus;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement