In: Computer Science
Create a Snake game project in C++ using all these which are given below.
1. Classes
2. structures
3. files ( MULTFILLING AND FILE HANDLING )
4. loops
5. conditions
6. switch
7. functions
8. array
Note: Also upload the zip file.
//This program has successfully run on
//operating system - windows 10 64 bit
//IDE - Code::Blocks 16.01
#include <iostream>
#include <windows.h>
#include <winuser.h>
using namespace std;
struct po_si_tion
{
int x_axis,y_axis;
};
//Defining field class
class fld_cls
{
static const int ht; //ht = height
static const int wth; //wth = width
char ** fld;
fld_cls(const fld_cls &);
fld_cls operator=(const fld_cls &);
public:
fld_cls()
{
fld = new char*[fld_cls::ht]; //fld = field
for(int i = 0; i < fld_cls::ht; ++i)
{
fld[i] = new char[fld_cls::wth];
}
}
~fld_cls()
{
for(int i = 0; i < fld_cls::ht; ++i)
{
delete[] fld[i];
}
delete[] fld;
}
//to clear snake
void to_clear()
{
for(int i = 0; i < ht; ++i)
{
for(int r = 0; r < wth; ++r)
{
fld[i][r] = ' ';
}
}
}
//printing snake
void to_print()
{
for(int i = 0; i < ht; ++i)
{
for(int r = 0; r < wth; ++r)
{
cout << fld[i][r];
}
cout << endl;
}
}
int get_wth() const {return wth;}
int get_ht() const {return ht;}
//to draw sanke
void to_draw(int y_axis, int x_axis, char what)
{
//y_axis = (y_axis < 0) ? 0 : (y_axis >= ht ? ht - 1 : y_axis);
//x_axis = (x_axis < 0) ? 0 : (x_axis >= wth ? wth - 1 : x_axis);
fld[y_axis][x_axis] = what;
}
} fld;
//food represented by letter 'X'
class food_cls
{
po_si_tion pos;
char symbol;
public:
food_cls(): symbol('X'), pos()
{
pos.x_axis = pos.y_axis = -1;
}
//setting position of snake
void set_pos(int x_axis, int y_axis)
{
pos.x_axis = x_axis;
pos.y_axis = y_axis;
}
//resetting position of snake
void re_po_si_tion(const fld_cls & fld)
{
pos.x_axis = rand() % fld.get_wth();
pos.y_axis = rand() % fld.get_ht();
}
int get_x_axis() const {return pos.x_axis;}
int get_y_axis() const {return pos.y_axis;}
char get_symbol() const {return symbol;}
} food;
//class for giving direction to snake
class snake_cls
{
enum {UP, DOWN, LEFT, RIGHT} dir;
char symbol, head_symbol;
po_si_tion pos[100];
po_si_tion & head;
int speed;
int size;
bool can_turn;
public:
snake_cls(int x_axis, int y_axis):
symbol('#'), head_symbol('@'), pos(),//snake head is '@' and tail is '#'
speed(1), size(1), dir(RIGHT),
head(pos[0]), can_turn(true)
{
pos[0].x_axis = x_axis;
pos[0].y_axis = y_axis;
}
//Display random food by symbol 'X'
bool check_food(const food_cls & food)
{
if(food.get_x_axis() == head.x_axis && food.get_y_axis() == head.y_axis)
{
size += 1;
return true;
}
return false;
}
//Function to take direction from user/navigation for snake
void get_input(const fld_cls & fld)
{
if(GetAsyncKeyState(VK_UP) && dir != DOWN)
{
dir = UP;
}
if(GetAsyncKeyState(VK_DOWN) && dir != UP)
{
dir = DOWN;
}
if(GetAsyncKeyState(VK_LEFT) && dir != RIGHT)
{
dir = LEFT;
}
if(GetAsyncKeyState(VK_RIGHT) && dir != LEFT)
{
dir = RIGHT;
}
}
void to_move(const fld_cls & fld)
{
po_si_tion nex_axist = {0, 0};
switch(dir)
{
case UP:
nex_axist.y_axis = -speed;
break;
case DOWN:
nex_axist.y_axis = speed;
break;
case LEFT:
nex_axist.x_axis = -speed;
break;
case RIGHT:
nex_axist.x_axis = speed;
}
for(int i = size - 1; i > 0; --i)
{
pos[i] = pos[i-1];
}
head.x_axis += nex_axist.x_axis;
head.y_axis += nex_axist.y_axis;
if(head.x_axis < 0 || head.y_axis < 0 || head.x_axis >= fld.get_wth() || head.y_axis >= fld.get_ht())
{
throw "DEADD!!!!";
}
}
void to_draw(fld_cls & fld)
{
for(int i = 0; i < size; ++i)
{
if(i == 0)
{
fld.to_draw(pos[i].y_axis, pos[i].x_axis, head_symbol);
}
else
{
fld.to_draw(pos[i].y_axis, pos[i].x_axis, symbol);
}
}
}
int get_x_axis() const { return head.x_axis; }
int get_y_axis() const { return head.y_axis; }
char get_symbol() const { return symbol; }
} snake(1, 1);
const int fld_cls::ht = 24;
const int fld_cls::wth = 79;
int main()
{
fld.to_clear();
food.set_pos(5, 5); //setting food position
while(1)
{
fld.to_clear();
snake.get_input(fld);
try
{
snake.to_move(fld);
}
catch (const char * er)
{
fld.to_clear();
cerr << er << endl;
system("pause");
return -1;
}
snake.to_draw(fld);
fld.to_draw(food.get_y_axis(), food.get_x_axis(), food.get_symbol());
if(snake.check_food(food))
{
food.re_po_si_tion(fld);
}
fld.to_print();
Sleep(1000/30);
system("cls");
}
return 0;
}