**贪吃蛇**
#include <iostream>
#include<Windows.h>
#include<conio.h>
#include<string>
#include<time.h>
using namespace std;
typedef struct a
{
int x; int y;
struct a *next;
}Snake;
int weizhix = 0, weizhiy = 0; //食物的位置坐标;
void Gotoab(short a, short b); //到达指定地方;
void Greatgraph(); //打印范围;
void GreatFood(); //随机产生食物;
int ClickControl(); //获取键盘信息;
int Judge(); //判断结束条件;
void Greatsnake(); //创造蛇;
void Movebody(); //蛇方向的移动;
void Changebody(int a,int b); //蛇身体的移动;
void Eating(); //判断是否吃到食物;
void Welcome(); //开始界面;
void End(); //结束界面;
Snake *head, *tail;
string name;
int score = 0;
char click=1;
int speed;
int main()
{
system("color 0A");
Welcome();
Greatgraph();
GreatFood();
if (ClickControl() == 0)return 0;
return 0;
}
void Gotoab(short a, short b)
{
COORD pos = { a, b }; //结构体;
HANDLE Output; //句柄,类似指向指针的东西;
Output = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(Output, pos);
}
void Greatgraph()
{
for (int i = 0; i < 26; i++)
{
Gotoab(0, i);
cout << "■";
Gotoab(62, i);
cout << "■";
}
for (int i = 2; i < 62; i += 2)
{
Gotoab(i, 0);
cout << "■";
Gotoab(i, 25);
cout << "■";
}
Gotoab(86, 5);
cout << "你当前分数:";
Gotoab(85, 7);
cout << "☆☆☆☆☆☆☆";
Gotoab(85, 13);
cout << "☆☆☆☆☆☆☆";
for (int i = 8; i < 13; i++)
{
Gotoab(85, i);
cout << "☆";
Gotoab(97, i);
cout << "☆";
}
Gotoab(91, 10);
cout << score;
Greatsnake();
}
void GreatFood()
{
srand((int)time(NULL));
flag:
weizhix = rand() % 30 + 1;
weizhix *= 2;
weizhiy = rand() % 24 + 1;
Snake *t;
t = head;
while (t->next!=NULL)
{
if (weizhix == t->x&&weizhiy == weizhiy)
goto flag;
t = t->next;
}
Gotoab(weizhix, weizhiy);
cout << "⊙";
}
int ClickControl()
{
while (1)
{
if (Judge() == 0) return 0;
if (_kbhit()) //判断是否有键盘输入;
{
click = _getch(); //只接收不显示;
}
Movebody();
Eating();
}
return 1;
}
int Judge()
{
if (head->x == 0 || head->x == 62 || head->y == 0 || head->y == 25)
{
End();
return 0;
}
Snake *t = head->next;
while (t->next!=NULL)
{
if (head->x == t->x&&head->y == t->y)
{
End();
return 0;
}
t= t->next;
}
return 1;
}
void Greatsnake()
{
Snake *p, *q;
p = new Snake;
q = new Snake;
head = new Snake;
tail = new Snake;
head->x = 16;
head->y = 16;
p->x = 16;
p->y = 17;
q->x = 16;
q->y = 18;
tail->x = 0; //只是连接在sanke链表后面,并不显示出来;
tail->y = 0;
tail->next = NULL;
head->next = p;
p->next = q;
q->next = tail;
}
void Movebody()
{
int number = 0;
int data1 = head->x, data2 = head->y;
Snake *t=head;
while (t->next!=NULL)
{
Gotoab(t->x, t->y);
cout << " ";
number++;
t = t->next;
}
switch (click)
{
case 'w':
head->y -= 1;
Changebody(data1, data2);
break;
case 'a':
head->x -= 2;
Changebody(data1, data2);
break;
case 'd':
head->x += 2;
Changebody(data1, data2);
break;
case 's':
head->y += 1;
Changebody(data1, data2);
break;
case'p':
_getch();
break;
}
t = head;
while (t->next!=NULL)
{
Gotoab(t->x, t->y);
cout << "■";
t = t->next;
}
t = head;
Gotoab(0, 28);
#if(0)
if (number <= 10) speed = 150;
else if (number > 10 && number <= 20) speed = 100;
else if (number > 20 && number <= 40) speed = 50;
else speed = 10;
#endif
speed = 150;
Sleep(speed);
}
void Changebody(int a,int b)
{
Snake *t = head->next;
int temp1, temp2, mid1, mid2;
temp1 = t->x; temp2 = t->y;
while (t->next->next!=NULL)
{
mid1 = t->next->x;
mid2 = t->next->y;
t->next->x = temp1;
t->next->y = temp2;
temp1 = mid1;
temp2 = mid2;
t = t->next;
}
t = head->next;
t->x = a;
t->y = b;
}
void Eating()
{
if (head->x == weizhix && head->y == weizhiy)
{
GreatFood();
Snake *stud = new Snake;
Snake *t;
t = head;
while (t->next->next!=NULL)
{
t = t->next;
} //插入在q后面;
t->next = stud;
stud->next = tail;
score+=10;
Gotoab(91, 10);
cout << score;
}
}
void Welcome()
{
Gotoab(25, 6);
cout << "/*****************************************************************/";
Gotoab(25, 25);
cout << "/*****************************************************************/";
Gotoab(40, 11);
printf("WELCOME TO THE GAME OF RETRO SNAKE");
Gotoab(30, 16);
cout<<"请在英文输入法中操作,反向移动等同于吃到自己,W A S D控制 P暂停";
Gotoab(40, 20);
cout<<"PLEASE ENTER YOUR NAME:";
cin >> name;
system("cls");
}
void End()
{
system("cls");
Gotoab(25, 6);
cout << "◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎";
Gotoab(40, 11);
cout << "GAME OVER (# ̄▽ ̄#)";
Gotoab(40, 16);
cout << "Your Score is " << score<<" ≥▽≤";
Gotoab(40, 20);
cout << "还不错哦,继续努力~(*▔^▔*)";
Gotoab(25, 25);
cout << "◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎";
cout << endl<<endl;
system("pause");
}