#include<stdio.h>
#include<Windows.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#define H 20
#define W 60
typedef struct body {
int x; int y;
}BODY;
typedef struct snake {
int size;//蛇的长度
BODY list[W * H];//蛇的位置,最大可以铺满整个墙
BODY food;
COORD coord;
int dx;//x轴移动方向
int dy;//y轴移动方向
int score;//分数
BODY tail;
}SNAKE;
//展示界面
void show_wall()
{
for (int i = 0; i <= H; i++)
{
for (int j = 0; j <= W; j++)
{
if (i == H || j == W)printf("+");
else printf(" ");
}
puts("");
}
}
void init_food(BODY* food)
{
srand(time(NULL));
food->x = rand() % W;
food->y = rand() % H;
while (food->x == 0 || food->y == 0)
{
food->x = rand() % W;
food->y = rand() % H;
}
}
void init_snake(SNAKE* snake)
{
snake->size = 2;
snake->list[0].x = W / 2; snake->list[1].x = W / 2 - 1;
snake->list[0].y = H / 2; snake->list[1].y = H / 2;
snake->score = 0;
snake->dx = 1; snake->dy = 0;
init_food(&(snake->food));
}
void hide_cur()//隐藏光标,里面是windows特有的函数,不需要刻意去记,拿来就用
{
CONSOLE_CURSOR_INFO cci;
cci.dwSize = sizeof(cci);
cci.bVisible = FALSE;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci);
}
void show_ui(SNAKE* snake)
{
for (int i = 0; i < snake->size; i++)
{ //定位光标位置,windows自带函数
snake->coord.X = snake->list[i].x; snake->coord.Y = snake->list[i].y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), snake->coord);
if (i == 0)printf("@");
else printf("*");
}
snake->coord.X = snake->food.x; snake->coord.Y = snake->food.y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), snake->coord);
printf("#");
snake->coord.X = snake->tail.x; snake->coord.Y = snake->tail.y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), snake->coord);
printf(" ");
}
void move_snake(SNAKE* snake)
{
snake->tail.x = snake->list[snake->size - 1].x;
snake->tail.y = snake->list[snake->size - 1].y;
for (int i = snake->size - 1; i > 0; i--)
{
snake->list[i].x = snake->list[i - 1].x;
snake->list[i].y = snake->list[i - 1].y;
}
snake->list[0].x += snake->dx;
snake->list[0].y += snake->dy;
}
void control_move(SNAKE* snake)
{
char key = 0;
while (_kbhit())
{
key = _getch();
}
switch (key)
{
case 'd':
snake->dx = 1;
snake->dy = 0;
break;
case 's':
snake->dx = 0;
snake->dy = 1;
break;
case 'a':
snake->dx = -1;
snake->dy = 0;
break;
case 'w':
snake->dx = 0;
snake->dy = -1;
break;
}
}
void eat_food(SNAKE* snake)
{
if (snake->food.x == snake->list[0].x && snake->food.y == snake->list[0].y)
{
snake->size++;
snake->score += 10;
init_food(&snake->food);
}
}
void game_over(SNAKE* snake)
{
snake->coord.X = 30; snake->coord.Y = 25;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), snake->coord);
printf("游戏结束!!! 得分:%d", snake->score);
system("pause");
}
void eat_snake_body(SNAKE* snake)
{
for (int i = 1; i < snake->size; i++)
{
if (snake->list[0].x == snake->list[i].x && snake->list[0].y == snake->list[i].y)
{
game_over(snake);
exit(0);
}
}
}
void start_game(SNAKE* snake)
{
while (snake->list[0].x >= 0 && snake->list[0].x <= W - 1 && snake->list[0].y >= 0 && snake->list[0].y <= H - 1)
{
show_ui(snake);
control_move(snake);
eat_food(snake);
move_snake(snake);
eat_snake_body(snake);
Sleep(300);
}
game_over(snake);
}
int main()
{
hide_cur();
SNAKE* snake = (SNAKE*)malloc(sizeof(SNAKE));
init_snake(snake);
show_wall();
show_ui(snake);
start_game(snake);
free(snake);
system("pause");
return 0;
}