Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include "snake.h"
- //#define DEBUG
- //////////////////////////////////////DEFAULT SETTINGS, STRUCTURE
- #define MAPSIZE 50
- using namespace std;
- bool Game = TRUE;
- const int Coord_X = MAPSIZE;
- const int Coord_Y = Coord_X / 2;
- int CharCoord_X = 0;
- int CharCoord_Y = 0;
- int XS1, XS2, YS1, YS2;
- int Score = 0;
- #ifdef DEBUG
- int *BonusCoord_X = new int();
- int *BonusCoord_Y = new int();
- #else
- int BonusCoord_X = 0;
- int BonusCoord_Y = 0;
- #endif
- bool TOP, BOTTOM, LEFT, RIGHT;
- //////////////////////////////////////CONSTRUCTOR
- Snake::Snake()
- {
- srand(time(0));
- }
- //////////////////////////////////////DESTRUCTOR
- Snake::~Snake()
- {
- #ifdef DEBUG
- delete BonusCoord_X, BonusCoord_Y;
- BonusCoord_X = NULL;
- BonusCoord_Y = NULL;
- #endif
- }
- //////////////////////////////////////Random()
- void Snake::Random()
- {
- BonusCoord_X = rand() % (Coord_X - 10) + 5;
- BonusCoord_Y = rand() % (Coord_Y - 10) + 5;
- }
- //////////////////////////////////////GameOver()
- void Snake::GameOver()
- {
- Game = FALSE;
- cout << "#############[GAME OVER]#############" << endl << "[Enter - New Game]" << endl << "SCORE: " << Score << endl;
- cin.get();
- TOP = FALSE;
- BOTTOM = FALSE;
- LEFT = FALSE;
- RIGHT = FALSE;
- CharCoord_X = Coord_X / 2;
- CharCoord_Y = Coord_Y / 2;
- #ifdef DEBUG
- *BonusCoord_X = rand() % (Coord_X - 2) + 2;
- *BonusCoord_Y = rand() % (Coord_Y - 2) + 2;
- #else
- Random();
- #endif
- Score = 0;
- Game = TRUE;
- }
- //////////////////////////////////////Keyboard()
- void Snake::Keyboard()
- {
- while (_kbhit())
- {
- char arrows = _getch();
- switch (arrows)
- {
- case 'w':
- if (!(BOTTOM))
- {
- TOP = TRUE;
- BOTTOM = FALSE;
- LEFT = FALSE;
- RIGHT = FALSE;
- }
- break;
- case 's':
- if (!(TOP))
- {
- TOP = FALSE;
- BOTTOM = TRUE;
- LEFT = FALSE;
- RIGHT = FALSE;
- }
- break;
- case 'a':
- if (!(RIGHT))
- {
- TOP = FALSE;
- BOTTOM = FALSE;
- LEFT = TRUE;
- RIGHT = FALSE;
- }
- break;
- case 'd':
- if (!(LEFT))
- {
- TOP = FALSE;
- BOTTOM = FALSE;
- LEFT = FALSE;
- RIGHT = TRUE;
- }
- break;
- default:
- break;
- }
- }
- if (TOP)
- if ((CharCoord_Y > 1))
- CharCoord_Y -= 1;
- else
- GameOver();
- if (BOTTOM)
- if ((CharCoord_Y < Coord_Y - 2))
- CharCoord_Y += 1;
- else
- GameOver();
- if (LEFT)
- if ((CharCoord_X > 1))
- CharCoord_X -= 1;
- else
- GameOver();
- if (RIGHT)
- if ((CharCoord_X < Coord_X - 2))
- CharCoord_X += 1;
- else
- GameOver();
- }
- //////////////////////////////////////CoordStore()
- void Snake::CoordStore(int opt, int x, int y)
- {
- switch (opt)
- {
- case 0:
- {
- XS1 = x;
- YS1 = y;
- }
- break;
- case 1:
- {
- XS2 = x;
- YS2 = y;
- if (XS1 == XS2 && YS1 == YS2)
- {
- Random();
- Score += 5;
- }
- }
- break;
- default:
- break;
- }
- }
- //////////////////////////////////////InitGame()
- void Snake::InitGame(const int x, const int y)
- {
- CharCoord_X = Coord_X / 2;
- CharCoord_Y = Coord_Y / 2;
- #ifdef DEBUG
- *BonusCoord_X = rand() % (Coord_X - 2) + 2;
- *BonusCoord_Y = rand() % (Coord_Y - 2) + 2;
- #else
- Random();
- #endif
- while (Game)
- {
- if (Score > 4*4 && Score < 10*4)
- {
- system("color 0a");
- }
- else if (Score > 9*4 && Score < 14*4)
- {
- system("color 0c");
- }
- Keyboard();
- CoordStore(0, BonusCoord_X, BonusCoord_Y);
- cout << "Score: " << Score << endl;
- for (int i = 0; i < y; i++)
- {
- for (int j = 0; j < x; j++)
- {
- if (i < 1)
- {
- cout << "#";
- }
- else if (i > 0 && i < Coord_Y - 1)
- {
- if (j > 0 && j < Coord_X - 1)
- {
- if (j == CharCoord_X && i == CharCoord_Y)
- {
- CoordStore(1, j, i);
- cout << "O";
- }
- #ifdef DEBUG
- else if (j == *BonusCoord_X && i == *BonusCoord_Y)
- {
- CoordStore(1, *BonusCoord_X, *BonusCoord_Y);
- cout << "F";
- }
- #else
- else if (j == BonusCoord_X && i == BonusCoord_Y)
- {
- cout << "F";
- }
- #endif
- else
- {
- cout << " ";
- }
- }
- else
- {
- cout << "#";
- }
- }
- else
- {
- cout << "#";
- }
- }
- cout << endl;
- }
- system("cls");
- Sleep(30);
- }
- }
- //////////////////////////////////////main()
- int main()
- {
- Snake _Snake;
- _Snake.InitGame(Coord_X, Coord_Y);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement