#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
#include<conio.h>
#define map_height 27
#define map_width 60
#define map_mode "+"//宏定义边框符号
#define food_mode "*"//食物符号
#define snake_mode "@"//蛇身
#define up 'w'//定义上下左右键
#define down 's'
#define right 'd'
#define left 'a'
int i, j, score, grow;
char ch = up, state = up;//ch储存按键,state储存上一次按键
struct FOOD
{
int x;
int y;
}food;
struct SNAKE
{
int x[100];
int y[100];
int len;//蛇长
}snake;
void map();//绘制地图
void create_food();创建食物
void move_snake();//蛇的移动
void gotoxy(int x, int y)//光标移动函数
{
HANDLE hout;
COORD cor;
/* typedef struct _COORD { SHORT X; // horizontal coordinate SHORT Y; // vertical coordinate } COORD; 用该结构体来储存坐标 */
hout = GetStdHandle(STD_OUTPUT_HANDLE);//从标准输出设备中取得一个句柄
/*这其中x,y的赋值对象要注意,不懂的好好想想*/
cor.X = y;
cor.Y = x;
SetConsoleCursorPosition(hout, cor);//定位光标的函数
}
int death();//检测蛇是否死亡
int main()
{
score = 0;
system("cls");//每次游戏开始前清除屏幕
map();
while (1)
{
create_food();
move_snake();
Sleep(200);
if (death())
{
break;
}
}
printf("Game Over!!\n\n");
printf("%d\n", score);
return 0;
}
void map()
{
//绘制边框
for (i = 0; i < map_height; i++)//绘制左右边框
{
gotoxy(i, 0);
printf(map_mode);
gotoxy(i, map_width);
printf(map_mode);
}
for (j = 0; j < map_width; j++)//绘制上下边框
{
gotoxy(0, j);
printf(map_mode);
gotoxy(map_height, j);
printf(map_mode);
}
//绘制食物
srand(time(NULL));//随机种子
food.x = rand() % (map_height - 2) + 1;//在边框范围内生成食物
food.y = rand() % (map_width - 2) + 1;
gotoxy(food.x, food.y);//
printf(food_mode);
//绘制蛇
snake.x[0] = map_height / 2;//初始化头
snake.y[0] = map_width / 2;
gotoxy(snake.x[0], snake.y[0]);
printf("?");
snake.len = 3;
for (i = 1; i < 3; i++)
{
snake.x[i] = snake.x[i - 1] + 1;
snake.y[i] = snake.y[i - 1];
gotoxy(snake.x[i], snake.y[i]);
printf(snake_mode);//初始化蛇身
}
}
void create_food()
{
grow = 0;
if (snake.x[0] == food.x && snake.y[0] == food.y)//吃到食物后
{
score += 10;
srand(time(NULL));
int flag = 1;//标记
do
{
food.x = rand() % (map_height - 2) + 1;
food.y = rand() % (map_width - 2) + 1;
for (i = 0; i < snake.len; i++)
{
if (snake.x[i] == food.x && snake.y[i] == food.y)//是否出现在蛇上
{
flag = 0;
break;
}
}
} while (flag == 0);
gotoxy(food.x, food.y);
printf(food_mode);//重新生成食物
snake.len++;//长度加一
grow = 1;//表示吃到食物
}
}
void move_snake()
{
while (_kbhit())//暂停等待按键
{
ch = _getch();//获取按键
}
if (!grow)//没吃到食物
{
gotoxy(snake.x[snake.len - 1], snake.y[snake.len - 1]);//清除蛇尾,为下一步铺垫
printf(" ");
}
for (i = snake.len - 1; i > 0; i--)//蛇身移动
{
snake.x[i] = snake.x[i - 1];
snake.y[i] = snake.y[i - 1];
gotoxy(snake.x[i], snake.y[i]);
printf(snake_mode);
}
switch (ch)//下面都是蛇头的移动
{
case up:
if (state == down)//原来是下,按上建则无效,下面同理
{
snake.x[0]++;
break;
}
else
{
snake.x[0]--;
state = up;
break;
}
case down:
if (state == up)
{
snake.x[0]--;
break;
}
else {
snake.x[0]++;
state = down;
break;
}
case right:
if (state == left)
{
snake.y[0]--;
break;
}
else
{
snake.y[0]++;
state = right;
break;
}
case left:
if (state == right)
{
snake.y[0]++;
break;
}
else {
snake.y[0]--;
state = left;
break;
}
default:
if (state == up)
{
snake.x[0]--;
break;
}
if (state == down)
{
snake.x[0]++;
break;
}
if (state == right)
{
snake.y[0]++;
break;
}
if (state == left)
{
snake.y[0]--;
break;
}
}
gotoxy(snake.x[0], snake.y[0]);
printf("?");
gotoxy(0, 0);
grow = 0;
}
int death()
{
if (snake.x[0] == 0 || snake.x[0] == map_height || snake.y[0] == 0 || snake.y[0] == map_width)
{
return 1;
}
else {
return 0;
}
}
简易版贪吃蛇游戏
这是一个使用C语言编写的蛇形游戏程序,包含了地图绘制、食物生成、蛇的移动以及边界检测等关键功能。程序通过键盘输入控制蛇的移动,当蛇吃到食物时会增长,碰到边界或自身时游戏结束。
摘要由CSDN通过智能技术生成