编译环境是vs2017
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define High 15 // 游戏画面尺寸
#define Width 35
#define EnemyNum 5 // 敌机个数
// 全局变量
int fond_x, fond_y; // 食物 :-2
int moveDirection; // 蛇身的移动2345
int position_x, position_y; // 飞机位置
int enemy_x[EnemyNum], enemy_y[EnemyNum]; // 敌机位置
int canvas[High][Width] = { 0 }; // 二维数组存储游戏画布中对应的元素
// 0为空格0,-1为边框#,-2为食物F,1为蛇头@,大于1的正数为蛇身*
int score; // 得分
int BulletWidth; // 子弹宽度
int EnemyMoveSpeed; // 敌机移动速度
void gotoxy(int x, int y) //光标移动到(x,y)位置
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void HideCursor() // 用于隐藏光标
{
CONSOLE_CURSOR_INFO cursor_info = { 1, 0 }; // 第二个值为0表示隐藏光标
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void startup() // 数据初始化
{
HideCursor(); // 隐藏光标
position_x = High - 1;
position_y = Width / 2;
canvas[position_x][position_y] = 1;
int i, j;
for (i = 0; i < High; i++)
{
canvas[i][0] = -1; //第一列
canvas[i][Width-1] = -1; //最后一列
}
for (j = 0; j < Width; j++)
{
canvas[0][j] = -1; //第一行
canvas[High - 1][j] = -1;//最后一行
}
//初始化蛇头和蛇身
canvas[High/2][Width/2] = 1;//蛇头
for (i = 1; i < 8; i++)
{
canvas[High /2 ][Width / 2-i] = i+1;//蛇身
}
score = 0;
BulletWidth = 0;
EnemyMoveSpeed = 20;
moveDirection = 3; //向右
//食物随机位置
fond_x = rand() % (High - 5);
fond_y = rand() % (Width - 5);
canvas[fond_x][fond_y] = -2; //画布中 -2 为食物
}
void moveSnakeByDirection()
{
int i, j;
int max = 0;
//记录蛇尾的坐标
int last_snake_i, last_snake_j;
//记录蛇头
int first_snake_i, first_snake_j;
//新蛇头位置
int new_head_i, new_head_j;
//蛇移动
for (i = 0; i < High; i++)
{
for (j = 0; j < Width; j++)
{
if (canvas[i][j] > 0)
{
//首先对所有大于1的值进行+1
canvas[i][j]++;
//求出最大值:遍历所有
if (max < canvas[i][j])
{
max = canvas[i][j];
last_snake_i = i;
last_snake_j = j;
}
//记录旧蛇头的位置
if (canvas[i][j] == 2)
{
first_snake_i = i;
first_snake_j = j;
}
}
}
}
if (moveDirection == 1)
{
//canvas[first_snake_i - 1][first_snake_j] = 1; // 蛇头向上就是,i-1,表示上一个位置减1,并置为1就可以。(*^▽^*),
new_head_i = first_snake_i-1;
new_head_j = first_snake_j;
}
if (moveDirection == 2)
{
//canvas[first_snake_i + 1][first_snake_j] = 1; // 蛇头向xia就是,i+1。
new_head_i = first_snake_i + 1;
new_head_j = first_snake_j;
}
if (moveDirection == 3)
{
//canvas[first_snake_i][first_snake_j + 1] = 1; // 蛇头向右就是
new_head_i = first_snake_i;
new_head_j = first_snake_j+1;
}
if (moveDirection == 4)
{
//canvas[first_snake_i][first_snake_j - 1] = 1; // 蛇头向左就是
new_head_i = first_snake_i;
new_head_j = first_snake_j-1;
}
//判断小蛇蛇头是否吃到食物
if (canvas[new_head_i][new_head_j] == -2)
{
//食物随机位置, 吃到食物,重新刷新
fond_x = rand() % (High - 1);
fond_y = rand() % (Width - 1);
canvas[fond_x][fond_y] = -2; //画布中 -2 为食物
//吃到食物,不用截取蛇尾
}
else
{
//没有吃到食物,截取尾巴,长度不变
canvas[last_snake_i][last_snake_j] = 0;
}
// 判断小蛇蛇头撞到了边框会死亡和撞到了自己的身体,游戏失败
if (canvas[new_head_i][new_head_j] > 1 || canvas[new_head_i][new_head_j] == -1)
{
printf(" the game gone! \n");
}
else
{
canvas[last_snake_i][last_snake_j] = 0; //尾巴置为空
canvas[new_head_i][new_head_j] = 1;
}
}
void show() // 显示画面
{
gotoxy(0, 0); // 光标移动到原点位置,以下重画清屏
int i, j;
for (i = 0; i<High; i++)
{
for (j = 0; j<Width; j++)
{
if (canvas[i][j] == 0)
printf(" "); // 输出空格
else if (canvas[i][j] == -1)
printf("#"); // 输出边框#
else if (canvas[i][j] == 1)
printf("@"); // 输出蛇头
else if (canvas[i][j] >1)
printf("*"); // 输出蛇身
else if (canvas[i][j] ==-2)
printf("0"); // 输出食物
}
printf("\n");
}
printf("得分:%d\n", score);
Sleep(100);
}
void updateWithoutInput() // 与用户输入无关的更新
{
moveSnakeByDirection();
}
void updateWithInput() // 与用户输入有关的更新
{
char input;
if (_kbhit()) // 判断是否有输入
{
input = _getch(); // 根据用户的不同输入来移动,不必输入回车
if (input == 'a')
{
moveDirection = 3; // 位置左移
moveSnakeByDirection();
}
else if (input == 'd')
{
moveDirection = 4; // 位置右移
moveSnakeByDirection();
}
else if (input == 'w')
{
moveDirection = 1; // 位置上移
moveSnakeByDirection();
}
else if (input == 's')
{
moveDirection = 2; // 位置下移
moveSnakeByDirection();
}
}
}
int main()
{
startup(); // 数据初始化
while (1) // 游戏循环执行
{
show(); // 显示画面
updateWithoutInput(); // 与用户输入无关的更新
updateWithInput(); // 与用户输入有关的更新
}
return 0;
}
代码中写明了详细的注释,贴一张效果图给你们看看
上述代码中,还没有添加吃食物得分,计分.可以自己自行添加效果.