### 代码如下：

//"贪吃蛇游戏"V1.0
//李国良于2016年12月29日编写完成

#include <iostream>
#include <string>
#include <ctime>
#include <windows.h>
#include <conio.h>

using namespace std;

int food[2] = { 9, 9 };//初始食物坐标
int snake[1000][2];//蛇身坐标
int length = 1;//初始蛇长
int speed = 500;//游戏难度
int score = 0;//分数
int level = 1;//难度等级
string name;//玩家姓名

void gotoxy(short x, short y);//移动光标
int setdirection(int x);//确定方向变量
void changesnake(int x);//改变蛇身坐标
void ifchangefood();//判断蛇是否吃到食物
void makefood();//创造新食物
bool judgelife();//判断蛇是否存活
void drawsnake();//画蛇
void drawfood();//画食物
void drawwall();//画墙
void drawscore();//画数据
void draw();//绘图

int main()
{
SetConsoleTitle("贪吃蛇游戏");
int po = 2;//初始方向变量
snake[0][0] = 7;
snake[0][1] = 7;//初始蛇头坐标
gotoxy(30, 7);
cout << "欢迎来到贪吃蛇游戏";
gotoxy(30, 9);
cout << "作者：李国良 版本1.0";
gotoxy(30, 11);
cout << "请输入你的姓名：";
cin >> name;
system("cls");
gotoxy(30, 7);
cout << "游戏控制方式:";
gotoxy(30, 9);
cout << "W键:向上 S键:向下";
gotoxy(30, 11);
cout << "A键:向左 D键:向右";
gotoxy(30, 13);
cout << "空格键:暂停";
gotoxy(30, 15);
cout << "将游戏窗口最大化之后";
gotoxy(30, 17);
cout << "按回车键开始游戏...";
cin.get();
cin.get();
system("cls");
while (true)
{
po = setdirection(po);
system("cls");
changesnake(po);
ifchangefood();
if (!judgelife())
break;
draw();
Sleep(speed);
}
gotoxy(30, 10);
cout << "Game Over!!!";
Sleep(2000);
gotoxy(28, 12);
system("pause");
return 0;
}

void gotoxy(short x, short y)
{
COORD position = { x, y };
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, position);
}

int setdirection(int x)
{
char ch;
if (_kbhit())
{
ch = _getch();
switch (ch)
{
case 'w':
x = 1;
break;
case 's':
x = 2;
break;
case 'a':
x = 3;
break;
case 'd':
x = 4;
break;
case ' ':
gotoxy(37, 16);
cout << "游  戏  暂  停. . .";
gotoxy(37, 18);
system("pause");
break;
default:
break;
}
}
return x;
}

void changesnake(int x)
{
switch (x)
{
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
default:
break;
}
for (int i = length; i > 0; --i)
{
for (int j = 0; j < 2; ++j)
{
snake[i][j] = snake[i - 1][j];
}
}
}

void ifchangefood()
{
if (snake[0][0] == food[0] && snake[0][1] == food[1])
{
length++;
makefood();
++score;
if (length > 5)
{
speed = 450;
level = 2;
}
if (length > 10)
{
speed = 400;
level = 3;
}
if (length > 15)
{
speed = 350;
level = 4;
}
if (length > 20)
{
speed = 300;
level = 5;
}
if (length > 25)
{
speed = 250;
level = 6;
}
if (length > 30)
{
speed = 200;
level = 7;
}
if (length > 35)
{
speed = 150;
level = 8;
}
if (length > 40)
{
speed = 100;
level = 9;
}
if (length > 45)
{
speed = 50;
level = 10;
}
}
}

void makefood()
{
srand((unsigned)time(NULL));
food[0] = rand() % 30 + 2;
food[1] = rand() % 30 + 4;
for (int m = 0; m < length; ++m)
{
if (food[0] == snake[m][0] && food[1] == snake[m][1])
{
makefood();
break;
}
}
}

bool judgelife()
{
for (int x = 1; x < length; ++x)
{
{
return false;
}

}
return false;
else
return true;
}

void drawsnake()
{
gotoxy(snake[0][0], snake[0][1]);
cout << "@";
for (int n = 1; n < length; ++n)
{
gotoxy(snake[n][0], snake[n][1]);
cout << "#";
}
}

void drawfood()
{
gotoxy(food[0], food[1]);
cout << "\$";
}

void drawwall()
{
gotoxy(0, 0);
cout << "------------------------------------";
gotoxy(16, 1);
cout << "贪吃蛇";
gotoxy(0, 2);
cout << "++++++++++++++++++++++++++++++++++++";
gotoxy(0, 35);
cout << "------------------------------------";
for (int x = 0; x < 35; ++x)
{
gotoxy(0, x);
cout << "|";
gotoxy(35, x);
cout << "|";
}
}

void drawscore()
{
gotoxy(37, 10);
cout << "分数：" << score;
gotoxy(37, 12);
cout << "等级：" << level;
gotoxy(37, 14);
cout << "玩家姓名：" << name;
}

void draw()
{
drawsnake();
drawfood();
drawwall();
drawscore();
}

#### 用c++编写的贪吃蛇小游戏

2009年08月31日 1.84MB 下载

#### 用C/C++语言实现贪吃蛇游戏

2014-10-06 23:04:36

#### c++经典项目控制台贪吃蛇小游戏详细教程

2017-02-12 21:05:19

#### C++实现的贪吃蛇游戏

2006-07-21 16:24:00

#### 用C/C++实现较完整贪吃蛇游戏

2017-08-22 13:35:53

#### C++<em>游戏</em>编程之<em>贪吃蛇</em>(新手向,大量注释)

2018年05月03日 00:00

#### C++题目练习，贪吃蛇制作学习

2016-12-21 12:00:22

#### 贪吃蛇——C++经典小游戏

2016-09-22 23:31:59

#### <em>贪吃蛇</em>源代码

2018年05月09日 00:00

#### leetcode题解

2016-09-04 13:11:02