main.c

  1 //纯C语言贪吃蛇
  2 #define _CRT_SECURE_NO_WARNINGS
  3 #include"snake.h"
  4 #include<stdio.h>
  5 int main()
  6 {
  7     //去除光标
  8     CONSOLE_CURSOR_INFO test1;
  9     test1.dwSize = sizeof(test1);
 10     test1.bVisible = false;
 11     SetConsoleCursorInfo((GetStdHandle(STD_OUTPUT_HANDLE)), &test1);
 12     
 13     
 14     initSnake();
 15     initFood();
 16     //initUI();
 17     DrawWall();
 18     playGame();
 19     system("cls");
 20     printf("您的得分为:%d\n", score);
 21     return 0;
 22 }
 23 
 24 //初始化蛇
 25 void initSnake(void)
 26 {
 27     SNAKE_1.size = 2;
 28     SNAKE_1.body[0].x = LENGTH / 2;    //🐍头从中间初始化
 29     SNAKE_1.body[0].y = WIDTH / 2;
 30     //假定初始化的🐍身只有一个,且蛇头朝左
 31     SNAKE_1.body[1].x = LENGTH / 2-1;
 32     SNAKE_1.body[1].y = WIDTH / 2;
 33 
 34 }
 35 
 36 //初始化食物
 37 void initFood(void)
 38 {
 39     srand(time(NULL));//每次都更改随机数的种子使得每次生成的随机数不同
 40     FOOD_1.x = rand() % (LENGTH);//注意出现墙壁的情况
 41     FOOD_1.y = rand() % (WIDTH);
 42 }
 43 void DrawWall()
 44 {
 45     for (int i = 0; i <= WIDTH; i++)
 46     {
 47         for (int j = 0; j <= LENGTH; j++)
 48         {
 49             if (i == WIDTH)
 50             {
 51                 printf("-");
 52             }
 53             else if(j== LENGTH)
 54             {
 55                 printf("|");
 56             }
 57             else
 58             {
 59                 printf(" ");
 60             }
 61 
 62         }
 63         printf("\n");
 64     }
 65 }
 66 
 67 void initUI()
 68 {
 69     //画蛇头
 70     coord.X = SNAKE_1.body[0].x;
 71     coord.Y = SNAKE_1.body[0].y;
 72     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);//采用函数来移动光标
 73     putchar('@');
 74 
 75     //画蛇身体
 76     for (int i = 1; i < SNAKE_1.size; i++)
 77     {
 78         coord.X = SNAKE_1.body[i].x;
 79         coord.Y = SNAKE_1.body[i].y;
 80         SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
 81         putchar('*');
 82     }
 83     //清除蛇尾
 84     coord.X = lastX;
 85     coord.Y = lastY;
 86     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
 87     printf(" ");
 88 
 89 
 90     //画食物
 91     coord.X = FOOD_1.x;
 92     coord.Y = FOOD_1.y;
 93     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
 94     printf("");
 95 
 96     //将光标移到不干扰游戏的位置
 97     coord.X = 0;
 98     coord.Y = WIDTH+1;
 99     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
100 }
101 void playGame()
102 {
103     char way = 'a';
104     while ((SNAKE_1.body[0].x <LENGTH && SNAKE_1.body[0].x>=0) && (SNAKE_1.body[0].y <WIDTH && SNAKE_1.body[0].y>=0))//首先判断蛇头是否撞墙
105     {
106         initUI();//更新蛇
107         if (_kbhit())//利用kbhit()函数来确保用户是否输入
108         {
109             way = _getch();
110         }
111         //蛇移动,前一节身体给后一节身体赋值
112         switch (way)
113         {
114         case 'a':
115             kx = -1;
116             ky = 0;
117             break;
118         case 's':
119             kx = 0;
120             ky = 1;
121             break;
122         case 'd':
123             kx = 1;
124             ky = 0;
125             break;
126         case 'w':
127             kx = 0;
128             ky = -1;
129             break;
130         }
131         //判断蛇头是否撞到任意一节身体
132         for (int i = 1; i < SNAKE_1.size; i++)
133         {
134             if (SNAKE_1.body[0].x == SNAKE_1.body[i].x && SNAKE_1.body[0].y == SNAKE_1.body[i].y)
135             {
136                 return;//游戏结束
137             }
138         }
139         //蛇头撞食物
140         if (SNAKE_1.body[0].x == FOOD_1.x && SNAKE_1.body[0].y == FOOD_1.y)
141         {
142             //食物消失
143             initFood();
144             //身体增长
145             SNAKE_1.size += 1;
146             //吃一个食物加10分
147             score += 10;
148         }
149         //存储蛇尾位置
150         lastX = SNAKE_1.body[SNAKE_1.size - 1].x;
151         lastY = SNAKE_1.body[SNAKE_1.size - 1].y;
152         //蛇移动,前一节给后一节赋值
153         for (int i = SNAKE_1.size - 1; i > 0; i--)
154         {
155             SNAKE_1.body[i].x = SNAKE_1.body[i - 1].x;
156             SNAKE_1.body[i].y = SNAKE_1.body[i - 1].y;
157         }
158         //移动蛇头a
159         SNAKE_1.body[0].x += kx;
160         SNAKE_1.body[0].y += ky;
161         Sleep(300);
162     }
163 }
View Code

 

 

snake.h

 1 #ifndef __SNAKE_H__
 2 #define __SNAKE_H__
 3 #include<stdlib.h>
 4 #include<time.h>
 5 #include<Windows.h>
 6 #include<conio.h>
 7 
 8 #define WIDTH 20
 9 #define LENGTH 40
10 struct BODY {
11     int x;//x坐标
12     int y;//y坐标
13 };
14 //定义整个蛇
15 struct snake {
16     struct BODY body[WIDTH * LENGTH];//蛇身最长的长度
17     int size;//当前🐍的长度
18 }SNAKE_1;
19 
20 //定义食物对象
21 struct food {
22     int x;
23     int y;
24 }FOOD_1;
25 //初始化光标
26 COORD coord;//引入光标COORD类来移动光标
27 
28 //定义对象和食物对象
29 int score = 0;
30 
31 //定义当用户输入asdw的时候坐标的变化
32 int kx, ky;
33 //记录蛇尾的位置
34 int lastX = SNAKE_1.body[SNAKE_1.size - 1].x;
35 int lastY = SNAKE_1.body[SNAKE_1.size - 1].y;
36 
37 //初始化蛇
38 void initSnake(void);
39 //初始化食物
40 void initFood(void);
41 //初始化界面
42 void initUI();
43 //进行游戏
44 void playGame();
45 //接收用户按键
46 void GetKey();
47 void DrawWall();
48 #endif
View Code