patterncppMinor
Snake game for C++
Viewed 0 times
gameforsnake
Problem
This is my first game that I made. How could this be improved?
```
#include
#include
#include
#include
#include
using namespace std;
int snakelenght=2;
char direction='d';
char field[20][30];
class Past
{
int m_nx;
int m_ny;
public:
void Init(int x,int y)
{
m_nx=x;
m_ny=y;
}
int GetX(){return m_nx;}
int GetY(){return m_ny;}
}past[50];
void MakeField();
void DrawField();
void Random(int &x, int &y);
bool IsFree(int &x,int &y);
void MakeFood();
bool Food(int &x,int &y);
bool Move(int &x, int &y);
void Erase(int x, int y);
void Past(int x,int y);
void Key(int &x,int &y);
bool End(int &x, int &y);
int main()
{
int x=10,y=15;
MakeField();
MakeFood();
DrawField();
while(End(x,y))
{
cout << direction;
if(kbhit())
{
direction=getch();
Key(x,y);
}
else
Key(x,y);
if(!Move(x,y))
break;
Past(x,y);
system("cls");
DrawField();
usleep(1000);
}
cout << "End!";
return 0;
}
void MakeField()
{
for(int i=0;i<20;i++)
for(int j=0;j<30;j++)
field[i][j]=' ';
}
void DrawField()
{
for(int i=0;i<=30;i++)
cout << "- ";
cout << endl;
for(int i=0;i<20;i++)
{
cout << "| ";
for(int j=0;j<30;j++)
cout << field[i][j] << " ";
cout << "|"<< endl;
}
for(int i=0;i<=30;i++)
cout << "- ";
cout << endl;
}
void Random(int &x, int &y)
{
srand(time(0));
x=rand()%10;
y=rand()%10;
}
bool IsFree(int &x,int &y)
{
if(field[x][y]==' ')
return 1;
return 0;
}
void MakeFood()
{
again:
int x=0,y=0;
Random(x,y);
if(IsFree(x,y))
field[x][y]='H';
else
goto again;
}
bool Food(int &x,int &y)
{
if(field[x][y]=='H')
{
snakelenght++;
return 1;
}
return 0;
}
bool Move(int &x, int &y)
{
if(Food(x,y))
{
```
#include
#include
#include
#include
#include
using namespace std;
int snakelenght=2;
char direction='d';
char field[20][30];
class Past
{
int m_nx;
int m_ny;
public:
void Init(int x,int y)
{
m_nx=x;
m_ny=y;
}
int GetX(){return m_nx;}
int GetY(){return m_ny;}
}past[50];
void MakeField();
void DrawField();
void Random(int &x, int &y);
bool IsFree(int &x,int &y);
void MakeFood();
bool Food(int &x,int &y);
bool Move(int &x, int &y);
void Erase(int x, int y);
void Past(int x,int y);
void Key(int &x,int &y);
bool End(int &x, int &y);
int main()
{
int x=10,y=15;
MakeField();
MakeFood();
DrawField();
while(End(x,y))
{
cout << direction;
if(kbhit())
{
direction=getch();
Key(x,y);
}
else
Key(x,y);
if(!Move(x,y))
break;
Past(x,y);
system("cls");
DrawField();
usleep(1000);
}
cout << "End!";
return 0;
}
void MakeField()
{
for(int i=0;i<20;i++)
for(int j=0;j<30;j++)
field[i][j]=' ';
}
void DrawField()
{
for(int i=0;i<=30;i++)
cout << "- ";
cout << endl;
for(int i=0;i<20;i++)
{
cout << "| ";
for(int j=0;j<30;j++)
cout << field[i][j] << " ";
cout << "|"<< endl;
}
for(int i=0;i<=30;i++)
cout << "- ";
cout << endl;
}
void Random(int &x, int &y)
{
srand(time(0));
x=rand()%10;
y=rand()%10;
}
bool IsFree(int &x,int &y)
{
if(field[x][y]==' ')
return 1;
return 0;
}
void MakeFood()
{
again:
int x=0,y=0;
Random(x,y);
if(IsFree(x,y))
field[x][y]='H';
else
goto again;
}
bool Food(int &x,int &y)
{
if(field[x][y]=='H')
{
snakelenght++;
return 1;
}
return 0;
}
bool Move(int &x, int &y)
{
if(Food(x,y))
{
Solution
using namespace std is considered a bad practice. Don't do it.The magic numbers 20 and 30 for the game dimensions would be better as global constants. That way you can easily extend the game to other dimensions.
Instead of:
#include
#include You should use:
#include
#include In the boolean functions, instead of returning 0 and 1 values,
it would be more natural to return
false and true.In the
case blocks of the switch statement,you don't really need the braces,
you could write simpler this way:
switch(direction)
{
case 'd':
y++;
break;
case 'a':
y--;
break;The condition in the
End function looks odd:bool End(int &x, int &y)
{
if (field[x][y] == ' ' ||'H')
return 1;
return 0;
}The condition will always evaluate to true.
You can correct and simplify:
bool End(int &x, int &y)
{
return field[x][y] == ' ' || field[x][y] == 'H';
}Code Snippets
#include <time.h>
#include <stdlib.h>#include <ctime>
#include <cstdlib>switch(direction)
{
case 'd':
y++;
break;
case 'a':
y--;
break;bool End(int &x, int &y)
{
if (field[x][y] == ' ' ||'H')
return 1;
return 0;
}bool End(int &x, int &y)
{
return field[x][y] == ' ' || field[x][y] == 'H';
}Context
StackExchange Code Review Q#77831, answer score: 4
Revisions (0)
No revisions yet.