# 纯C语言实现经典小游戏——贪吃蛇（VS2017）

#define M 20//整个图形界面的长和宽
#define N 60
struct snake a[(M - 2)*(N - 2)];//蛇身数组
int snake_x =4;//蛇身的XY坐标
int snake_y =4;
int X = 1;//控制蛇头的方向量
int Y = 0;
int food_x , food_y ;//食物的XY坐标
int score = 0;//分数

void food(int *x, int *y,int *fx,int *fy,int *s, struct snake *snake)
{
int ffx, ffy;//上一次食物的XY坐标
ffx = *fx;
ffy = *fy;

if (*x == *fx && *y == *fy)//如果吃到了食物，产生下一个食物
{
do {
*fx = 1 + rand() % (N - 3);
*fy = 1 + rand() % (M - 3);
} while (ffx == *fx && ffy == *fy);//保证与上次食物位置不同

for (int i= (*s); i >= 0; i--)
{
if ((snake + *s)->snake_x == *fx && (snake + *s)->snake_y == *fy) {
*fx = 1 + rand() % (N - 3);
*fy = 1 + rand() % (M - 3);
}
}//大概率保证食物与蛇身子的位置不同（不能完全保证）
(*s)++ ;                          //分数加一!!!!!!!
}
}

void control(int *x,int *y,int *X,int *Y)//xy是蛇头的坐标，XY是控制运动方向的量
{
if (_kbhit())
{
switch (_getch())
{
case 'w' :
case 'W' :
if(interf[*y - 1][*x]!='@'){   //if语句保证蛇不能倒着走
*X = 0; *Y = -1;
}
break;
case 'd':
case 'D':
if(interf[*y][*x + 1] != '@'){
*X = 1; *Y = 0;
}
break;
case 's':
case 'S':
if (interf[*y + 1][*x] != '@') {
*X = 0; *Y = 1;
}
break;
case 'a':
case 'A':
if (interf[*y][*x - 1] != '@') {
*X = -1; *Y = 0;
}
break;
default:
break;
}
}
*x = *x + *X;//改变一次位置
*y = *y + *Y;
}

struct snake
{
int number;
int snake_x;
int snake_y;
//struct snake *next;
};

struct snake a[(M - 2)*(N - 2)];

void build_snake(struct snake *snake,int s,int *sx,int *sy)//sx,sy蛇头的位置
{
for (int i = s; i >= 0; i--)
{
if (i == 0) {
(snake + i)->number = i;
(snake + i)->snake_x = *sx;
(snake + i)->snake_y = *sy;
}else{
(snake + i)->number = i;
(snake + i)->snake_x = (snake + i - 1)->snake_x;
(snake + i)->snake_y = (snake + i - 1)->snake_y;
}
}
}


char interf[M][N];

void newinterface(struct snake *snake,  int fx, int fy,int s)
{
int x, y;
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
if (i == 0 || i == M - 1)
interf[i][j] = '-';
//printf("-");
else if (j == 0)
interf[i][j] = '|';
//printf("|");
else if (j == N - 1)
interf[i][j] = '|';
//printf("|\n");
else if (i == fy && j == fx)
interf[i][j] = '$'; //printf("$");
else
interf[i][j] = ' ';
//printf(" ");
}
}
for (; s >= 0; s--)
{
x = (snake + s)->snake_x;
y = (snake + s)->snake_y;
interf[y][x] = '@';
}
}


文件 snakemain.cpp

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "interface.h"
#include "snake.h"

int main(void)
{
struct snake a[(M - 2)*(N - 2)];
int snake_x =4;
int snake_y =4;
int X = 1;
int Y = 0;
int food_x , food_y ;
int score = 0;

do {
food_x = 1 + rand() % (N - 3);
food_y = 1 + rand() % (M - 3);
} while (food_x == 4 && food_y == 4);

for (;;)
{
system("cls");
printf("\n                       贪吃蛇小游戏");
printf("\n作者：xhyang 博客地址：http://blog.csdn.net/weixin_39449570\n");
printf("按W控制向上运动，按D控制向右运动，按S控制向下运动，按A控制向左运动。\n");
printf("得分：%d",score);
printf("\n");

control(&snake_x, &snake_y,&X,&Y);
build_snake(a, score, &snake_x, &snake_y);
death(snake_x, snake_y,score);
newinterface(a, food_x, food_y, score);
food(&snake_x, &snake_y, &food_x, &food_y,&score,a);
draw();
Sleep(140);
}
system("pause");
return 0;
}
文件 snake.h

#ifndef SNAKE_H
#define SNAKE_H

struct snake
{
int number;
int snake_x;
int snake_y;
//struct snake *next;
};

void build_snake(struct snake *snake, int s, int *sx, int *sy);

#endif

文件 snake.cpp

#include <stdio.h>
#include <stdlib.h>
#include "snake.h"

void build_snake(struct snake *snake,int s,int *sx,int *sy)
{
for (int i = s; i >= 0; i--)
{
if (i == 0) {
(snake + i)->number = i;
(snake + i)->snake_x = *sx;
(snake + i)->snake_y = *sy;
}else{
(snake + i)->number = i;
(snake + i)->snake_x = (snake + i - 1)->snake_x;
(snake + i)->snake_y = (snake + i - 1)->snake_y;
}
}
}
文件 interface.h

#ifndef INTERFACE_H
#define INTERFACE_H

#define M 20
#define N 60

void control(int *x, int *y, int *X, int *Y);
void snake(int x, int y);
void newinterface(struct snake *snake, int fx, int fy, int s);
void food(int *x, int *y, int *fx, int *fy, int *s, struct snake *snake);
void draw(void);
void death(int x, int y, int s);

#endif
文件 interface.cpp
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include "interface.h"
#include "snake.h"

char interf[M][N];

void newinterface(struct snake *snake,  int fx, int fy,int s)
{
int x, y;
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
if (i == 0 || i == M - 1)
interf[i][j] = '-';
//printf("-");
else if (j == 0)
interf[i][j] = '|';
//printf("|");
else if (j == N - 1)
interf[i][j] = '|';
//printf("|\n");
else if (i == fy && j == fx)
interf[i][j] = '$'; //printf("$");
else
interf[i][j] = ' ';
//printf(" ");
}
}
for (; s >= 0; s--)
{
x = (snake + s)->snake_x;
y = (snake + s)->snake_y;
interf[y][x] = '@';
}
}

void food(int *x, int *y,int *fx,int *fy,int *s, struct snake *snake)
{
int ffx, ffy;//上一次食物的XY坐标
ffx = *fx;
ffy = *fy;

if (*x == *fx && *y == *fy)//如果吃到了食物，产生下一个食物
{
do {
*fx = 1 + rand() % (N - 3);
*fy = 1 + rand() % (M - 3);
} while (ffx == *fx && ffy == *fy);//保证与上次食物位置不同

for (int i= (*s); i >= 0; i--)
{
if ((snake + *s)->snake_x == *fx && (snake + *s)->snake_y == *fy) {
*fx = 1 + rand() % (N - 3);
*fy = 1 + rand() % (M - 3);
}
}//大概率保证食物与蛇身子的位置不同（不能完全保证）
(*s)++ ;                          //分数加一!!!!!!!
}
}

void control(int *x,int *y,int *X,int *Y)//xy是蛇头的坐标，XY是控制运动方向的量
{
if (_kbhit())
{
switch (_getch())
{
case 'w' :
case 'W' :
if(interf[*y - 1][*x]!='@'){   //if语句保证蛇不能倒着走
*X = 0; *Y = -1;
}
break;
case 'd':
case 'D':
if(interf[*y][*x + 1] != '@'){
*X = 1; *Y = 0;
}
break;
case 's':
case 'S':
if (interf[*y + 1][*x] != '@') {
*X = 0; *Y = 1;
}
break;
case 'a':
case 'A':
if (interf[*y][*x - 1] != '@') {
*X = -1; *Y = 0;
}
break;
default:
break;
}
}
*x = *x + *X;//改变一次位置
*y = *y + *Y;
}

void draw(void)
{
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
printf("%c", interf[i][j]);
}
printf("\n");
}
}

void death(int x,int y,int s)
{
if (x == 0 || x == N-1 || y  == 0 || y == M-1 || interf[y][x] == '@') {
for (;;)
{
system("cls");
printf("\n              游戏结束！\n");
printf("              最终得分：%d \n", s);
Sleep(140);
//system("pause");
}

}
}

• 104
点赞
• 501
收藏
觉得还不错? 一键收藏
• 15
评论
05-27

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。