一,涉及知识点:
结构体链表,动态分配内存,键盘输入检测,设置光标。
二,实现逻辑
1,可以设置光标,就能实现制定位置打印制定符号。
2,涉及一个结构体,包含两个元素坐标元素和一个结构体指针。
3,结构体串联形成链表,遍历获取成员坐标,打印符号得到蛇身。
4,不断的加头,去尾,重新遍历坐标,再打印形成蛇的移动。
5,食物产生的位置判定,不能越界,也不能与蛇身体重合。
6,蛇的转向判定,一条规则,不允许倒退。
7,转向的实现,跟行进方向决定新的关节坐标(当前头的上下左右)
8,死亡检测,是否头节点坐标是否与墙壁重合,是否与身体其他关节重合。
9,加速减速,设置刷新休眠时间实现。
pragma comment(lib, “user32.lib”)
上面这句代码不加的话用VS的开发人员命令提示编译时不通过
具体这个东西是干嘛的,参考下面这位仁兄的博客:
//http://blog.csdn.net/jiary5201314/article/details/9011203
源码,拿走不谢
#pragma comment(lib, "user32.lib")
#include<stdio.h>
#include<time.h>
#include<windows.h>
#include<stdlib.h>
#define U 1
#define D 2
#define L 3
#define R 4
typedef struct Node
{
int x;
int y;
struct Node *next;
} Node;
Node *head,*food,*index;
void snake_create();
void wall_create();
void Pos(int, int);
void game_description();
void snake_move();
void game_control();
void start_game();
void pause_game();
void exit_game();
void isbitself();
void isbitwall();
void spawn_food();
int score = 0,
add = 10,
death_type,
direction = R,
sleeptime = 200;
void Pos(int x, int y)
{
COORD pos;
HANDLE hOutput;
pos.X = x;
pos.Y = y;
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOutput, pos);
}
void game_description()
{
Pos(40, 14);
printf("欢迎进入贪吃蛇游戏!\n\n");
system("pause");
system("color 5e");
system("cls");
Pos(10, 14);
printf("您将通过键盘控制蛇的移动: 上↑ 下↓ 左 ← 右→ 加速F1 减速F2 暂停space 退出Esc");
Pos(10, 15);
printf("吃到食物身体会变长\n");
Pos(10, 16);
printf("撞到身体或墙壁游戏都会结束\n");
system("pause");
system("cls");
wall_create();
snake_create();
spawn_food();
}
void wall_create()
{
int i;
for (i = 0; i<58; i += 2)
{
Pos(i, 0);
printf("▓");
Pos(i, 26);
printf("▓");
}
for (i = 1; i<26; i++)
{
Pos(0, i);
printf("▓");
Pos(56, i);
printf("▓");
}
Pos(70, 10);
printf("当前分数");
}
void snake_create()
{
Node *tail;
int i;
tail = (Node*)malloc(sizeof(Node));
tail->x = 24;
tail->y = 5;
tail->next = NULL;
for (i = 1; i <= 4; i++)
{
head = (Node*)malloc(sizeof(Node));
head->next = tail;
head->x = 24 + 2 * i;
head->y =5;
tail = head;
}
while (tail != NULL)
{
Pos(tail->x, tail->y);
printf("◆");
tail = tail->next;
}
}
void spawn_food()
{
if (food != NULL)
{
return;
}
Node* food_1 = (Node*)malloc(sizeof(Node));
food_1->x = rand() % 52 + 2;
food_1->y = rand() % 24 + 1;
while ((food_1->x) % 2 != 0)
{
food_1->x = rand() % 50 + 2;
}
index = head;
while (index != NULL)
{
if (food_1->x == index->x && food_1->y == index->y)
{
free(food_1);
spawn_food();
break;
}
index = index->next;
}
food = food_1;
Pos(food->x, food->y);
printf("●");
}
void snake_move()
{
isbitwall();
Node *nexthead = (Node*)malloc(sizeof(Node));
switch (direction)
{
case U:
nexthead->x = head->x;
nexthead->y = head->y - 1;
break;
case D:
nexthead->x = head->x;
nexthead->y = head->y + 1;
break;
case L:
nexthead->x = head->x - 2;
nexthead->y = head->y;
break;
case R:
nexthead->x = head->x + 2;
nexthead->y = head->y;
break;
}
nexthead->next = head;
head = nexthead;
index = head;
isbitself();
if (nexthead->x == food->x && nexthead->y == food->y)
{
while (index != NULL)
{
Pos(index->x,index->y);
printf("◆");
index = index->next;
}
score += add;
free(food);
food = NULL;
spawn_food();
}
else
{
while (index->next->next != NULL)
{
Pos(index->x, index->y);
printf("◆");
index = index->next;
}
Pos(index->next->x, index->next->y);
printf(" ");
free(index->next);
index->next = NULL;
}
}
void game_control()
{
Pos(64, 15);
printf("不能穿墙,不能咬到自己\n");
Pos(64, 16);
printf("用↑ ↓ ← → 控制蛇的移动.");
Pos(64, 17);
printf("F1 为加速,F2 为减速\n");
Pos(64, 18);
printf("ESC :退出游戏 space:暂停游戏.");
direction = R;
while (1)
{
Pos(64, 10);
printf("得分:%d ", score);
Pos(64, 11);
printf("当前食物分值:%d分", add);
if (GetAsyncKeyState(VK_UP) && direction != D)
{
direction = U;
}
else if (GetAsyncKeyState(VK_DOWN) && direction != U)
{
direction = D;
}
else if (GetAsyncKeyState(VK_RIGHT) && direction != L)
{
direction = R;
}
else if (GetAsyncKeyState(VK_LEFT) && direction != R)
{
direction = L;
}
else if (GetAsyncKeyState(VK_ESCAPE))
{
death_type = 3;
break;
}
else if (GetAsyncKeyState(VK_SPACE))
{
pause_game();
}
else if (GetAsyncKeyState(VK_F1))
{
if (sleeptime > 50)
{
sleeptime -= 30;
add = add + 2;
if (sleeptime == 320)
{
add = 2;
}
}
}
else if (GetAsyncKeyState(VK_F2))
{
if (sleeptime<350)
{
sleeptime = sleeptime + 30;
add = add - 2;
if (sleeptime == 350)
{
add = 1;
}
}
}
Sleep(sleeptime);
snake_move();
}
}
void pause_game()
{
while(1)
{
Sleep(300);
if (GetAsyncKeyState(VK_SPACE))
{
break;
}
}
}
void isbitwall()
{
if (head->x == 0 || head->x == 56 || head->y == 0 || head->y == 26)
{
death_type = 1;
exit_game();
}
}
void isbitself()
{
Node *self = head->next;
while (self != NULL)
{
if (self->x == head->x && self->y == head->y)
{
death_type = 2;
exit_game();
}
self = self->next;
}
}
void exit_game()
{
system("cls");
Pos(24, 12);
if (death_type == 1)
{
printf("对不起,您撞到墙了。游戏结束.");
}
else if (death_type == 2)
{
printf("对不起,您咬到自己了。游戏结束.");
}
else if (death_type == 3)
{
printf("您已经结束了游戏。");
}
Pos(24, 13);
printf("您的得分是%d\n", score);
getchar();
exit(0);
}
void main()
{
system("title ★★贪吃蛇★★");
system("mode con cols=100 lines=30");
game_description();
game_control();
system("pause");
}
运行效果截图:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)