patterncModerate
Snake game in C
Viewed 0 times
gamesnakestackoverflow
Problem
Here is a very basic Snake game in C, which I just want to make better. The game is working perfectly but it is very annoying because when playing it, it is always blinking. I hope that somebody could try it in their compiler to see how annoying it is. How can I improve this?
Here is a screen shot of the game:
Of course it works, but I just need some advice about the design.
```
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
int length;
int bend_no;
int len;
char key;
void record();
void load();
int life;
void Delay(long double);
void Move();
void Food();
int Score();
void Print();
void gotoxy(int x, int y);
void GotoXY(int x,int y);
void Bend();
void Boarder();
void Down();
void Left();
void Up();
void Right();
void ExitGame();
int Scoreonly();
struct coordinate{
int x;
int y;
int direction;
};
typedef struct coordinate coordinate;
coordinate head, bend[500],food,body[30];
int main()
{
char key;
Print();
system("cls");
load();
length=5;
head.x=25;
head.y=20;
head.direction=RIGHT;
Boarder();
Food(); //to generate food coordinates initially
life=3; //number of extra lives
bend[0]=head;
Move(); //initialing initial bend coordinate
return 0;
}
void Move()
{
int a,i;
do{
Food();
fflush(stdin);
len=0;
for(i=0;i clock());
}
void load(){
int row,col,r,c,q;
gotoxy(36,14);
printf("loading...");
gotoxy(30,15);
for(r=1;r=70||head.y=30||check!=0)
{
life--;
if(life>=0)
{
head.x=25;
head.y=20;
bend_no=0;
head.direction=RIGHT;
Move();
}
else
{
system("cls");
printf("All lives completed\nBetter Luck Next Time!!!\nPress any key to quit the game\n");
record();
Here is a screen shot of the game:
Of course it works, but I just need some advice about the design.
```
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
int length;
int bend_no;
int len;
char key;
void record();
void load();
int life;
void Delay(long double);
void Move();
void Food();
int Score();
void Print();
void gotoxy(int x, int y);
void GotoXY(int x,int y);
void Bend();
void Boarder();
void Down();
void Left();
void Up();
void Right();
void ExitGame();
int Scoreonly();
struct coordinate{
int x;
int y;
int direction;
};
typedef struct coordinate coordinate;
coordinate head, bend[500],food,body[30];
int main()
{
char key;
Print();
system("cls");
load();
length=5;
head.x=25;
head.y=20;
head.direction=RIGHT;
Boarder();
Food(); //to generate food coordinates initially
life=3; //number of extra lives
bend[0]=head;
Move(); //initialing initial bend coordinate
return 0;
}
void Move()
{
int a,i;
do{
Food();
fflush(stdin);
len=0;
for(i=0;i clock());
}
void load(){
int row,col,r,c,q;
gotoxy(36,14);
printf("loading...");
gotoxy(30,15);
for(r=1;r=70||head.y=30||check!=0)
{
life--;
if(life>=0)
{
head.x=25;
head.y=20;
bend_no=0;
head.direction=RIGHT;
Move();
}
else
{
system("cls");
printf("All lives completed\nBetter Luck Next Time!!!\nPress any key to quit the game\n");
record();
Solution
Things that could be improved:
Portability:
-
Every time you add an
You should always try to create a program so that it is as portable as possible, and can be played on a variety of systems. Right now, your game can only be played on a few select systems.
Conventions/Standards:
-
You don't follow proper C naming conventions for method names.
Either use camelCase, or snake_case with method names.
-
You should have unique method names.
Be more expressive with your function naming.
-
You don't
You can combine these two together for the proper definition of a
-
Don't use a
There are many problems with busy waiting instead of using
-
If you don't take in any variables as parameters, you should declare them as
-
Define
Styling:
-
You have way too much space in some areas of your program.
I'm all for using whitespace, but there are limits to everything. Cut back on it a bit, right now the amount of whitespace you are using makes this program unreadable.
Syntax:
-
You have some
These are all related to each other because they are all directions. Therefore, we can group them together in an
-
Use
Portability:
-
Every time you add an
#import to the top of your C file, you potentially create a dependency. For example: #include creates a dependency that the program can only be compiled on a Windows system.#include
#include
#include
#include
#include
#include
#include
#include
#include
#include You should always try to create a program so that it is as portable as possible, and can be played on a variety of systems. Right now, your game can only be played on a few select systems.
Conventions/Standards:
-
You don't follow proper C naming conventions for method names.
void Delay(long double);
void Move();
void Food();
int Score();
void Print();
void gotoxy(int x, int y);
void GotoXY(int x,int y);
void Bend();
void Boarder();
void Down();
void Left();
void Up();
void Right();
void ExitGame();
int Scoreonly();Either use camelCase, or snake_case with method names.
-
You should have unique method names.
void gotoxy(int x, int y);
void GotoXY(int x,int y);Be more expressive with your function naming.
-
You don't
typedef a struct in the standard way, nor do you use proper naming conventions of typedef structs.struct coordinate{
int x;
int y;
int direction;
};
typedef struct coordinate coordinate;You can combine these two together for the proper definition of a
typedef struct. Also, you should always capitalize the first letter of the typedef struct name.typedef struct
{
int x;
int y;
int direction;
} Coordinate;-
Don't use a
for loop in the place of sleep().for(i=0;i<=(10000000);i++);There are many problems with busy waiting instead of using
sleep(). See this question for more information.-
If you don't take in any variables as parameters, you should declare them as
void.int main(void)-
Define
i inside of your for loop.(C99)for(int i = 4; i < length; i++)Styling:
-
You have way too much space in some areas of your program.
int main()
{
char key;
Print();
system("cls");
load();
length=5;
head.x=25;
head.y=20;
head.direction=RIGHT;
Boarder();
Food(); //to generate food coordinates initially
life=3; //number of extra lives
bend[0]=head;
Move(); //initialing initial bend coordinate
return 0;
}I'm all for using whitespace, but there are limits to everything. Cut back on it a bit, right now the amount of whitespace you are using makes this program unreadable.
Syntax:
-
You have some
#defines that are related to each other.#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77These are all related to each other because they are all directions. Therefore, we can group them together in an
enum.typedef enum
{
UP = 72;
DOWN = 80;
LEFT = 75;
RIGHT = 77;
} Direction;-
Use
puts() instead of printf() when you are not formatting strings.Code Snippets
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <ctype.h>
#include <time.h>
#include <windows.h>
#include <process.h>
#include <unistd.h>void Delay(long double);
void Move();
void Food();
int Score();
void Print();
void gotoxy(int x, int y);
void GotoXY(int x,int y);
void Bend();
void Boarder();
void Down();
void Left();
void Up();
void Right();
void ExitGame();
int Scoreonly();void gotoxy(int x, int y);
void GotoXY(int x,int y);struct coordinate{
int x;
int y;
int direction;
};
typedef struct coordinate coordinate;typedef struct
{
int x;
int y;
int direction;
} Coordinate;Context
StackExchange Code Review Q#42602, answer score: 17
Revisions (0)
No revisions yet.