贪吃蛇1.0
规则
? 初始蛇身长为2,向右移动,每吃到一次食物身长加1,积分加10
? wasd控制移动方向
? 碰到墙壁、障碍物或者吃到自身时死亡,游戏结束
代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <windows.h>
#include <conio.h>
#define WIDE 60
#define HIGH 20
typedef struct _body
{
int x;
int y;
} BODY;
typedef struct snake
{
int size;
BODY list[WIDE * HIGH];
BODY food;
COORD coord;
BODY tail;
int dx;
int dy;
} SNAKE;
void hide_cur();
void init_snake(SNAKE *snake);
void init_food(SNAKE *snake);
void init_wall();
void show_ui(SNAKE *snake);
void start_game(SNAKE *snake);
void move_snake(SNAKE *snake);
void snake_direction(SNAKE *snake);
void eat_food(SNAKE *snake);
int eat_self(SNAKE *snake);
void game_over(SNAKE *snake);
int main()
{
system("cls");
hide_cur();
SNAKE *snake = (SNAKE *)malloc(sizeof(SNAKE));
init_snake(snake);
start_game(snake);
return 0;
}
void hide_cur()
{
CONSOLE_CURSOR_INFO cci;
cci.dwSize = sizeof(cci);
cci.bVisible = FALSE;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci);
}
void init_ui()
{
for (int i = 0; i < HIGH; i++)
{
for (int j = 0; j < WIDE; j++)
{
printf("#");
}
printf("\n");
}
}
void init_snake(SNAKE *snake)
{
snake->list[0].x = WIDE / 2;
snake->list[0].y = HIGH / 2;
snake->list[1].x = WIDE / 2 - 1;
snake->list[1].y = HIGH / 2;
snake->size = 2;
init_food(snake);
snake->dx = 1;
snake->dy = 0;
}
void init_food(SNAKE *snake)
{
srand(time(NULL));
snake->food.x = rand() % (WIDE - 3) + 1;
snake->food.y = rand() % (HIGH - 3) + 1;
}
void init_wall()
{
for (int i = 0; i < HIGH; i++)
{
for (int j = 1; j < WIDE + 1; j++)
{
if (i == 0 || i == HIGH - 1 || j == 1 || j == WIDE)
printf("#");
else
printf(" ");
}
printf("\n");
}
}
void show_ui(SNAKE *snake)
{
for (int i = 0; i < snake->size; i++)
{
if (!snake->list[i].x && !snake->list[i].y)
return;
snake->coord.X = snake->list[i].x;
snake->coord.Y = snake->list[i].y + 1;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), snake->coord);
if (i == 0)
{
printf("@");
}
else
{
printf("*");
}
}
snake->coord.X = snake->tail.x;
snake->coord.Y = snake->tail.y + 1;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), snake->coord);
printf(" ");
snake->coord.X = snake->food.x;
snake->coord.Y = snake->food.y + 1;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), snake->coord);
printf("+");
}
void start_game(SNAKE *snake)
{
init_wall();
while (snake->list[0].x < WIDE - 1 && snake->list[0].x > 0 && snake->list[0].y < HIGH - 2 && snake->list[0].y > -1)
{
snake_direction(snake);
move_snake(snake);
show_ui(snake);
eat_food(snake);
if (eat_self(snake))
break;
Sleep(200);
}
game_over(snake);
}
void snake_direction(SNAKE *snake)
{
char key = 0;
while (_kbhit())
key = _getch();
switch (key)
{
case 'w':
snake->dx = 0;
snake->dy = -1;
break;
case 's':
snake->dx = 0;
snake->dy = 1;
break;
case 'a':
snake->dx = -1;
snake->dy = 0;
break;
case 'd':
snake->dx = 1;
snake->dy = 0;
break;
default:
break;
}
}
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] = snake->list[i - 1];
snake->list[0].x += snake->dx;
snake->list[0].y += snake->dy;
}
void game_over(SNAKE *snake)
{
system("chcp 65001");
system("cls");
COORD coord;
coord.X = WIDE / 2 - 10;
coord.Y = HIGH / 2;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
printf("游戏结束,得分:%d\n", 10 * (snake->size - 2));
coord.X = 0;
coord.Y = HIGH;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
Sleep(2000);
exit(0);
}
int eat_self(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)
return 1;
return 0;
}
void eat_food(SNAKE *snake)
{
if (snake->list[0].x == snake->food.x && snake->list[0].y == snake->food.y)
{
init_food(snake);
snake->size++;
}
}
运行方法
? 打开命令行窗口,执行
gcc -o greedy_snake.c greedy
? 生成greedy_snake.exe可执行文件
? 执行
./greedy_snake
待实现功能
关卡设置,积分达到一定值进入下一关
加入障碍物、巡逻者等
|