C language implements snake game (command line)

  • 2020-09-28 09:04:52
  • OfStack

This is a pure C language written snake game, for your reference, the specific content is as follows


#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
#include<conio.h>

#define SNAKE_LENGTH 100// Defines the maximum length of a snake 
#define SCREEN_WIDETH 80
#define SCREEN_HEIGHT 30

// Define each 1 The coordinates of the knotted snake 
struct coor{
 int x;
 int y;
};

// Enumeration direction 
enum CH {
 right = VK_RIGHT,
 left = VK_LEFT,
 up = VK_UP,
 down = VK_DOWN
};

// Define the attributes of the snake 
struct snake{
 int len;// Current snake length 
 struct coor coord[SNAKE_LENGTH];// every 1 The coordinates of the knotted snake 
 enum CH CH;// Define the direction of the snake 
 int SPEED;
 int flag;// Define the snake's state   1 Said to survive   0 Said death 
}snake;

// Cursor movement function 
void gotoxy(int x, int y)
{
 COORD pos;
 pos.X = x;
 pos.Y = y;
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}

// Initializes the game interface 
void init_sence()
{
 // Initialize the upper and lower walls 
 for (int i = 0; i < SCREEN_WIDETH; i += 2)
 {
 gotoxy(i,0);
 printf(" s ");
 gotoxy(i, SCREEN_HEIGHT);
 printf(" s ");
 }
 // Initialize left and right walls 
 for (int i = 0; i <=SCREEN_HEIGHT; i++)
 {
 gotoxy(0, i);
 printf(" s ");
 gotoxy(SCREEN_WIDETH,i);
 printf(" s ");
 }
 // Print prompt message 
 gotoxy(SCREEN_WIDETH + 5, 2);
 printf("\t\t snake ");
 gotoxy(SCREEN_WIDETH + 5, 6);
 printf("2018//12//1");
 gotoxy(SCREEN_WIDETH + 5, 8);
 printf(" Author: Bean sprouts ");
 gotoxy(SCREEN_WIDETH + 5, 10);
 printf("F1: To speed up \tF2 : slow down ");
 gotoxy(SCREEN_WIDETH + 5, 12);
 printf("CTRL: Continue to \t Space: Pause ");
 gotoxy(SCREEN_WIDETH + 5, 14);
 printf("ESC: Quit the game ");
 gotoxy(SCREEN_WIDETH + 5, 28);
 printf(" Advice: QQ : 2862841130:::");
}

struct foodcoord {
 int x;
 int y;
 int flag;// Define the state of the food 
}food;


//** This is a c The program **


#include"snake.h"
// The movement of the snake 
void move_snake();
// Draw a snake 
void draw_snake();
// To produce food 
void creatfood();
// Determine if the snake has eaten the food 
void eatfood();
// Determine if the snake is dead 
void SnakeState();


int main()
{
 // Set window size 
 system("mode con cols=110 lines=31");
 // Set the title 
 SetConsoleTitleA(" snake ");


 // Initialize the snake 
begin:
 snake.CH = VK_RIGHT;// Initialization direction 
 snake.len = 5; // Initialization length 
 snake.SPEED = 300;// Initializes the snake's movement speed 
 snake.coord[1].x = SCREEN_WIDETH / 2;// Initializes the coordinates of the snakehead 
 snake.coord[1].y = SCREEN_HEIGHT / 2;
 snake.coord[2].x = SCREEN_WIDETH / 2-2;// Initializes the coordinates of the snakehead 
 snake.coord[2].y = SCREEN_HEIGHT / 2;
 snake.coord[3].x = SCREEN_WIDETH / 2-4;// Initializes the coordinates of the snakehead 
 snake.coord[3].y = SCREEN_HEIGHT / 2;

 // Initializes the food state 
 food.flag = 1;//1 It means to eat food   0 No food was eaten 

   // Initializes the food state 
 snake.flag = 1;//1 live  0 die 

 init_sence();// Initializes the game interface 
 while (1)
 {
 draw_snake();// Draw a snake 
 Sleep(snake.SPEED);// The speed of the snake 
 move_snake();// Moving snake 
 if(food.flag)
 creatfood();// To produce food 
 eatfood();// Determine if you have eaten the food 
 SnakeState();// Determine if the snake is dead 
 if (!snake.flag)break;
 }
 system("cls");
 gotoxy(SCREEN_WIDETH/2, SCREEN_HEIGHT/2-4);
 printf(" GAME OVER!!!");
 gotoxy(SCREEN_WIDETH / 2-6, SCREEN_HEIGHT / 2+2);
 printf(" Your score is: \t\t\t%d ",snake.len-1);
 gotoxy(SCREEN_WIDETH / 2-6, SCREEN_HEIGHT / 2+4);
 printf(" I won't come again: \t\t\tCTRL ");
 gotoxy(SCREEN_WIDETH / 2-6, SCREEN_HEIGHT / 2+6);
 printf(" Forget it. Junk games ruin my youth :\t\tESC");

 while (1)
 {
 if (GetAsyncKeyState(VK_CONTROL))
 {
  system("cls");
  goto begin;
 }
 else if (GetAsyncKeyState(VK_ESCAPE))
  return 0;
 }
}

// The movement of the snake 
void move_snake()
{
 // Determine if there are keystrokes 
 if (GetAsyncKeyState(up))
 {
 if(snake.CH!=down)snake.CH = up;
 }
 else if (GetAsyncKeyState(down))
 {
 if (snake.CH != up)snake.CH = down;
 }
 else if (GetAsyncKeyState(right))
 {
 if (snake.CH != left)snake.CH = right;
 }
 else if (GetAsyncKeyState(left))
 {
 if (snake.CH != right)snake.CH = left;
 }
 else if (GetAsyncKeyState(VK_F1))
 {
 if(snake.SPEED>=100)snake.SPEED -= 50;
 }
 else if (GetAsyncKeyState(VK_F2))
 {
 if (snake.SPEED <= 3000)snake.SPEED += 100;
 }
 // Change the position of the snake's head according to the direction it is detected 
 switch (snake.CH)
 {
 case right:snake.coord[1].x += 2; break;
 case left:snake.coord[1].x -= 2; break;
 case up:snake.coord[1].y -= 1; break;
 case down:snake.coord[1].y += 1; break;
 }
 
 
}

// Draw a snake 
void draw_snake()
{
 // Draw a snake 
 gotoxy(snake.coord[1].x, snake.coord[1].y);
 printf(" - ");

 // Draw the snake, straight 1 a for Cycle to achieve 
 for (int i = 2; i <= snake.len; i++)
 {
 gotoxy(snake.coord[i].x, snake.coord[i].y);
 printf(" - ");
 }
 // Wipe away the tail 
 gotoxy(snake.coord[snake.len].x, snake.coord[snake.len].y);
 printf(" ");

 // Traverse every 1 Section of the snake 
 for (int i = snake.len; i >1; i--)
 {
 snake.coord[i].x = snake.coord[i - 1].x;
 snake.coord[i].y = snake.coord[i - 1].y;
 }
 
 gotoxy(0, 0);
 printf(" s ");
 gotoxy(85, 25);
 printf(" score :%d ", snake.len-1);
 
}

// To produce food 
void creatfood()
{
 // Random seed generation 
 srand((unsigned)time(NULL));
 if(food.flag)
 while (1)
 {
 food.x = rand() % 80;
 food.y = rand() % 30;
 if (food.x % 2 == 0 && food.x >= 2 && food.x <= 78 && food.y > 1 && food.y < 30)
 {
  int flag = 0;
  // Determine if the food produced is likely to be on the snake's body 
  for (int i = 1; i <= snake.len; i++)
  {
  if (snake.coord[i].x == food.x&&snake.coord[i].y == food.y)
  {
   flag = 1;
   break;
  }
  }
  if (flag)continue;
  // Draw the food 
  else
  {
  gotoxy(food.x, food.y);
  printf(" Even though ");
  food.flag = 0;
  break;
  }
 }
 }
 food.flag = 0;
}

// Determine if the snake has eaten the food 
void eatfood()
{
 // You just have to determine if the head of the snake overlaps with the food 
 if (food.x == snake.coord[1].x&&food.y == snake.coord[1].y)
 {
  snake.len+=1;
  food.flag = 1;
 }
}

// Determine if the snake is dead 
void SnakeState()
{
 if (snake.coord[1].x < 2 || snake.coord[1].x>78 || snake.coord[1].y < 1 || snake.coord[1].y>29)
 snake.flag = 0;

 for (int i = 2; i <= snake.len; i++)
 {
 if (snake.coord[1].x == snake.coord[i].x&&snake.coord[1].y == snake.coord[i].y)
  snake.flag = 0;
 }
 
}

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: