C 语言实现贪吃蛇

//在这里我要吐槽,什么编码格式,复制上来之后格式没有了,全是第一行对齐,我要一个一个tab敲!!!!!


#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <stdlib.h>
#include <time.h>
#define LEN sizeof(struct Part)

typedef struct Part Sneak;
void bodymove(char direct);
void primarybody();
void bodyadd(int x, int y);
void bodyprint();
void bodydelete();
void gotoxy(int x, int y);
void gotoprint(int x, int y);
void creatfood();
void mapprint();
bool eat_food();
bool is_over();
int color(int c);
Sneak* head, * p1, * p2;
int score = 0;

//蛇身元素
struct Part
{
int x;
int y;
Sneak* next;
};

//食物
struct Food
{
int x;
int y;
}food;

int main()
{
char str;
int speed = 100;//速度
char name[100];//玩家名字
printf("请输入你想用来记录成绩的名称(不能超过6个字符):\n");
scanf_s("%s", name,6);
system("cls");//清屏函数,需要#include <windows.h>
color(10);//设置背景颜色
primarybody();//初始化身体
mapprint();//打印地图
bodyprint();//打印蛇身
creatfood();//创造食物

/*
_getch()是非阻塞IO输入,你不需要回车就可被读取,需要#include <conio.h>

*/
while (str = _getch())//当有输入的时候改变状态(暂停,改变方向),没有的话一直执行 # 处的循环,实现一直走
{
if (str == 'p')//暂停
{
system("pause");//暂停函数,需要#include<windows.h>
str = _getch();
}
while (1)//#
{
if (is_over())//是否结束(撞墙)
goto here;
if (eat_food())//判断是否头部坐标是否等于食物坐标
{
score++;
bodymove(str);
creatfood();
}
if (_kbhit())//检测键盘是否有输入,有为true
break;
bodymove(str);
bodyprint();//打印身体
bodydelete();//清除身体尾部
//通过不停的打印身体和清除身体,来实现移动的效果
gotoxy(0, 27);
printf("Your score:%d个%s", score, name);
Sleep(speed);//函数休眠函数,需要#include<windows.h>
speed = 10 * (10 - score / 10);//速度为成绩的函数,成绩越高,移动越快。
}
}
here:
system("cls");
system("pause");
gotoxy(35, 13);
printf("Game Over!\n");
return 0;
}

bool is_over()
{
p1 = head->next;
while (p1 != NULL)
{
if (p1->x == head->x && p1->y == head->y)
return 1;
p1 = p1->next;
}
if (head->x == 68 || head->x == 0 || head->y == 0 || head->y == 26)
return 1;
return 0;
}

void mapprint()//打印地图
{
int i;
for (i = 0; i < 70; i += 2)
{
gotoprint(i, 0);
gotoprint(i, 26);
}
for (i = 0; i <= 26; i++)
{
gotoprint(0, i);
gotoprint(70, i);
}
}

void creatfood()
{

srand(time(NULL));//设置随机函数种子
int x, y;
the:
x = int(rand()) % 66 + 2;
if (x % 2 != 0)
x++;
y = int(rand()) % 25 + 1;
p1 = head;
while (p1 != NULL)
{
if (x == p1->x && y == p1->y) //避免食物和头部重复
goto the;
p1 = p1->next;
}
food.x = x, food.y = y;
gotoxy(food.x, food.y);
printf("⊙");
}
bool eat_food()
{
if ((head->x == food.x) && head->y == food.y)
return 1;
return 0;
}

void bodymove(char direct)
{
switch (direct)
{
case 'w':bodyadd(head->x, head->y - 1); break;
case 's':bodyadd(head->x, head->y + 1); break;
case 'a':bodyadd(head->x - 2, head->y); break;
case 'd':bodyadd(head->x + 2, head->y); break;
default:break;
}
}

void bodyadd(int x, int y)
{
p1 = (Sneak*)malloc(LEN);
p1->x = x;
p1->y = y;
p1->next = head;
head = p1;
}
void bodydelete()
{
p1 = head;
while (p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}
gotoxy(p1->x, p1->y);
printf(" ");
free(p1);
p2->next = NULL;

}

void bodyprint()
{
Sneak* p;
p = head;
while (p != NULL)
{
gotoprint(p->x, p->y);
p = p->next;
}
//gotoprint(p->x,p->y);
}

void primarybody()
{
int n = 5;
p2 = p1 = (Sneak*)malloc(LEN);
p1->x = 26;
p2->y = n;
while (n-- > 1)
{
if (n == 4) head = p1;
else p2->next = p1;
p2 = p1;
p1 = (Sneak*)malloc(LEN);
p1->x = 26;
p1->y = n;
}
p2->next = NULL;
}

void gotoxy(int x, int y)//将光标定位到坐标x,y处
{
COORD pos;
HANDLE hOutput;
pos.X = x;
pos.Y = y;
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOutput, pos);
}

void gotoprint(int x, int y)
{
gotoxy(x, y);
printf("■");
}

int color(int c)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);
return 0;
}


猜你喜欢

转载自www.cnblogs.com/zerohua/p/12583336.html