//贪吃蛇
//有bug
//请评论提出
#include <iostream>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>
#include <unistd.h>
#include<time.h>
#include<string>
using namespace std;
int fa = 4;//1上2下3左4右
int f = 0;
int cnt = 1;
int win = 20;
int x[2024] = {0,1},y[2024] = {0,1};
void HideCursor()//隐藏光标
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void SetPos(COORD a)// set cursor
{
HANDLE out=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(out, a);
}
void gotoxy(int x, int y)//去往点(x,y)
{
COORD pos={y,x};
SetPos(pos);
}
bool key()
{
if (kbhit())
{
return 1;
}
return 0;
}
void space(int a);
void clean();
void clean(int a);
string map[22][42]=
{
{" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "},
};
void menu();
void fang();
int ccc;
bool cc = false;
int xx = rand()%17+2;
int yy = rand()%37+2;
int xxx = rand()%17+2;
int yyy = rand()%37+2;
bool xxy = false;
bool xy = false;
bool sp = false;
void wing();
void print(string a);
#
#
int main()
{
int ccc;
while(1)
{
for (int i = 0;i <= 21;i++)
for (int j = 0;j <= 41;j++)
map[i][j] = " ";
map[xx][yy] = "□";
map[xxx][yyy] = "⊙";
for (int i = 1;i <= cnt;i++)
{
map[x[i]][y[i]] = "\033[33m■\033[0m";
}
menu();
for(int i = 1;i <= 15;i++)
{
if (key())
{
ccc = getch();
if(ccc == 'H'||ccc == 'P'||ccc == 'K'||ccc == 'M'||ccc == 27||ccc == 32)
{
cc = true;
break;
}
}
Sleep(20);
}
if(cc)
{
if (ccc == 'H')
{
x[1]--;
fa = 2;
}
else if (ccc == 'P')
{
x[1]++;
fa = 1;
}
else if (ccc == 'K')
{
y[1]--;
fa = 3;
}
else if (ccc == 'M')
{
y[1]++;
fa = 4;
}
for (int i = cnt;i >= 2;i--)
{
x[i] = x[i-1];
y[i] = y[i-1];
}
//转向
if (ccc == 32)
{
arand:;
ccc = getch();
if (ccc != 32)
goto arand;
else
sp = !sp;
}
//暂停
if (ccc == 27)
goto end;
}
//转向+暂停+退出
else
{
switch(fa)
{
case 1:x[1]++;break;
case 2:x[1]--;break;
case 3:y[1]--;break;
case 4:y[1]++;break;
}
for (int i = cnt;i >= 2;i--)
{
x[i] = x[i-1];
y[i] = y[i-1];
}
}
//不转向
if (x[1] == xx&&y[1] == yy&&xy == false)
{
xy = true;
cnt++;
f+=10;
xx = rand()%17+2;
yy = rand()%37+2;
x[cnt] = x[cnt-1];
y[cnt] = y[cnt-1];
if (fa == 1)
x[cnt]++;
else if(fa == 2)
x[cnt]--;
else if(fa == 3)
y[cnt]--;
else if (fa == 4)
y[cnt]++;
xy = false;
}
//吃东西
if (cnt == win)
{
wing();
return 0;
}
//胜利
if (x[1] == xxx&&y[1] == yyy&&xxy == false)
{
xxy = true;
cnt--;
f-=5;
xxx = rand()%17+2;
yyy = rand()%37+2;
xy = false;
}
//炸弹
if (cnt == 0 || x[1] == 0 || y[1] == 0)
{
clean();
gotoxy(11,31);
cout<<"失";
Sleep(60);
cout<<"败";
Sleep(60);
cout<<"!";
clean(200);
gotoxy(11,31);
cout<<"得分:"<<f;
gotoxy(25,1);
return 0;
}
clean();
}
end:;
return 0;
}
#
#
void print(string a)
{
for (int i = 1;i <= a.size();i++)
{
cout<<a.at(i);
Sleep(20);
}
}
void wing()
{
clean();
gotoxy(11,31);
cout<<"胜利!";
clean(500);
gotoxy(11,31);
cout<<"得分:"<<f;
gotoxy(25,1);
}
void fang()
{
}
void clean()
{
system("cls");
}
void clean(int a)
{
Sleep(a);
system("cls");
}
void menu()
{
HideCursor();
space(10);
cout<<"■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■"<<endl;
for (int i = 0;i <= 21;i++)
{
space(10);
cout<<"■";
for (int j = 0;j <= 41;j++)
cout<<map[i][j];
cout<<"■\n";
}
space (10);
cout<<"■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■"<<endl;
gotoxy(2,58);
cout<<"分数:";
gotoxy(3,59);
cout<<f;
gotoxy(6,58);
cout<<"长度:";
gotoxy(7,59);
cout<<cnt;
gotoxy(9,56);
cout<<"目标长度:";
gotoxy(10,59);
cout<<win;
if(!sp)
{
gotoxy(12,58);
cout<<"空格暂停";
}
else
{
gotoxy(12,55);
cout<<"再次按空格启动";
}
gotoxy(13,58);
cout<<"esc退出";
gotoxy(15,58);
cout<<"□ 是食物";
gotoxy(16,58);
cout<<"⊙ 是炸弹";
gotoxy(25,1);
}
void space(int a)
{
for (int i = 1;i <= a;i++)
cout<<" ";
}
07-19
1万+
01-21
1578