贪吃蛇
后续更新…
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
#define W 50 //墙的宽度
#define H 20 //墙的高度
char g_Buf[H][W];
int quit = 0;
typedef struct _snake
{
int number;
int snake_x;
int snake_y;
}SNAKE;
int g_stop = 0;
int snake_x_old = 0, snake_y_old = 0;
char PressRecord = 'P'; // 初始化,记录按键的变量,没什么特殊的意义,可以随便复制,但不能和按键重复
void control(int *snake_header_x,int *snake_header_y,int *snake_init_x,int *snake_init_y)
{
int x, y;
if (kbhit() != 0)
{
char ch = getch();
switch (ch)
{
case 'w':
case 'W': // UP
if (PressRecord == 'w' || PressRecord == 'W')
{
// 哈哈,抱歉啊,请不要在意。
}
else
if (g_Buf[*snake_header_y-1][*snake_header_x] != '@')
{
(*snake_init_x) = 0;
(*snake_init_y) -= 1;
PressRecord = ch;
}
break;
case 'a':
case 'A': // Left
if (PressRecord == 'a' || PressRecord == 'A')
{
// 哈哈,抱歉啊,请不要在意。
}
else
if (g_Buf[*snake_header_y][*snake_header_x-1] != '@')
{
(*snake_init_x) -= 1;
(*snake_init_y) = 0;
PressRecord = ch;
}
break;
case 'd':
case 'D': //Right
if (PressRecord == 'd' || PressRecord == 'D')
{
// 哈哈,抱歉啊,请不要在意。
}
else
if (g_Buf[*snake_header_y][*snake_header_x+1] != '@')
{
(*snake_init_x) += 1;
(*snake_init_y) = 0;
PressRecord = ch;
}
break;
case 's':
case 'S': //Down
if (PressRecord == 's' || PressRecord == 'S')
{
// 哈哈,抱歉啊,请不要在意。
}
else
if (g_Buf[*snake_header_y+1][*snake_header_x] != '@')
{
(*snake_init_x) = 0;
(*snake_init_y) += 1;
PressRecord = ch;
}
break;
case 32:
if (!g_stop)
{
g_stop = 1;
}
else
{
g_stop = 0;
}
default:
break;
}
}
if (!g_stop)
{
*snake_header_x = *snake_header_x + *snake_init_x;
*snake_header_y = *snake_header_y + *snake_init_y;
}
else
{
*snake_header_x = *snake_header_x + 0;
*snake_header_y = *snake_header_y + 0;
}
}
void CreateWallFoodSnake(SNAKE * snake,int food_x,int food_y,int score)
{
int w, h;
for (h = 0;h < H; h++)
{
for (w = 0; w < W; w++)
{
if ((h == 0) || (h == H - 1))
{
g_Buf[h][w] = '*';
}
else if (w == 0 || w == W - 1)
{
g_Buf[h][w] = '*';
}
else if (w == food_x && h == food_y)
{
g_Buf[h][w] = '#';
}
else
{
g_Buf[h][w] = ' ';
}
}
}
for (; score >= 0;score--)
{
h = (snake + score)->snake_y;
w = (snake + score)->snake_x;
// printf("h:%d,w:%d\n",h,w);
g_Buf[h][w] = '@';
}
}
void draw(void)
{
int w, h;
for (h = 0; h < H; h++)
{
for (w = 0; w < W; w++)
{
printf("%c",g_Buf[h][w]);
}
printf("\n");
}
}
void createsnake(SNAKE *snake,int score,int *snake_header_x,int *snake_header_y)
{
int i;
for (i = score; i >= 0; i--)
{
if (i == 0)
{
(snake+i)->number = i;
(snake+i)->snake_x = *snake_header_x;
(snake+i)->snake_y = *snake_header_y;
}
else
{
(snake + i)->number = i;
(snake + i)->snake_x = (snake + i - 1)->snake_x;
(snake + i)->snake_y = (snake + i - 1)->snake_y;
}
}
}
void foodcreate(SNAKE *snake,int *food_init_x,int *food_init_y,int *snake_header_x,int *snake_header_y,int *score)
{
int ffx, ffy,i;
ffx = *food_init_x;
ffy = *food_init_y;
if (*food_init_x == *snake_header_x && *snake_header_y == *food_init_y)
{
do
{
*food_init_x = rand() % (W - 3);
*food_init_y = rand() % (H - 3);
} while ((*food_init_x == ffx && *food_init_y == ffy) ||(*food_init_x == 0) || (*food_init_y == 0));
for (i = (*score);i >= 0; i--)
{
if ((snake + (*score))->snake_x == *food_init_x && (snake + (*score))->snake_y == *food_init_y)
{
*food_init_x = rand() % (W - 3);
*food_init_y = rand() % (H - 3);
}
}
(*score)++;
}
}
void death(int snake_init_dir_x,int snake_init_dir_y,int score)
{
if (snake_init_dir_x == 0 || snake_init_dir_x == W - 1 || snake_init_dir_y == 0 || snake_init_dir_y == H - 1
|| g_Buf[snake_init_dir_y][snake_init_dir_x] == '@')
{
system("cls");
printf(" \n");
printf(" \n");
printf(" \n");
printf(" 非常抱歉,游戏结束\n");
printf(" 记得下次不要撞墙或者吃到自己哦!!!\n");
printf(" 阁下的得分似: %d\n",score);
printf(" \n");
printf(" \n");
printf(" \n");
quit = 1;
}
}
int main(void)
{
SNAKE snake[(W-3)*(H-3)]; //保存蛇的身体
int snake_header_x = 6; // 游戏开始后蛇头的初始化位置
int snake_header_y = 6;
int snake_init_direction_x = 1; //游戏启动后,让蛇能够自己动起来,在x方向上
int snake_init_direction_y = 0;
int food_x, food_y;
int score = 0; //初始化得分
char level;
int value;
//初始化食物的位置
// srand(time(NULL)); // 初始化种子随机数
do
{
food_x = rand() % (W - 3);
food_y = rand() % (H - 3);
} while ((food_x == snake_header_x) && (food_y == snake_header_y));
printf(" \n");
printf(" \n");
printf(" \n");
printf(" 欢迎来到贪吃蛇游戏玩耍...... \n");
printf(" 希望你能在这里找到无限的乐趣和美好的回忆!!! \n");
printf(" 稍等片刻主角即将登场...... \n");
Sleep(150);
while (1)
{
system("cls");
printf(" \n");
printf(" 请仔细阅读一下游戏的操作规则 \n");
printf(" W键是向上移动、A键是向左移动、D键是向右移动、S键是向下移动...\n");
printf(" 空格键是暂停游戏 \n");
printf(" 记清楚了嘛!那就继续向下阅读吧! \n");
printf(" 请选择想要挑战的等级: \n");
printf(" 1:正常等级 2: 1倍速度等级 3: 3倍速度等级 \n");
printf(" 如果不想挑战这个有趣的游戏,那就请按 q 键退出吧!!! \n");
printf(" 请选择: ");
level = getchar();
getchar();
if (level == 'q')
{
system("cls");
printf(" \n");
printf(" \n");
printf(" \n");
printf(" 再见,咱们后会有期\n");
Sleep(900);
return 0;
}
if (level < '1' || level > '3')
{
system("cls");
printf(" \n");
printf(" \n");
printf(" 你选择的等级已经不再人类的承受范围,请重新选择!!!\n");
Sleep(900);
continue;
}
switch (level)
{
case '1':
value = 800;
break;
case '2':
value = 400;
break;
case '3':
value = 150;
break;
default:
value = 400;
break;
}
if (snake_init_direction_x != 0)
{
break;
}
}
while (1)
{
system("cls");
printf("我们将在这里记录你的成长...\n");
printf(" 得分: %d\n", score);
control(&snake_header_x, &snake_header_y, &snake_init_direction_x, &snake_init_direction_y);
createsnake(snake, score, &snake_header_x, &snake_header_y);
death(snake_header_x, snake_header_y, score);
if (quit)
{
break;
}
CreateWallFoodSnake(snake, food_x, food_y, score);
foodcreate(snake, &food_x, &food_y, &snake_header_x, &snake_header_y, &score);
if (g_stop)
{
while (1)
{
system("cls");
printf("我们将在这里记录你的成长...\n");
printf(" 得分: %d\n", score);
control(&snake_header_x, &snake_header_y, &snake_init_direction_x, &snake_init_direction_y);
createsnake(snake, score, &snake_header_x, &snake_header_y);
draw();
if (!g_stop)
{
break;
}
Sleep(250);
}
}
else
{
draw();
}
Sleep(value);
}
system("pause");
return 0;
}