C Language-Snake Game Project

C Language-Snake Game Project

Initialization of the snake

The initialization of the snake is actually the initialization of a two-dimensional array. The two-dimensional array stores two values, which contains the coordinate information of the snake's body. The initial position of his appearance is the middle position of the simulated coordinate.

The movement of the snake

The movement of the snake is achieved by changing the coordinates of the two-dimensional array. For example, when the snake advances one unit to the right, the coordinates of each body position of the tail are changed, and the direction of the tongue, the body and the tail of the snake are changed at the same time. In this way, the snake has moved forward by one unit overall.

Snake growth

When the snake eats normal food, the length of the snake will increase. Increasing the length of the snake means adding a two-dimensional array to the position of the food and turning this position into the head of the snake.

Death of snake

When the snake hits an obstacle, itself, or clears a level, the death of the snake is the destruction of the two-dimensional array.

Food production

The location of food is random. These factors are determined by random numbers obtained through random functions. The location of food cannot appear on obstacles and boundaries.

Control keyboard input

Change the direction of movement in the module by obtaining the W (up), S (down), A (left), and D (right) input by the keyboard, thereby affecting the movement direction of the snake.

Code example:

Snake.c

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<Windows.h>
#include<conio.h>
#include"Snake.h"
/*
1.定义蛇的结构体
2.初始化蛇和食物
3.开始游戏
        蛇和墙的碰撞
        蛇和自身碰撞
        蛇和食物碰撞
               重新随机食物
               蛇身体增长
               分数增长
        方向键控制
4.游戏结束
*/
void InitFood()
{
    
    
        food[0] = rand() % WIDE;
        food[1] = rand() % HIGH;
}
void InitSnake()
{
    
    
        snake.size = 2;
        snake.body[0].X = WIDE / 2;
        snake.body[0].Y = HIGH / 2;
        snake.body[1].X = WIDE / 2 - 1;
        snake.body[1].Y = HIGH / 2;
}
void ShowUI()
{
    
    
        
        COORD coord;
        coord.X = lx;
        coord.Y = ly;
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
        putchar(' ');
        //显示蛇的位置上
        for (int i = 0; i < snake.size; i++)
        {
    
    
               coord.X = snake.body[i].X;
               coord.Y = snake.body[i].Y;
               SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
               if (i == 0)
                       putchar('@');
               else
               {
    
    
                       putchar('*');
               }
               //显示食物位置
               coord.X = food[0];
               coord.Y = food[1];
               SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
               putchar('#');
        }
}
void PlayGame()
{
    
    
        char key = 'D';
        while (snake.body[0].X>=0 && snake.body[0].X<WIDE
               &&snake.body[0].Y>=0&& snake.body[0].Y<HIGH)
        {
    
    
               //显示界面
               ShowUI();
               //方向控制
               while (_kbhit())
               {
    
    
                       key = _getch();
               }
               //方向控制按钮
               switch (key)
               {
    
    
               case 'D':case'd':dx = 1; dy = 0; break;
               case 'A':case'a':dx = -1; dy = 0; break;
               case 'W':case'w':dx = 0; dy = -1; break;
               case 'S':case's':dx = 0; dy = 1; break;
               }
               //是否和自身碰撞
               for (int i = 1; i < snake.size; i++)
               {
    
    
                       //当条件满足就停止游戏
                       if (snake.body[0].X == snake.body[i].X&&snake.body[0].Y ==  snake.body[i].Y)
                       {
    
    
                              return;
                       }
               }
               //蛇和食物的碰撞
               if (snake.body[0].X == food[0] && snake.body[0].Y == food[1])
               {
    
    
                       //随机新食物
                       InitFood();
                       //蛇身体增长
                       snake.size++;
                       score += 10;
                       //关卡 速度  障碍物
               }
               //蛇更新坐标
               lx = snake.body[snake.size - 1].X;
               ly = snake.body[snake.size - 1].Y;
               for (int i = snake.size - 1; i > 0; i--)
               {
    
    
                       snake.body[i].X = snake.body[i - 1].X;
                       snake.body[i].Y = snake.body[i - 1].Y;
               }
               //更新舌头
               snake.body[0].X += dx;
               snake.body[0].Y += dy;
               
               Sleep(100);
               //system("cls");
        }
}
//加框架墙
void InitWall()
{
    
    
        for (int i = 0; i <= HIGH; i++)
        {
    
    
               for (int j = 0; j <= WIDE; j++)
               {
    
    
                       if (i == HIGH)
                              putchar('=');
                       else if(j==WIDE)
                              putchar('|');
                       else
                              putchar(' ');
               }
               putchar('\n');
        }
}
int main()
{
    
    
        //添加随机数种子
        srand((size_t)time(NULL));
        //清除控制台光标
        CONSOLE_CURSOR_INFO cci;
        cci.dwSize = sizeof(cci);
        cci.bVisible = FALSE;
        SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci);
        InitWall();
        InitFood();
        InitSnake();
        PlayGame();
        
        //getchar();
        return EXIT_SUCCESS;
}

Snake.h

#pragma once
#define WIDE 60
#define HIGH 20
struct BODY
{
    
    
        int X;
        int Y;
};
struct SNAKE
{
    
    
        int size;
        struct BODY body[WIDE * HIGH];
}snake;
int food[2] = {
    
     0 };//food[0]为X坐标 food[1]为Y坐标
int score = 0;
//偏移坐标
int dx = 0;
int dy = 0;
//记录蛇末尾坐标
int lx = 0;
int ly = 0;
void InitFood();
void InitSnake();
void ShowUI();
void PlayGame();
void InitWall();

Your attention is the motivation for my creation!

Guess you like

Origin blog.csdn.net/gyqailxj/article/details/114991058