Snake game in C (Single player)

  • 2020-10-23 21:05:21
  • 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

Compared to # 1: Snake game, it has more features


#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 ");
 
 // play bgm
 playmusic();
 
 // Initialize the snake 
begin:
 snake.CH = VK_RIGHT;// Initialization direction 
 snake.len = 5; // Initialization length 
 snake.SPEED = 100;// 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)
 {
  colormap();
  gotoxy(0, 0);
  printf(" s ");
  HuiFu();
  setcolor();
 
  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>=50)snake.SPEED -= 10;
 }
 else if (GetAsyncKeyState(VK_F2))
 {
  if (snake.SPEED <= 100)snake.SPEED += 10;
 }
 // Game difficulty setting 
 if (snake.len >= 30 && snake.SPEED >= 10)snake.SPEED -= 2;
 // 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 
 HuiFu();
 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);
 gotoxy(85, 22);
 printf(" speed :%dms/ step  ", snake.SPEED);
 
}
 
// 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
   {
    colorfood();
    gotoxy(food.x, food.y);
    printf(" Even though ");
    HuiFu();
    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: