C language development to achieve the snake game

  • 2020-10-31 21:55:27
  • OfStack

This article examples for you to share C language to achieve the snake game specific code, for your reference, the specific content is as follows

1. It's best to run with VS
2, the use of functions are :_kbhit _getch EasyX graph library 1 series of functions
3. The body of the snake and the food are drawn in a rectangle

The code is as follows:


#include<stdio.h>
#include<graphics.h>   //easyx The header file 
#include<conio.h>
#include<time.h>
/******************** Set various properties **********************/
// Coordinate attribute 
typedef struct point {
 int x, y;
}PYINT;
// The snake 
struct snake {
 PYINT xy[100];    // Each of the snake's coordinates is placed in an array 
 int position;    // The direction of 
 int num;       // The length of the 
}SNAKE;
// food 
struct food{
 PYINT fdxy;
 int flag;      // If there is a 
 int grade;      // results 
}FOOD;
// Enumeration: Directions 
// Also can use macros  #define
enum position
{
 up, down, left, right
};
/********************** The operation of the snake *****************************/
// Initialize the snake 
// Each stanza of the snake is  10x10 Filled rectangle 
void initsnake()
{
 SNAKE.xy[0].x = 0;
 SNAKE.xy[0].y = 0;
 // The first array 1 The element is the head of the snake 
 SNAKE.xy[1].x = 10;
 SNAKE.xy[1].y = 0;
 SNAKE.xy[2].x = 20;
 SNAKE.xy[2].y = 0;
 SNAKE.position = right; // The direction of   ( give )
 SNAKE.num = 3;      // The number of   (Help yourself) 
}
// Draw a snake 
void Drawsnake()
{
 for (int i = 0; i < SNAKE.num; i++)
 {
 setlinecolor(BLACK);
 setfillcolor(RGB(rand() % 255, rand() % 255, rand() % 255));
 fillrectangle(SNAKE.xy[i].x, SNAKE.xy[i].y, SNAKE.xy[i].x + 10, SNAKE.xy[i].y + 10);
 }
}
// Make the snake move 
/*
   Every time the head of the snake moves forward 1 Grid, followed by the head of the snake :
   We put each of the snake's coordinates in an array (the snake's head coordinates are at the top of the array) 1 A), when moving, simply make the snakehead (array of the first 1 Elements) change, then swap with the front ;
*/
void Movesnake()
{
 for (int i = SNAKE.num; i > 0; i--) {
 SNAKE.xy[i].x = SNAKE.xy[i - 1].x;
 SNAKE.xy[i].y = SNAKE.xy[i - 1].y;  // Move each element forward 
 }
 switch (SNAKE.position)
 {
 case up:          
 SNAKE.xy[0].y -= 10;
 break;
 case down:
 SNAKE.xy[0].y += 10;
 break;
 // Move up and down  X The coordinates don't change 
 case left:
 SNAKE.xy[0].x -= 10;
 break;
 case right:
 SNAKE.xy[0].x += 10;
 break;
  // Or so mobile  Y The coordinates don't change 
 }
}
/****************************** keystrokes *****************************************/
// While the snake is moving up, pressing the down key does not work (others 3 The same) 
void Keydown()
{
 char ch = _getch();
 switch (ch)
 {
 case 'W':
 case 'w':
 case 72:              // "Up" on the keypad 
 if (SNAKE.position != down)  // Whether down 
  SNAKE.position = up;
 break;
 case 'S':
 case 's':
 case 80:              // "Down" on the keypad 
 if (SNAKE.position != up)   // Whether the upward 
  SNAKE.position = down;
 break;
 case 'A':
 case 'a':
 case 75:             // "Left" on the keypad 
 if (SNAKE.position != right) // Whether or not to the right 
  SNAKE.position = left;
 break;
 case 'D':
 case 'd':
 case 77:              // "To the right" on the keypad 
 if (SNAKE.position != left)  // Whether or not the left 
  SNAKE.position = right;
 break;
 }
}
/****************************** Food operation **************************************/
// Initialize food 
void initfood()
{
 FOOD.fdxy.x = rand() % 60 * 10;        
 FOOD.fdxy.y = rand() % 40 * 10;           // Control the coordinates of the food in the game interface 
 FOOD.flag = 1; //1 Is for existing food 
 //FOOD.grade = 0;
 for (int i = 0; i < SNAKE.num; i++) {
 if (FOOD.fdxy.x == SNAKE.xy[i].x&&FOOD.fdxy.y == SNAKE.xy[i].y) // If the food appears on the snake, reproduce X Y The value of the 
 {
  FOOD.fdxy.x = rand() % 60 * 10;
  FOOD.fdxy.y = rand() % 40 * 10;
 }
 }
}
// Draw the food 
void Drawfood()
{
 setlinecolor(BLACK);
 setfillcolor(RGB(rand() % 255, rand() % 255, rand() % 255));         
 fillrectangle(FOOD.fdxy.x, FOOD.fdxy.y, FOOD.fdxy.x + 10, FOOD.fdxy.y + 10);
}
/************************************* Eat food *************************************************/
void eatfood()
{
 if (SNAKE.xy[0].x == FOOD.fdxy.x&&SNAKE.xy[0].y == FOOD.fdxy.y) {
 SNAKE.num++;
 FOOD.flag = 0;      // Markers of the presence of food ( 1: There are  0; There is no) 
 FOOD.grade += 10;    // Every eat 1 Plus the food score 10  (Every food is 10 Points, can be changed at will) 
 }
}
/************************************* According to score *************************************************/
void putgrade()
{
 char str[20] = " ";
 sprintf_s(str, "greade: %d", FOOD.grade);
 settextcolor(RED);             // Set the font color 
 outtextxy(500, 50, str);
}
/*********************************** Game over ***************************************************/
// When the snake's head hits the wall (the snake's head hits itself) 
// Only analysis 1 Kind of circumstance 
int gameover()
{
 if (SNAKE.xy[0].x > 600 || SNAKE.xy[0].x < 0 | SNAKE.xy[0].y>400 || SNAKE.xy[0].y < 0) // Against the wall 
 {
 initgraph(800, 600);
 settextcolor(RED);
 outtextxy(600, 400, " You hit the wall ");
 settextcolor(YELLOW);
 settextstyle(50, 50, " Regular script ");   // Font style 
 outtextxy(200, 250, "GAME OVER"); // Specifies the position of the output string 
 return 1;
 }
 return 0;
}
/*****************************************************************************************/
int main()
{
 initgraph(600, 400);
 setbkcolor(BLACK); // The background color 
 cleardevice();         // Clear the screen : The refresh screen 
 srand((unsigned int)time(NULL)); // Random number seed 
 initsnake();
 Drawsnake();
 while (1)
 {
 cleardevice(); // Clear the screen 
 Movesnake();
 Drawsnake();
 if (FOOD.flag == 0) {
  initfood();
 }
 Drawfood();
 if (_kbhit()) {   //_kbhit:  Detect if there is a keystroke 
  Keydown();
 }
 if (gameover()) {  // If the function returns 1: End of the game 
  break;
 }
 eatfood();
 putgrade();
 Sleep(100);  // Extension of the time  ( The speed at which the snake moves )
 }
 _getch();     // To prevent the splash screen 
 closegraph();   // Close the window 
 return 0;
}
/**********************************************************/
//RGB(rand() % 255, rand() % 255, rand() % 255)  in 3 Randomly generated within the primary color range 
// Remember to install EasyX The plug-in 

More interesting classic games to realize the special topic, to share with you:

C++ classic game summary

python classic game summary

python Tetris game collection

JavaScript classic games don't stop playing

java classic game summary

javascript classic game summary


Related articles: