C++ Snake Game I Made in Class
C++ Snake Game I Made in Class
C++ Snake Game I Made in Class
#include <iostream>
#include <windows.h>
#include <math.h>
#define BOARD_SIZE_X 35
#define BOARD_SIZE_Y 20
#define GOAL_LEN 50
#define GAME_SPEED 500
class Snake {
public:
Snake();
~Snake();
void Start(void);
private:
inline void Generate(void);
char heading;
int len;
int x,y;
int **map;
};
/*
map decryption:
0 - empty space
(-1) - apple
1 - snake head
2,3,4.. - snake body
*/
////////////////////////////////////
Snake::Snake() {
heading = 'N';
len = 5;
x = (int)round((float)BOARD_SIZE_X/2);
y = (int)round((float)BOARD_SIZE_Y/2);
map[x][y]=1; // head
};
Snake::~Snake() {
for (int i=0;i<BOARD_SIZE_Y;i++) {
delete[] map[i];
};
delete[] map;
};
////////////////////////////////////
void Snake::Start(void) {
this->Generate();
while (true) {
Sleep(GAME_SPEED);
this->Read();
this->Move();
this->Draw();
};
};
////
void Snake::Generate(void) {
int temp = rand() % (BOARD_SIZE_X * BOARD_SIZE_Y);
map[temp %BOARD_SIZE_X][(int)floor((float)(BOARD_SIZE_X*BOARD_SIZE_Y)/temp)]
= -1; // new apple
};
////
void Snake::Read(void) {
if (GetAsyncKeyState(38)) { //VK_UP
heading = 'N';
}
else if (GetAsyncKeyState(40)) { //VK_DOWN
heading = 'S';
}
else if (GetAsyncKeyState(37)) { //VK_LEFT
heading = 'W';
}
else if (GetAsyncKeyState(39)) { //VK_RIGHT
heading = 'E';
};
};
////
void Snake::Move(void) {
int i;
for (int j=0;j<BOARD_SIZE_Y;j++) {
for (i=0;i<BOARD_SIZE_X;i++) {
if (map[i][j]==len) {
map[i][j]=0;
}
else if (map[i][j]>0) {
map[i][j]+=1;
};
};
};
Snake::Check(x,y);
};
////
if (map[a][b]==-1) {
map[a][b]= 1;
this->len+=1;
if (this->len>GOAL_LEN-1) throw 201;
this->Generate();
} else if (map[a][b]>0) {
throw 202;
} else map[a][b]=1;
};
////
void Snake::Draw(void) {
system("cls");
if (debug==1) {
cout << "Snake's head position is (" << x << "," << y << ") and is
heading " << heading << endl;
};
cout << "Your Snake\'s size is " << this->len << " of " << GOAL_LEN << ".
Reach length of " << GOAL_LEN << " to win."<< endl;
for (int i=0;i<BOARD_SIZE_X+2;i++) {
cout << "=";
};
cout << endl;
int i;
for (int j=0;j<BOARD_SIZE_Y;j++) {
cout << "|";
for (i=0;i<BOARD_SIZE_X;i++) {
if (map[i][j]== -1) {
cout << "A";
}
else if (map[i][j]== 0) {
cout << " ";
}
else if (map[i][j]== 1) {
cout << "X";
}
else {
cout << "O";
};
};
cout << "|" <<endl;
};
////////////////////////////////////
Snake someSnake;
try {
someSnake.Start();
}
catch (int e) {
switch (e) {
case 101:
cout << "Error! Unknown direction." << endl;
break;
case 201:
cout << "Congratulation! You've reached size of 100!";
break;
case 202:
cout << "You've lost! Never bite yourself..";
break;
case 203:
cout << "You've lost! You hit the wall..";
break;
default:
cout << "Unknown Error! Program execution stoped." << endl;
break;
};
};
return 0;
};